feat(update): add auto updates of package.json
All checks were successful
Discord / discord commits (push) Has been skipped

This commit is contained in:
matt1432 2024-11-30 17:59:44 -05:00
parent f52293ffe3
commit 2c94bc9809
6 changed files with 183 additions and 109 deletions

View file

@ -2,6 +2,7 @@
buildApp, buildApp,
callPackage, callPackage,
nodejs_latest, nodejs_latest,
prefetch-npm-deps,
... ...
}: }:
buildApp { buildApp {
@ -10,6 +11,7 @@ buildApp {
runtimeInputs = [ runtimeInputs = [
nodejs_latest nodejs_latest
prefetch-npm-deps
(callPackage ../../nixosModules/docker/updateImage.nix {}) (callPackage ../../nixosModules/docker/updateImage.nix {})
]; ];
} }

View file

@ -20,6 +20,7 @@ if (!FLAKE) {
const args = parseArgs(); const args = parseArgs();
const main = async() => {
if (args['d'] || args['docker']) { if (args['d'] || args['docker']) {
console.log(updateDocker()); console.log(updateDocker());
} }
@ -37,7 +38,9 @@ if (args['v'] || args['vuetorrent']) {
} }
if (args['c'] || args['custom-sidebar']) { if (args['c'] || args['custom-sidebar']) {
console.log(updateCustomPackage('scopedPackages.x86_64-linux.lovelace-components.custom-sidebar')); console.log(updateCustomPackage(
'scopedPackages.x86_64-linux.lovelace-components.custom-sidebar',
));
} }
if (args['s'] || args['some-sass-language-server']) { if (args['s'] || args['some-sass-language-server']) {
@ -45,7 +48,7 @@ if (args['s'] || args['some-sass-language-server']) {
} }
if (args['n'] || args['node_modules']) { if (args['n'] || args['node_modules']) {
updateNodeModules(); console.log(await updateNodeModules());
} }
if (args['a'] || args['all']) { if (args['a'] || args['all']) {
@ -65,12 +68,20 @@ if (args['a'] || args['all']) {
console.log(dockerOutput); console.log(dockerOutput);
const nodeModulesOutput = await updateNodeModules();
console.log(nodeModulesOutput);
const vuetorrentOutput = updateVuetorrent(); const vuetorrentOutput = updateVuetorrent();
console.log(vuetorrentOutput); console.log(vuetorrentOutput);
// This doesn't need to be added to commit msgs // This doesn't need to be added to commit msgs
console.log(updateCustomPackage('scopedPackages.x86_64-linux.lovelace-components.custom-sidebar')); console.log(updateCustomPackage(
'scopedPackages.x86_64-linux.lovelace-components.custom-sidebar',
));
console.log(updateCustomPackage('some-sass-language-server')); console.log(updateCustomPackage('some-sass-language-server'));
@ -79,21 +90,28 @@ if (args['a'] || args['all']) {
stdio: [process.stdin, process.stdout, process.stderr], stdio: [process.stdin, process.stdout, process.stderr],
}); });
const indentOutput = (output: string): string => {
return ` ${output.split('\n').join('\n ')}`;
};
const output = [ const output = [
'chore: update sources\n\n', 'chore: update sources\n\n',
]; ];
if (flakeOutput.length > 5) { if (flakeOutput.length > 5) {
output.push(`Flake Inputs:\n${flakeOutput}\n\n`); output.push(`Flake Inputs:\n${indentOutput(flakeOutput)}\n\n`);
} }
if (dockerOutput.length > 5) { if (dockerOutput.length > 5) {
output.push(`Docker Images:\n${dockerOutput}\n`); output.push(`Docker Images:\n${indentOutput(dockerOutput)}\n`);
} }
if (firefoxOutput.length > 5) { if (firefoxOutput.length > 5) {
output.push(`Firefox Addons:\n${firefoxOutput}\n\n`); output.push(`Firefox Addons:\n${indentOutput(firefoxOutput)}\n\n`);
}
if (nodeModulesOutput.length > 5) {
output.push(`Node modules:\n${indentOutput(nodeModulesOutput)}\n`);
} }
if (vuetorrentOutput.length > 5) { if (vuetorrentOutput.length > 5) {
output.push(`Misc Sources:\n${vuetorrentOutput}\n`); output.push(`Misc Sources:\n${indentOutput(vuetorrentOutput)}\n`);
} }
if (args['f']) { if (args['f']) {
@ -105,3 +123,6 @@ if (args['a'] || args['all']) {
} }
spawnSync('alejandra', ['-q', FLAKE], { shell: true }); spawnSync('alejandra', ['-q', FLAKE], { shell: true });
};
main();

View file

@ -1,4 +1,5 @@
import { spawnSync } from 'node:child_process'; import { spawnSync } from 'node:child_process';
import { readFileSync, writeFileSync } from 'node:fs';
export const parseArgs = () => { export const parseArgs = () => {
@ -28,3 +29,15 @@ export const parseFetchurl = (url: string) => JSON.parse(spawnSync(
'nix', ['store', 'prefetch-file', '--refresh', '--json', 'nix', ['store', 'prefetch-file', '--refresh', '--json',
'--hash-type', 'sha256', url, '--name', '"escaped"'], { shell: true }, '--hash-type', 'sha256', url, '--name', '"escaped"'], { shell: true },
).stdout.toString()).hash; ).stdout.toString()).hash;
export const replaceInFile = (replace: RegExp, replacement: string, file: string) => {
const fileContents = readFileSync(file);
const replaced = fileContents.toString().replace(replace, replacement);
writeFileSync(file, replaced);
};
export const npmRun = (args: string[], workspaceDir: string) => spawnSync(
'npm', args, { cwd: workspaceDir },
).stdout.toString();

View file

@ -1,42 +1,77 @@
import { readPackageJSON } from 'pkg-types'; import { readPackageJSON, writePackageJSON } from 'pkg-types';
import { readdirSync } from 'node:fs'; import { readdirSync } from 'node:fs';
import { spawnSync } from 'node:child_process'; import { spawnSync } from 'node:child_process';
import { replaceInFile, npmRun } from './lib';
/* Constants */ /* Constants */
const FLAKE = process.env.FLAKE as string; const FLAKE = process.env.FLAKE as string;
export default () => {
readdirSync(FLAKE, { withFileTypes: true, recursive: true }).forEach(async(path) => {
if (path.name === 'package.json' && !path.parentPath.includes('node_modules')) {
const currentWorkspace = path.parentPath;
const currentPackageJson = await readPackageJSON(`${currentWorkspace}/package.json`); const updatePackageJson = async(workspaceDir: string, updates: object) => {
const outdated = JSON.parse(spawnSync( const currentPackageJson = await readPackageJSON(`${workspaceDir}/package.json`);
'npm',
['outdated', '--json'],
{ cwd: currentWorkspace },
).stdout.toString());
Object.keys(currentPackageJson.dependencies ?? {}).forEach((dep) => { const outdated = JSON.parse(npmRun(['outdated', '--json'], workspaceDir));
const updateDeps = (deps: string) => {
Object.keys(currentPackageJson[deps]).forEach((dep) => {
const versions = outdated[dep]; const versions = outdated[dep];
if (!versions?.current) { if (!versions?.current) {
return; return;
} }
console.log(`${dep}: ${versions.current} -> ${versions.latest}`); if (!updates[dep]) {
}); updates[dep] = `${versions.current} -> ${versions.latest}`;
Object.keys(currentPackageJson.devDependencies ?? {}).forEach((dep) => {
const versions = outdated[dep];
if (!versions?.current) {
return;
} }
console.log(`${dep}: ${versions.current} -> ${versions.latest}`); currentPackageJson[deps][dep] = versions.latest;
});
}
}); });
}; };
if (currentPackageJson.dependencies) {
updateDeps('dependencies');
}
if (currentPackageJson.devDependencies) {
updateDeps('devDependencies');
}
await writePackageJSON(`${workspaceDir}/package.json`, currentPackageJson);
};
const prefetchNpmDeps = (workspaceDir: string): string => {
npmRun(['install', '--package-lock-only'], workspaceDir);
return spawnSync(
'prefetch-npm-deps',
[`${workspaceDir}/package-lock.json`],
).stdout.toString().replace('\n', '');
};
export default async() => {
const updates = {};
const packages = readdirSync(FLAKE, { withFileTypes: true, recursive: true });
for (const path of packages) {
if (path.name === 'package.json' && !path.parentPath.includes('node_modules')) {
await updatePackageJson(path.parentPath, updates);
const hash = prefetchNpmDeps(path.parentPath);
replaceInFile(
/npmDepsHash = ".*";/,
`npmDepsHash = "${hash}";`,
`${path.parentPath}/default.nix`,
);
}
}
return Object.entries(updates)
.map(([key, dep]) => `${key}: ${dep}`)
.join('\n');
};

View file

@ -0,0 +1,3 @@
{
npmDepsHash = "sha256-XNvj59XfO6f+04PatCOZ93tkkZ1K7jReZPqLGJL2Ojo=";
}

View file

@ -98,7 +98,7 @@ in {
}) })
// { // {
"${cfg.configDir}/node_modules".source = "${cfg.configDir}/node_modules".source =
buildNodeModules ./config "sha256-XNvj59XfO6f+04PatCOZ93tkkZ1K7jReZPqLGJL2Ojo="; buildNodeModules ./config (import ./config).npmDepsHash;
"${cfg.configDir}/tsconfig.json".source = let "${cfg.configDir}/tsconfig.json".source = let
inherit (ags.packages.${pkgs.system}) gjs; inherit (ags.packages.${pkgs.system}) gjs;