nodejs: fix backward compat for nodejs-package-json

- revert recent changes on nodejs-package-json
- instead introduce nodejs-package-json-v3

This way, existing users won't face breakages in the api
This commit is contained in:
DavHau 2023-11-08 14:18:08 +07:00 committed by mergify[bot]
parent 24e81d5ce8
commit 7cb0fded82
5 changed files with 114 additions and 11 deletions

View File

@ -7,7 +7,7 @@
system = config.deps.stdenv.system;
in {
imports = [
dream2nix.modules.dream2nix.nodejs-package-json
dream2nix.modules.dream2nix.nodejs-package-json-v3
dream2nix.modules.dream2nix.nodejs-granular-v3
];

View File

@ -0,0 +1,73 @@
{
config,
lib,
...
}: let
l = lib // builtins;
cfg = config.nodejs-package-json;
writers = import ../../../pkgs/writers {
inherit lib;
inherit
(config.deps)
bash
coreutils
gawk
path
writeScript
writeScriptBin
;
};
npmArgs = l.concatStringsSep " " (map (arg: "'${arg}'") cfg.npmArgs);
in {
imports = [
./interface.nix
../nodejs-package-lock-v3
];
config = {
deps = {nixpkgs, ...}:
l.mapAttrs (_: l.mkDefault) {
inherit
(nixpkgs)
bash
coreutils
gawk
path
writeScript
writeScriptBin
;
npm = nixpkgs.nodejs.pkgs.npm;
};
lock.fields.package-lock.script =
writers.writePureShellScript
[
config.deps.coreutils
config.deps.npm
]
''
source=${cfg.source}
pushd $TMPDIR
cp -r $source/* ./
chmod -R +w ./
rm -f package-lock.json
npm install --package-lock-only ${npmArgs}
mv package-lock.json $out
popd
'';
lock.invalidationData = {
packageJson = lib.importJSON (config.nodejs-package-json.source + /package.json);
};
nodejs-package-lock-v3 = {
packageLockFile = null;
packageLock = l.mkForce config.lock.content.package-lock;
};
};
}

View File

@ -0,0 +1,31 @@
{
config,
lib,
...
}: let
l = lib // builtins;
t = l.types;
in {
imports = [
../nodejs-package-lock-v3/interface.nix
];
options.nodejs-package-json = l.mapAttrs (_: l.mkOption) {
source = {
type = t.either t.path t.package;
description = "Source of the package";
default = config.mkDerivation.src;
defaultText = "config.mkDerivation.src";
};
npmArgs = {
type = t.listOf t.str;
description = "extra arguments to pass to 'npm install'";
default = [];
};
};
options.deps = l.mapAttrs (_: l.mkOption) {
npm = {
type = t.package;
description = "The npm package used to build the lock file";
};
};
}

View File

@ -6,6 +6,11 @@
l = lib // builtins;
cfg = config.nodejs-package-json;
npm =
if l.versionOlder config.deps.npm.version "9"
then config.deps.npm
else throw "The version of config.deps.npm must be < 9";
writers = import ../../../pkgs/writers {
inherit lib;
inherit
@ -23,7 +28,7 @@
in {
imports = [
./interface.nix
../nodejs-package-lock-v3
../nodejs-package-lock
];
config = {
deps = {nixpkgs, ...}:
@ -37,14 +42,13 @@ in {
writeScript
writeScriptBin
;
npm = nixpkgs.nodejs.pkgs.npm;
};
lock.fields.package-lock.script =
writers.writePureShellScript
[
config.deps.coreutils
config.deps.npm
npm
]
''
source=${cfg.source}
@ -61,11 +65,7 @@ in {
popd
'';
lock.invalidationData = {
packageJson = lib.importJSON (config.nodejs-package-json.source + /package.json);
};
nodejs-package-lock-v3 = {
nodejs-package-lock = {
packageLockFile = null;
packageLock = l.mkForce config.lock.content.package-lock;
};

View File

@ -7,14 +7,13 @@
t = l.types;
in {
imports = [
../nodejs-package-lock-v3/interface.nix
../nodejs-package-lock/interface.nix
];
options.nodejs-package-json = l.mapAttrs (_: l.mkOption) {
source = {
type = t.either t.path t.package;
description = "Source of the package";
default = config.mkDerivation.src;
defaultText = "config.mkDerivation.src";
};
npmArgs = {
type = t.listOf t.str;