From f0492d0235acb5369e1a60754c0902c70f5c931f Mon Sep 17 00:00:00 2001 From: John Perez Date: Tue, 8 Feb 2022 17:25:35 -0500 Subject: [PATCH] feat(back): #789 build package lock - Script to build `package-lock.json` using makes, to keep lock versions between the devs involved in a project. --- README.md | 25 +++++++++++++++++++ makes/utils/makeNodeJsLockfile/entrypoint.sh | 26 ++++++++++++++++++++ makes/utils/makeNodeJsLockfile/main.nix | 15 +++++++++++ 3 files changed, 66 insertions(+) create mode 100644 makes/utils/makeNodeJsLockfile/entrypoint.sh create mode 100644 makes/utils/makeNodeJsLockfile/main.nix diff --git a/README.md b/README.md index 609c05ee..b23c7ece 100644 --- a/README.md +++ b/README.md @@ -3707,6 +3707,31 @@ It appends: - `node_modules/.bin` to `PATH`. - `node_modules` to [NODE_PATH][NODE_PATH]. +Pre-requisites: + +1. You can generate a `package-lock.json` using a specific `node` version + like this: + + ```bash + m github:fluidattacks/makes@22.02 /utils/makeNodeJsLockfile \ + "${node_js_version}" \ + "${package_json}" \ + "${package_lock} + ``` + + - Supported `node_js_version`s are: `10`, `12`, `14` and `16`. + - `package_json` is the **absolute path** to the `package.json` file in your + project. + - `package_lock` is the **absolute path** + to the `package-lock.json` file in your project, this file can be an empty + file. + +Note: the reason behind this script is to maintain `lock` versions through the +construction of `makeNodeJsEnvironment`, since you can have an updated version +of `nodejs` or `npm` but not the same as the version stored on the +[Nixpkgs][NIXPKGS] repository. This doesn't affect your local version +of `npm` or `nodejs`. + Types: - makeNodeJsEnvironment (`function { ... } -> package`): diff --git a/makes/utils/makeNodeJsLockfile/entrypoint.sh b/makes/utils/makeNodeJsLockfile/entrypoint.sh new file mode 100644 index 00000000..aac00a74 --- /dev/null +++ b/makes/utils/makeNodeJsLockfile/entrypoint.sh @@ -0,0 +1,26 @@ +# shellcheck shell=bash + +function main { + local node_js_version="${1}" + local package_json="${2}" + local package_lock="${3}" + local npm_install_args=( + --audit false + --ignore-scripts true + ) + + case "${node_js_version}" in + 10) npm=__argNode10__/bin/npm ;; + 12) npm=__argNode12__/bin/npm ;; + 14) npm=__argNode14__/bin/npm ;; + 16) npm=__argNode16__/bin/npm ;; + *) critical NodeJs version not supported: "${node_js_version}" ;; + esac \ + && pushd "$(mktemp -d)" \ + && copy "${package_json}" package.json \ + && "${npm}" install "${npm_install_args[@]}" \ + && copy package-lock.json "${package_lock}" \ + && popd || return 1 +} + +main "${@}" diff --git a/makes/utils/makeNodeJsLockfile/main.nix b/makes/utils/makeNodeJsLockfile/main.nix new file mode 100644 index 00000000..9bac1fa2 --- /dev/null +++ b/makes/utils/makeNodeJsLockfile/main.nix @@ -0,0 +1,15 @@ +{ __nixpkgs__ +, makeNodeJsVersion +, makeScript +, ... +}: +makeScript { + entrypoint = ./entrypoint.sh; + name = "make-node-js-lockfile"; + replace = { + __argNode10__ = makeNodeJsVersion "10"; + __argNode12__ = makeNodeJsVersion "12"; + __argNode14__ = makeNodeJsVersion "14"; + __argNode16__ = makeNodeJsVersion "16"; + }; +}