-
Notifications
You must be signed in to change notification settings - Fork 0
/
init-bun.nix
41 lines (39 loc) · 1.79 KB
/
init-bun.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-bun.sh" ''
function _snow_blower-bun-install()
{
# Avoid running "bun install --yarn" for every shell.
# Only run it when the "yarn.lock" file or nodejs version has changed.
# We do this by storing the nodejs version and a hash of "yarn.lock" in node_modules.
local ACTUAL_BUN_CHECKSUM="${cfg.bun.package.version}:$(${pkgs.nix}/bin/nix-hash --type sha256 ${lib.optionalString (cfg.directory != config.snow-blower.paths.root) ''"${cfg.directory}/"''}yarn.lock)"
local BUN_CHECKSUM_FILE="${nodeModulesPath}/yarn.lock.checksum"
if [ -f "$BUN_CHECKSUM_FILE" ]
then
read -r EXPECTED_BUN_CHECKSUM < "$BUN_CHECKSUM_FILE"
else
EXPECTED_BUN_CHECKSUM=""
fi
if [ "$ACTUAL_BUN_CHECKSUM" != "$EXPECTED_BUN_CHECKSUM" ]
then
if ${cfg.bun.package}/bin/bun install --yarn ${lib.optionalString (cfg.directory != config.snow-blower.paths.root) "--cwd ${cfg.directory}"}
then
echo "$ACTUAL_BUN_CHECKSUM" > "$BUN_CHECKSUM_FILE"
else
echo "Install failed. Run 'bun install --yarn' 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}/ && "''}bun init' to create one." >&2
else
_snow_blower-bun-install
fi
''