From 55ddcfecea7dc0ad8426d1e52bbd04bde4f71f82 Mon Sep 17 00:00:00 2001 From: matt1432 Date: Wed, 22 Jan 2025 12:10:36 -0500 Subject: [PATCH] feat(update): improve error handling and fix problems with node_modules --- apps/update-sources/default.nix | 2 ++ apps/update-sources/src/node-modules.ts | 35 ++++++++++++++++++------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/apps/update-sources/default.nix b/apps/update-sources/default.nix index 02115d21..949d8004 100644 --- a/apps/update-sources/default.nix +++ b/apps/update-sources/default.nix @@ -1,6 +1,7 @@ { buildApp, callPackage, + findutils, go, nix-update, nodejs_latest, @@ -12,6 +13,7 @@ buildApp { npmDepsHash = "sha256-k4m8fSF0zOznebbH87p8IPP2SzRR9siVFYBU5Cfs2T0="; runtimeInputs = [ + findutils go nix-update nodejs_latest diff --git a/apps/update-sources/src/node-modules.ts b/apps/update-sources/src/node-modules.ts index 0029b59f..c6fde076 100644 --- a/apps/update-sources/src/node-modules.ts +++ b/apps/update-sources/src/node-modules.ts @@ -1,5 +1,5 @@ import { readPackageJSON, writePackageJSON } from 'pkg-types'; -import { existsSync, readdirSync } from 'node:fs'; +import { accessSync, constants, existsSync } from 'node:fs'; import { spawnSync } from 'node:child_process'; import { replaceInFile, npmRun } from './lib'; @@ -54,27 +54,42 @@ const prefetchNpmDeps = (workspaceDir: string): string => { export default async() => { + console.log('Updating node modules'); + const updates = {}; - const packages = readdirSync(FLAKE, { withFileTypes: true, recursive: true }); + 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))); for (const path of packages) { - if ( - path.name === 'package.json' && - !path.parentPath.includes('node_modules') - ) { - await updatePackageJson(path.parentPath, updates); + console.log(path); - if (existsSync(`${path.parentPath}/default.nix`)) { - const hash = prefetchNpmDeps(path.parentPath); + try { + accessSync(path, constants.R_OK | constants.W_OK); + + const parentPath = path.replace('/package.json', ''); + + await updatePackageJson(parentPath, updates); + + if (existsSync(`${parentPath}/default.nix`)) { + const hash = prefetchNpmDeps(parentPath); replaceInFile( /npmDepsHash = ".*";/, `npmDepsHash = "${hash}";`, - `${path.parentPath}/default.nix`, + `${parentPath}/default.nix`, ); } } + catch (e) { + console.warn(`Could not write to ${path}`); + console.warn(e); + } } return Object.entries(updates)