2024-11-30 17:59:44 -05:00
|
|
|
import { readPackageJSON, writePackageJSON } from 'pkg-types';
|
2024-11-30 03:44:34 -05:00
|
|
|
import { readdirSync } from 'node:fs';
|
|
|
|
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-11-30 03:44:34 -05:00
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
if (!versions?.current) {
|
|
|
|
return;
|
|
|
|
}
|
2024-11-30 03:44:34 -05:00
|
|
|
|
2024-11-30 17:59:44 -05:00
|
|
|
if (!updates[dep]) {
|
|
|
|
updates[dep] = `${versions.current} -> ${versions.latest}`;
|
|
|
|
}
|
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 => {
|
|
|
|
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`,
|
|
|
|
);
|
2024-11-30 03:44:34 -05:00
|
|
|
}
|
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
|
|
|
};
|