2024-11-30 17:59:44 -05:00
|
|
|
import { readPackageJSON, writePackageJSON } from 'pkg-types';
|
2025-01-22 12:10:36 -05:00
|
|
|
import { accessSync, constants, existsSync } from 'node:fs';
|
2024-11-30 03:44:34 -05:00
|
|
|
import { spawnSync } from 'node:child_process';
|
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
import { replaceInFile, npmRun } from './lib';
|
|
|
|
|
2024-11-30 03:44:34 -05:00
|
|
|
|
|
|
|
/* Constants */
|
|
|
|
const FLAKE = process.env.FLAKE as string;
|
|
|
|
|
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
const updatePackageJson = async(workspaceDir: string, updates: object) => {
|
|
|
|
const currentPackageJson = await readPackageJSON(`${workspaceDir}/package.json`);
|
|
|
|
|
|
|
|
const outdated = JSON.parse(npmRun(['outdated', '--json'], workspaceDir));
|
2024-11-30 03:44:34 -05:00
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
const updateDeps = (deps: string) => {
|
|
|
|
Object.keys(currentPackageJson[deps]).forEach((dep) => {
|
|
|
|
const versions = outdated[dep];
|
2024-12-11 18:06:48 -05:00
|
|
|
const current = versions?.wanted || versions?.current;
|
2024-11-30 03:44:34 -05:00
|
|
|
|
2024-12-11 18:06:48 -05:00
|
|
|
if (!current) {
|
2024-11-30 17:59:44 -05:00
|
|
|
return;
|
|
|
|
}
|
2024-11-30 03:44:34 -05:00
|
|
|
|
2024-12-11 18:06:48 -05:00
|
|
|
if (current !== versions.latest) {
|
|
|
|
updates[dep] = `${current} -> ${versions.latest}`;
|
2024-11-30 17:59:44 -05:00
|
|
|
}
|
2024-11-30 03:44:34 -05:00
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
currentPackageJson[deps][dep] = versions.latest;
|
|
|
|
});
|
|
|
|
};
|
2024-11-30 03:44:34 -05:00
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
if (currentPackageJson.dependencies) {
|
|
|
|
updateDeps('dependencies');
|
|
|
|
}
|
2024-11-30 03:44:34 -05:00
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
if (currentPackageJson.devDependencies) {
|
|
|
|
updateDeps('devDependencies');
|
|
|
|
}
|
|
|
|
|
|
|
|
await writePackageJSON(`${workspaceDir}/package.json`, currentPackageJson);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const prefetchNpmDeps = (workspaceDir: string): string => {
|
2025-01-04 23:48:28 -05:00
|
|
|
npmRun(['update', '--package-lock-only'], workspaceDir);
|
2024-11-30 17:59:44 -05:00
|
|
|
|
|
|
|
return spawnSync(
|
|
|
|
'prefetch-npm-deps',
|
|
|
|
[`${workspaceDir}/package-lock.json`],
|
|
|
|
).stdout.toString().replace('\n', '');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default async() => {
|
2025-01-22 12:10:36 -05:00
|
|
|
console.log('Updating node modules');
|
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
const updates = {};
|
|
|
|
|
2025-01-22 12:10:36 -05:00
|
|
|
const packages = spawnSync('find', [FLAKE, '-name', 'package.json']).stdout.toString().split('\n')
|
|
|
|
.filter((f) => f !== '')
|
|
|
|
.filter((f) => ![
|
|
|
|
'.direnv',
|
|
|
|
'node_modules',
|
|
|
|
'results',
|
|
|
|
].some((dirName) => f.includes(dirName)));
|
2024-11-30 17:59:44 -05:00
|
|
|
|
|
|
|
for (const path of packages) {
|
2025-01-22 12:10:36 -05:00
|
|
|
console.log(path);
|
|
|
|
|
|
|
|
try {
|
|
|
|
accessSync(path, constants.R_OK | constants.W_OK);
|
|
|
|
|
|
|
|
const parentPath = path.replace('/package.json', '');
|
2024-11-30 17:59:44 -05:00
|
|
|
|
2025-01-22 12:10:36 -05:00
|
|
|
await updatePackageJson(parentPath, updates);
|
|
|
|
|
|
|
|
if (existsSync(`${parentPath}/default.nix`)) {
|
|
|
|
const hash = prefetchNpmDeps(parentPath);
|
2024-11-30 17:59:44 -05:00
|
|
|
|
2025-01-02 02:02:09 -05:00
|
|
|
replaceInFile(
|
|
|
|
/npmDepsHash = ".*";/,
|
|
|
|
`npmDepsHash = "${hash}";`,
|
2025-01-22 12:10:36 -05:00
|
|
|
`${parentPath}/default.nix`,
|
2025-01-02 02:02:09 -05:00
|
|
|
);
|
|
|
|
}
|
2025-02-01 14:27:32 -05:00
|
|
|
|
|
|
|
// Make sure we update the apps' config package-lock.json
|
|
|
|
if (parentPath.includes('apps/config')) {
|
|
|
|
npmRun(['i'], parentPath);
|
|
|
|
}
|
2024-11-30 03:44:34 -05:00
|
|
|
}
|
2025-01-22 12:10:36 -05:00
|
|
|
catch (e) {
|
|
|
|
console.warn(`Could not write to ${path}`);
|
|
|
|
console.warn(e);
|
|
|
|
}
|
2024-11-30 17:59:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Object.entries(updates)
|
|
|
|
.map(([key, dep]) => `${key}: ${dep}`)
|
|
|
|
.join('\n');
|
2024-11-30 03:44:34 -05:00
|
|
|
};
|