Skip to content

Commit 41a9c70

Browse files
committed
nodejs: move resolved-json to ifd translator
1 parent f4b734e commit 41a9c70

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

src/subsystems/nodejs/discoverers/default/default.nix

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
getTranslatorNames = path: let
3939
nodes = l.readDir path;
40-
packageJson = l.fromJSON (l.readFile "${path}/package.json");
40+
packageJson = getPackageJson path;
4141
translators =
4242
# if the package has no dependencies we use the
4343
# package-lock translator with `packageLock = null`
@@ -46,15 +46,27 @@
4646
&& (packageJson.devDependencies or {} == {})
4747
&& (packageJson.workspaces or [] == [])
4848
then ["package-lock"]
49+
else if nodes ? "package-lock.json"
50+
then let
51+
# Wish there was a way to get the version without reading a 2MB file
52+
lockJson = getLockJson path;
53+
lockVersion = lockJson.lockfileVersion or 0;
54+
in
55+
if lockVersion == 1
56+
then ["package-lock"]
57+
else if lockVersion == 2
58+
then ["resolved-json"]
59+
else ["package-json"]
4960
else
5061
l.optionals (nodes ? "yarn.lock") ["yarn-lock"]
5162
++ ["package-json"];
5263
in
5364
translators;
5465

5566
# returns the parsed package.json of a given directory
56-
getPackageJson = dirPath:
57-
l.fromJSON (l.readFile "${dirPath}/package.json");
67+
getJson = jsonPath: l.fromJSON (l.readFile jsonPath);
68+
getPackageJson = dirPath: getJson "${dirPath}/package.json";
69+
getLockJson = dirPath: getJson "${dirPath}/package-lock.json";
5870

5971
# returns all relative paths to workspaces defined by a glob
6072
getWorkspacePaths = glob: tree:

src/subsystems/nodejs/translators/package-json/default.nix

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
git
3030
jq
3131
nodejs-18_x
32-
# nodejs-18_x.pkgs.npm
3332
openssh
3433
]
3534
''
@@ -49,16 +48,10 @@
4948
5049
cd ./$relPath
5150
52-
# create package lock if missing or old
53-
if ! [ -r package-lock.json ] || [ $(jq '.lockfileVersion' -r package-lock.json) != 2 ]; then
54-
rm -f package-lock.json
51+
rm -f package-lock.json
52+
echo Generating temporary package-lock.json with npm
5553
56-
# TODO: some easy way to add --offline, maybe separate flake run script? Speeds up enormously when cached.
57-
npm install --package-lock-only $npmArgs
58-
fi
59-
60-
# resolve packages - TODO move to RunCommandLocal
61-
node ${./resolver.cjs} package-lock.json resolved.json
54+
npm install --package-lock-only $npmArgs
6255
6356
jq ".source = \"$newSource\"" -c -r $jsonInput > $TMPDIR/newJsonInput
6457

src/subsystems/nodejs/translators/resolved-json/default.nix

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,37 @@
77
l = lib // builtins;
88
nodejsUtils = import ../utils.nix {inherit lib;};
99

10-
getResolved = tree: project:
11-
nodejsUtils.getWorkspaceLockFile tree project "resolved.json";
12-
1310
translate = {
1411
translatorName,
1512
utils,
13+
pkgs,
1614
...
1715
}: {
1816
project,
1917
source,
2018
tree,
2119
# translator args
22-
noDev,
20+
name ? project.name or "untitled",
2321
nodejs,
2422
...
2523
} @ args: let
2624
b = builtins;
2725

28-
# TODO only dev for devShell
29-
dev = ! noDev;
30-
name = project.name;
3126
tree = args.tree.getNodeFromPath project.relPath;
3227
relPath = project.relPath;
3328
source = "${args.source}/${relPath}";
3429
workspaces = project.subsystemInfo.workspaces or [];
3530

36-
lock = (getResolved args.tree project).jsonContent or null;
31+
getResolved = tree: project: let
32+
lock =
33+
nodejsUtils.getWorkspaceLockFile tree project "package-lock.json";
34+
resolved = pkgs.runCommandLocal "resolved.json" {} ''
35+
${nodejs}/bin/node ${./resolver.cjs} ${lock.fullPath} $out
36+
'';
37+
in
38+
l.fromJSON (l.readFile resolved);
39+
40+
lock = getResolved args.tree project;
3741

3842
packageVersion = lock.version or "unknown";
3943

@@ -113,7 +117,7 @@
113117
in rec {
114118
version = 2;
115119

116-
type = "pure";
120+
type = "ifd";
117121

118122
inherit translate;
119123

0 commit comments

Comments
 (0)