From 1beb1eaab56e719e938c0b1759c9df91b29639be Mon Sep 17 00:00:00 2001 From: DavHau Date: Fri, 29 Jul 2022 15:17:32 +0200 Subject: [PATCH] feat: improve nodejs translators/builders - also execute preinstall script during package installation - disable devDependencies if no instal script present by default --- .../nodejs/builders/granular/default.nix | 3 ++ .../translators/package-json/default.nix | 22 ++++++++++-- .../translators/package-lock/default.nix | 36 ++++++++++++++----- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/subsystems/nodejs/builders/granular/default.nix b/src/subsystems/nodejs/builders/granular/default.nix index a6c37b65..2fb10bee 100644 --- a/src/subsystems/nodejs/builders/granular/default.nix +++ b/src/subsystems/nodejs/builders/granular/default.nix @@ -473,6 +473,9 @@ elif [ -n "$runBuild" ] && [ "$(jq '.scripts.build' ./package.json)" != "null" ]; then npm run build else + if [ "$(jq '.scripts.preinstall' ./package.json)" != "null" ]; then + npm --production --offline --nodedir=$nodeSources run preinstall + fi if [ "$(jq '.scripts.install' ./package.json)" != "null" ]; then npm --production --offline --nodedir=$nodeSources run install fi diff --git a/src/subsystems/nodejs/translators/package-json/default.nix b/src/subsystems/nodejs/translators/package-json/default.nix index 02dd3cda..adfd8979 100644 --- a/src/subsystems/nodejs/translators/package-json/default.nix +++ b/src/subsystems/nodejs/translators/package-json/default.nix @@ -47,11 +47,29 @@ cd ./$relPath rm -rf package-lock.json - if [ "$(jq '.project.subsystemInfo.noDev' -c -r $jsonInput)" == "true" ]; then + hasInstallScript=false + if [ "$(jq 'has("scripts")' -c -r package.json)" == "false" ]; then + : + elif [ "$(jq '.scripts | has("preinstall")' -c -r package.json)" == "true" ]; then + hasInstallScript=true + elif [ "$(jq '.scripts | has("install")' -c -r package.json)" == "true" ]; then + hasInstallScript=true + elif [ "$(jq '.scripts | has("postinstall")' -c -r package.json)" == "true" ]; then + hasInstallScript=true + else + : + fi + + if [ "$hasInstallScript" == "false" ]; then + echo "package.json does not define an [pre|post]install script -> omitting dev dependencies" + fi + + if [ "$hasInstallScript" == "false" ] \ + || [ "$(jq '.project.subsystemInfo.noDev' -c -r $jsonInput)" == "true" ]; then echo "excluding dev dependencies" jq '.devDependencies = {}' ./package.json > package.json.mod mv package.json.mod package.json - npm install --package-lock-only --production $npmArgs + npm install --package-lock-only --omit=dev $npmArgs else npm install --package-lock-only $npmArgs fi diff --git a/src/subsystems/nodejs/translators/package-lock/default.nix b/src/subsystems/nodejs/translators/package-lock/default.nix index cf5117f1..7a3cac5d 100644 --- a/src/subsystems/nodejs/translators/package-lock/default.nix +++ b/src/subsystems/nodejs/translators/package-lock/default.nix @@ -25,7 +25,15 @@ } @ args: let b = builtins; - dev = ! noDev; + hasInstallScript = + (packageJson ? scripts.preinstall) + || (packageJson ? scripts.install) + || (packageJson ? scripts.postinstall); + + noDev = + if ! hasInstallScript + then true + else args.noDev; name = project.name; tree = args.tree.getNodeFromPath project.relPath; relPath = project.relPath; @@ -70,11 +78,23 @@ then let path = getPath dependencyObject; in - ( - b.fromJSON - (b.readFile "${source}/${path}/package.json") - ) - .version + if ! (l.pathExists "${source}/${path}/package.json") + then + throw '' + The lock file references a sub-package residing at '${source}/${path}', + but that directory doesn't exist or doesn't contain a package.json + + The reason might be that devDependencies are not included in this package release. + Possible solutions: + - get full package source via git and translate from there + - disable devDependencies by passing `noDev` to the translator + '' + else + ( + b.fromJSON + (b.readFile "${source}/${path}/package.json") + ) + .version else if lib.hasPrefix "https://" dependencyObject.version then "unknown" else dependencyObject.version; @@ -153,7 +173,7 @@ version = getVersion pdata; }) (lib.filterAttrs - (pname: pdata: ! (pdata.dev or false) || dev) + (pname: pdata: ! (pdata.dev or false) || ! noDev) parsedDependencies); subsystemName = "nodejs"; @@ -186,7 +206,7 @@ in lib.filter (pdata: - dev || ! (pdata.dev or false)) + ! noDev || ! (pdata.dev or false)) (lib.flatten (serialize inputData)); getName = dependencyObject: dependencyObject.pname;