-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-npm.nix
41 lines (39 loc) · 1.81 KB
/
init-npm.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
pkgs,
lib,
config,
}: let
cfg = config.snow-blower.languages.javascript;
nodeModulesPath = "${lib.optionalString (cfg.directory != config.snow-blower.paths.root) ''"${cfg.directory}/"''}node_modules";
in
pkgs.writeShellScript "init-npm.sh" ''
function _snow_blower-npm-install()
{
# Avoid running "npm install" for every shell.
# Only run it when the "package-lock.json" file or nodejs version has changed.
# We do this by storing the nodejs version and a hash of "package-lock.json" in node_modules.
local ACTUAL_NPM_CHECKSUM="${cfg.npm.package.version}:$(${pkgs.nix}/bin/nix-hash --type sha256 ${lib.optionalString (cfg.directory != config.snow-blower.paths.root) ''"${cfg.directory}/"''}package-lock.json)"
local NPM_CHECKSUM_FILE="${nodeModulesPath}/package-lock.json.checksum"
if [ -f "$NPM_CHECKSUM_FILE" ]
then
read -r EXPECTED_NPM_CHECKSUM < "$NPM_CHECKSUM_FILE"
else
EXPECTED_NPM_CHECKSUM=""
fi
if [ "$ACTUAL_NPM_CHECKSUM" != "$EXPECTED_NPM_CHECKSUM" ]
then
if ${cfg.npm.package}/bin/npm install ${lib.optionalString (cfg.directory != config.snow-blower.paths.root) "--prefix ${cfg.directory}"}
then
echo "$ACTUAL_NPM_CHECKSUM" > "$NPM_CHECKSUM_FILE"
else
echo "Install failed. Run 'npm install' manually."
fi
fi
}
if [ ! -f ${lib.optionalString (cfg.directory != config.snow-blower.paths.root) ''"${cfg.directory}/"''}package.json ]
then
echo "No package.json found${lib.optionalString (cfg.directory != config.snow-blower.paths.root) ''"in ${cfg.directory}"''}. Run '${lib.optionalString (cfg.directory != config.snow-blower.paths.root) ''"cd ${cfg.directory}/ && "''}npm init' to create one." >&2
else
_snow_blower-npm-install
fi
''