chore: npm translator: optimizations and fixes

- don't use expensive replaceRootSources
- don't mess with sources, just use data from package-lock
This commit is contained in:
DavHau 2022-09-05 11:24:51 +02:00
parent 1d182f0808
commit 12267818db
2 changed files with 26 additions and 20 deletions

View File

@ -20,6 +20,7 @@
moreutils,
nodePackages,
openssh,
python3,
writeScriptBin,
...
}:
@ -32,6 +33,7 @@
moreutils
nodePackages.npm
openssh
python3
]
''
# accroding to the spec, the translator reads the input from a json file
@ -42,6 +44,9 @@
name=$(jq '.project.name' -c -r $jsonInput)
version=$(jq '.project.version' -c -r $jsonInput)
npmArgs=$(jq '.project.subsystemInfo.npmArgs' -c -r $jsonInput)
if [ "$npmArgs" == "null" ]; then
npmArgs=
fi
if [ "$version" = "null" ]; then
candidate="$name"
@ -49,7 +54,6 @@
candidate="$name@$version"
fi
pushd $TMPDIR
newSource=$(pwd)
@ -62,26 +66,14 @@
# call package-lock translator
${subsystems.nodejs.translators.package-lock.translateBin} $TMPDIR/newJsonInput
# generate source info for main package
url=$(npm view $candidate dist.tarball)
hash=$(npm view $candidate dist.integrity)
echo "
{
\"type\": \"http\",
\"url\": \"$url\",
\"hash\": \"$hash\"
}
" > $TMPDIR/sourceInfo.json
# get resolved package version
export version=$(npm view $candidate version)
# set correct package version under `packages`
cat $outputFile \
| python3 ${./fixup-dream-lock.py} $TMPDIR/sourceInfo.json \
| sponge $outputFile
# add main package source info to dream-lock.json
${apps.callNixWithD2N} eval --json "
with dream2nix.utils.dreamLock;
replaceRootSources {
dreamLock = l.fromJSON (l.readFile \"$outputFile\");
newSourceRoot = l.fromJSON (l.readFile \"$TMPDIR/sourceInfo.json\");
}
" \
| sponge "$outputFile"
'';
# inherit options from package-lock translator

View File

@ -0,0 +1,14 @@
import json
import os
import sys
lock = json.load(sys.stdin)
version = os.environ.get('version')
# set default package version correctly
defaultPackage = lock['_generic']['defaultPackage']
lock['_generic']['packages'] = {
defaultPackage: version
}
print(json.dumps(lock, indent=2))