Skip to content

Commit

Permalink
nodejs: move resolved-json to ifd translator
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Jul 16, 2022
1 parent f4b734e commit fa3e37f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
18 changes: 15 additions & 3 deletions src/subsystems/nodejs/discoverers/default/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -46,15 +46,27 @@
&& (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"];
in
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:
Expand Down
13 changes: 3 additions & 10 deletions src/subsystems/nodejs/translators/package-json/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
git
jq
nodejs-18_x
# nodejs-18_x.pkgs.npm
openssh
]
''
Expand All @@ -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
Expand Down
35 changes: 13 additions & 22 deletions src/subsystems/nodejs/translators/resolved-json/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,36 @@
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,
nodejs,
name ? project.name or "untitled",
...
} @ 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" {} ''
${pkgs.nodejs}/bin/node ${./resolver.cjs} ${lock.fullPath} $out
'';
in
l.fromJSON (l.readFile resolved);

lock = getResolved args.tree project;

packageVersion = lock.version or "unknown";

Expand Down Expand Up @@ -113,7 +116,7 @@
in rec {
version = 2;

type = "pure";
type = "ifd";

inherit translate;

Expand All @@ -127,17 +130,5 @@ in rec {
default = "{automatic}";
type = "argument";
};

# TODO: this should either be removed or only used to select
# the nodejs version for translating, not for building.
nodejs = {
description = "nodejs version to use for building";
default = "14";
examples = [
"14"
"16"
];
type = "argument";
};
};
}

0 comments on commit fa3e37f

Please sign in to comment.