diff --git a/src/subsystems/nodejs/discoverers/default/default.nix b/src/subsystems/nodejs/discoverers/default/default.nix index b7307178b5..f04c28fd1a 100644 --- a/src/subsystems/nodejs/discoverers/default/default.nix +++ b/src/subsystems/nodejs/discoverers/default/default.nix @@ -37,7 +37,7 @@ getTranslatorNames = path: let nodes = l.readDir path; - packageJson = l.fromJSON (l.readFile "${path}/package.json"); + packageJson = getPackageJson path; translators = # if the package has no dependencies we use the # package-lock translator with `packageLock = null` @@ -46,6 +46,17 @@ && (packageJson.devDependencies or {} == {}) && (packageJson.workspaces or [] == []) then ["package-lock"] + else if nodes ? "package-lock.json" + then let + # Wish there was a way to get the version without reading a 2MB file + lockJson = getLockJson path; + lockVersion = lockJson.lockfileVersion or 0; + in + if lockVersion == 1 + then ["package-lock"] + else if lockVersion == 2 + then ["resolved-json"] + else ["package-json"] else l.optionals (nodes ? "yarn.lock") ["yarn-lock"] ++ ["package-json"]; @@ -53,8 +64,9 @@ translators; # returns the parsed package.json of a given directory - getPackageJson = dirPath: - l.fromJSON (l.readFile "${dirPath}/package.json"); + getJson = jsonPath: l.fromJSON (l.readFile jsonPath); + getPackageJson = dirPath: getJson "${dirPath}/package.json"; + getLockJson = dirPath: getJson "${dirPath}/package-lock.json"; # returns all relative paths to workspaces defined by a glob getWorkspacePaths = glob: tree: diff --git a/src/subsystems/nodejs/translators/package-json/default.nix b/src/subsystems/nodejs/translators/package-json/default.nix index 053e9b1409..971043ac7b 100644 --- a/src/subsystems/nodejs/translators/package-json/default.nix +++ b/src/subsystems/nodejs/translators/package-json/default.nix @@ -29,7 +29,6 @@ git jq nodejs-18_x - # nodejs-18_x.pkgs.npm openssh ] '' @@ -49,16 +48,10 @@ cd ./$relPath - # create package lock if missing or old - if ! [ -r package-lock.json ] || [ $(jq '.lockfileVersion' -r package-lock.json) != 2 ]; then - rm -f package-lock.json + rm -f package-lock.json + echo Generating temporary package-lock.json with npm - # TODO: some easy way to add --offline, maybe separate flake run script? Speeds up enormously when cached. - npm install --package-lock-only $npmArgs - fi - - # resolve packages - TODO move to RunCommandLocal - node ${./resolver.cjs} package-lock.json resolved.json + npm install --package-lock-only $npmArgs jq ".source = \"$newSource\"" -c -r $jsonInput > $TMPDIR/newJsonInput diff --git a/src/subsystems/nodejs/translators/resolved-json/default.nix b/src/subsystems/nodejs/translators/resolved-json/default.nix index fec773fdc7..5a1333283a 100644 --- a/src/subsystems/nodejs/translators/resolved-json/default.nix +++ b/src/subsystems/nodejs/translators/resolved-json/default.nix @@ -7,33 +7,37 @@ l = lib // builtins; nodejsUtils = import ../utils.nix {inherit lib;}; - getResolved = tree: project: - nodejsUtils.getWorkspaceLockFile tree project "resolved.json"; - translate = { translatorName, utils, + pkgs, ... }: { project, source, tree, # translator args - noDev, + name ? project.name or "untitled", nodejs, ... } @ args: let b = builtins; - # TODO only dev for devShell - dev = ! noDev; - name = project.name; tree = args.tree.getNodeFromPath project.relPath; relPath = project.relPath; source = "${args.source}/${relPath}"; workspaces = project.subsystemInfo.workspaces or []; - lock = (getResolved args.tree project).jsonContent or null; + getResolved = tree: project: let + lock = + nodejsUtils.getWorkspaceLockFile tree project "package-lock.json"; + resolved = pkgs.runCommandLocal "resolved.json" {} '' + ${nodejs}/bin/node ${./resolver.cjs} ${lock.fullPath} $out + ''; + in + l.fromJSON (l.readFile resolved); + + lock = getResolved args.tree project; packageVersion = lock.version or "unknown"; @@ -113,7 +117,7 @@ in rec { version = 2; - type = "pure"; + type = "ifd"; inherit translate; diff --git a/src/subsystems/nodejs/translators/package-json/resolver.cjs b/src/subsystems/nodejs/translators/resolved-json/resolver.cjs similarity index 100% rename from src/subsystems/nodejs/translators/package-json/resolver.cjs rename to src/subsystems/nodejs/translators/resolved-json/resolver.cjs