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-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 => {
|
|
|
|
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) {
|
2024-12-11 18:06:48 -05:00
|
|
|
if (
|
|
|
|
path.name === 'package.json' &&
|
2024-12-21 05:14:23 -05:00
|
|
|
!path.parentPath.includes('node_modules') &&
|
|
|
|
!path.parentPath.includes('apps/config')
|
2024-12-11 18:06:48 -05:00
|
|
|
) {
|
2024-11-30 17:59:44 -05:00
|
|
|
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
|
|
|
};
|