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

View file

@ -20,35 +20,38 @@ if (!FLAKE) {
const args = parseArgs();
if (args['d'] || args['docker']) {
const main = async() => {
if (args['d'] || args['docker']) {
console.log(updateDocker());
}
}
if (args['i'] || args['inputs']) {
if (args['i'] || args['inputs']) {
console.log(updateFlakeInputs());
}
}
if (args['f'] || args['firefox']) {
if (args['f'] || args['firefox']) {
console.log(updateFirefoxAddons());
}
}
if (args['v'] || args['vuetorrent']) {
if (args['v'] || args['vuetorrent']) {
console.log(updateVuetorrent());
}
}
if (args['c'] || args['custom-sidebar']) {
console.log(updateCustomPackage('scopedPackages.x86_64-linux.lovelace-components.custom-sidebar'));
}
if (args['c'] || args['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']) {
console.log(updateCustomPackage('some-sass-language-server'));
}
}
if (args['n'] || args['node_modules']) {
updateNodeModules();
}
if (args['n'] || args['node_modules']) {
console.log(await updateNodeModules());
}
if (args['a'] || args['all']) {
if (args['a'] || args['all']) {
// Update this first because of nix run cmd
const firefoxOutput = updateFirefoxAddons();
@ -65,12 +68,20 @@ if (args['a'] || args['all']) {
console.log(dockerOutput);
const nodeModulesOutput = await updateNodeModules();
console.log(nodeModulesOutput);
const vuetorrentOutput = updateVuetorrent();
console.log(vuetorrentOutput);
// 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'));
@ -79,21 +90,28 @@ if (args['a'] || args['all']) {
stdio: [process.stdin, process.stdout, process.stderr],
});
const indentOutput = (output: string): string => {
return ` ${output.split('\n').join('\n ')}`;
};
const output = [
'chore: update sources\n\n',
];
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) {
output.push(`Docker Images:\n${dockerOutput}\n`);
output.push(`Docker Images:\n${indentOutput(dockerOutput)}\n`);
}
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) {
output.push(`Misc Sources:\n${vuetorrentOutput}\n`);
output.push(`Misc Sources:\n${indentOutput(vuetorrentOutput)}\n`);
}
if (args['f']) {
@ -102,6 +120,9 @@ if (args['a'] || args['all']) {
else {
console.log(output.join(''));
}
}
}
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 { readFileSync, writeFileSync } from 'node:fs';
export const parseArgs = () => {
@ -28,3 +29,15 @@ export const parseFetchurl = (url: string) => JSON.parse(spawnSync(
'nix', ['store', 'prefetch-file', '--refresh', '--json',
'--hash-type', 'sha256', url, '--name', '"escaped"'], { shell: true },
).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 { spawnSync } from 'node:child_process';
import { replaceInFile, npmRun } from './lib';
/* Constants */
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 outdated = JSON.parse(spawnSync(
'npm',
['outdated', '--json'],
{ cwd: currentWorkspace },
).stdout.toString());
const updatePackageJson = async(workspaceDir: string, updates: object) => {
const currentPackageJson = await readPackageJSON(`${workspaceDir}/package.json`);
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];
if (!versions?.current) {
return;
}
console.log(`${dep}: ${versions.current} -> ${versions.latest}`);
});
Object.keys(currentPackageJson.devDependencies ?? {}).forEach((dep) => {
const versions = outdated[dep];
if (!versions?.current) {
return;
if (!updates[dep]) {
updates[dep] = `${versions.current} -> ${versions.latest}`;
}
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 =
buildNodeModules ./config "sha256-XNvj59XfO6f+04PatCOZ93tkkZ1K7jReZPqLGJL2Ojo=";
buildNodeModules ./config (import ./config).npmDepsHash;
"${cfg.configDir}/tsconfig.json".source = let
inherit (ags.packages.${pkgs.system}) gjs;