├── .envrc ├── .github ├── dependabot.yml └── workflows │ ├── niv-update.yml │ └── test.yml ├── .gitignore ├── API.md ├── LICENSE ├── README.md ├── default.nix ├── internal-v1.nix ├── internal-v2.nix ├── nix ├── default.nix ├── sources.json └── sources.nix ├── set-node-path.sh ├── shell.nix ├── test.sh └── tests ├── default.nix ├── lib.nix ├── tests-v1 ├── build-tests.nix ├── build.nix ├── default.nix ├── examples-projects │ ├── bin-project │ │ ├── index.js │ │ ├── package-lock.json │ │ ├── package.json │ │ └── shell.nix │ ├── bin-wrapped-dep │ │ ├── package-lock.json │ │ ├── package.json │ │ └── shell.nix │ ├── github-dependency-branch │ │ ├── default.nix │ │ ├── package-lock.json │ │ └── package.json │ ├── github-dependency │ │ ├── default.nix │ │ ├── package-lock.json │ │ └── package.json │ ├── github-dev-dependency │ │ ├── package-lock.json │ │ └── package.json │ ├── native-extensions │ │ ├── package-lock.json │ │ └── package.json │ ├── nested-dependencies │ │ ├── package-lock.json │ │ └── package.json │ ├── no-dependencies │ │ ├── package-lock.json │ │ └── package.json │ ├── no-version │ │ ├── package-lock.json │ │ └── package.json │ ├── nodejs-version-shell │ │ ├── package-lock.json │ │ ├── package.json │ │ └── shell.nix │ ├── single-dependency │ │ ├── package-lock.json │ │ ├── package.json │ │ └── shell.nix │ ├── source-patching │ │ ├── package-lock.json │ │ ├── package.json │ │ └── shell.nix │ ├── unsanitized-package-name │ │ ├── default.nix │ │ ├── package-lock.json │ │ ├── package.json │ │ └── shell.nix │ ├── url-as-version │ │ ├── package-lock.json │ │ └── package.json │ └── webpack-cli-project │ │ ├── package-lock.json │ │ ├── package.json │ │ ├── src │ │ └── index.js │ │ └── webpack.config.js ├── integration-tests │ ├── default.nix │ └── leftpad-shell.sh ├── make-github-source.nix ├── make-source-urls.nix ├── make-source.nix ├── node-modules.nix ├── parse-github-ref.nix ├── patch-lockfile.nix ├── patch-packagefile.nix ├── read-lockfile │ ├── default.nix │ ├── empty.json │ └── simple.json ├── shell.nix └── source-hash-func.nix └── tests-v2 ├── build-tests.nix ├── build.nix ├── default.nix ├── examples-projects ├── bin-project │ ├── index.js │ ├── package-lock.json │ ├── package.json │ └── shell.nix ├── bin-wrapped-dep │ ├── package-lock.json │ ├── package.json │ └── shell.nix ├── bundled-dep-require-patch-shebang │ ├── default.nix │ ├── package-lock.json │ └── package.json ├── github-dependency-branch │ ├── default.nix │ ├── package-lock.json │ └── package.json ├── github-dependency │ ├── default.nix │ ├── package-lock.json │ └── package.json ├── github-dev-dependency │ ├── package-lock.json │ └── package.json ├── in-bundle-dependency │ ├── default.nix │ ├── package-lock.json │ └── package.json ├── native-extensions │ ├── package-lock.json │ └── package.json ├── nested-dependencies │ ├── package-lock.json │ └── package.json ├── no-dependencies │ ├── package-lock.json │ └── package.json ├── no-version │ ├── package-lock.json │ └── package.json ├── nodejs-version-shell │ ├── package-lock.json │ ├── package.json │ └── shell.nix ├── single-dependency │ ├── package-lock.json │ ├── package.json │ └── shell.nix ├── source-patching │ ├── package-lock.json │ ├── package.json │ └── shell.nix ├── unsanitized-package-name │ ├── default.nix │ ├── package-lock.json │ ├── package.json │ └── shell.nix ├── url-as-version │ ├── package-lock.json │ └── package.json └── webpack-cli-project │ ├── package-lock.json │ ├── package.json │ ├── src │ └── index.js │ └── webpack.config.js ├── integration-tests ├── default.nix └── leftpad-shell.sh ├── make-github-source.nix ├── make-url-source.nix ├── node-modules.nix ├── parse-github-ref.nix ├── patch-package.nix ├── patch-packagefile.nix ├── patch-v2-package.nix ├── read-lockfile ├── default.nix ├── empty.json └── simple.json ├── shell.nix └── source-hash-func.nix /.envrc: -------------------------------------------------------------------------------- 1 | use_nix 2 | 3 | eval "$shellHook" 4 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | 4 | - package-ecosystem: github-actions 5 | directory: "/" 6 | schedule: 7 | interval: daily 8 | time: '00:00' 9 | timezone: UTC 10 | open-pull-requests-limit: 10 11 | commit-message: 12 | prefix: "chore" 13 | include: "scope" 14 | -------------------------------------------------------------------------------- /.github/workflows/niv-update.yml: -------------------------------------------------------------------------------- 1 | name: Automated niv-managed dependency updates 2 | on: 3 | schedule: 4 | # * is a special character in YAML so you have to quote this string 5 | # run this every day at 4:00am 6 | - cron: '0 4 * * *' 7 | jobs: 8 | niv-updater: 9 | name: 'Create PRs for niv-managed dependencies' 10 | runs-on: ubuntu-latest 11 | steps: 12 | # notice there is no checkout step 13 | - name: niv-updater-action 14 | uses: knl/niv-updater-action@v11 15 | with: 16 | # https://github.com/knl/niv-updater-action/issues/45 17 | niv_version: 'af958e8057f345ee1aca714c1247ef3ba1c15f5e' 18 | env: 19 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 20 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: "Tests" 2 | on: 3 | pull_request: 4 | push: 5 | jobs: 6 | tests: 7 | runs-on: ubuntu-20.04 8 | steps: 9 | - uses: actions/checkout@v3 10 | - uses: cachix/install-nix-action@v18 11 | with: 12 | nix_path: nixpkgs=channel:nixpkgs-unstable 13 | install_url: https://releases.nixos.org/nix/nix-2.3.16/install 14 | - uses: cachix/cachix-action@v12 15 | with: 16 | name: npmlock2nix 17 | signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}' 18 | - run: nix-shell --run "nixpkgs-fmt --check ." 19 | - name: Try latest stable Nix release 20 | run: nix-shell -I nixpkgs=nix -p nix --run ./test.sh 21 | - name: Try tests with nix 2.3 22 | run: nix-shell -I nixpkgs=nix -p nix_2_3 --run ./test.sh 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .direnv 3 | result* 4 | .pre-commit-config.yaml 5 | -------------------------------------------------------------------------------- /API.md: -------------------------------------------------------------------------------- 1 | ## API Documentation 2 | 3 | The sections below describe the public API of _npmlock2nix_. 4 | 5 | Npmlock2nix is currently in a transition phase. The npmlockfile format evolved with the npm v7 release so does the npmlock2nix parser. If you're trying to parse a lockfile v2, generated via npm >= 7, you'll have to use the `v2` top-level prefix. 6 | 7 | IE. you'll have to call the following functions with these prefixes: 8 | 9 | ```nix 10 | # V1 lockfiles 11 | 12 | npmlock2nix.v1.build { 13 | … 14 | } 15 | 16 | npmlock2nix.v1.node_modules { 17 | … 18 | } 19 | 20 | npmlock2nix.v1.shell { 21 | … 22 | } 23 | 24 | # V2 lockfiles 25 | 26 | npmlock2nix.v2.build { 27 | … 28 | } 29 | 30 | npmlock2nix.v2.node_modules { 31 | … 32 | } 33 | 34 | npmlock2nix.v2.shell { 35 | … 36 | } 37 | ``` 38 | 39 | Aside from these `v1` and `v2` prefixes, the underlying public API stays the same. 40 | 41 | If you forget to specify a `v1`/`v2` prefix, a warning containing some migration instructions will be emitted and npmlock2nix will fall back on using the lockfiles `v1` implementation. 42 | 43 | ## Functions 44 | 45 | ### node_modules 46 | 47 | The `node_modules` function parses `package.json` and `package-lock.json` and generates a derivation containing a populated `node_modules` folder equivalent to the result of running `npm install`. 48 | #### Arguments 49 | The `node_modules` function takes an attribute set with the following attributes: 50 | 51 | - **src** *(mandatory)*: Path to the source containing `package.json` and `package-lock.json` 52 | - **packageJson** *(default `src+"/package.json"`)*: Path to `package.json` 53 | - **packageLockJson** *(default `src+"/package-lock.json"`)*: Path to `package-lock.json` 54 | - **nodejs** *(default `nixpkgs.nodejs`, which is the Active LTS version)*: Node.js derivation to use 55 | - **preInstallLinks** *(default `{}`)*: Map of symlinks to create inside npm dependencies in the `node_modules` output (See [Concepts](#concepts) for details). 56 | - **githubSourceHashMap** *(default `{}`)*: Dependency hashes for evaluation in restricted mode (See [Concepts](#concepts) for details). 57 | - **sourceOverrides** *(default `{}`)*: Derivation attributes to apply to sources, allowing patching (See the [source derivation overrides](#source-derivation-overrides) concept for details) 58 | 59 | #### Notes 60 | - You may provide additional arguments accepted by `mkDerivation` all of which are going to be passed on. 61 | - Sometimes the installation behavior of npm packages needs to be altered by setting environment variables. You may set environment variables by simply adding them to the attribute set: `FOO_HOME = ${pkgs.libfoo.dev}`. 62 | 63 | 64 | --- 65 | 66 | ### shell 67 | The `shell` function creates a nix-shell environment with the `node_modules` folder for the given npm project provided in the local directory as either copy or symlink (as determined by `node_modules_mode`). 68 | 69 | #### Arguments 70 | The `shell` function takes an attribute set with the following attributes: 71 | 72 | - **src** *(mandatory)*: Path to the source containing `package.json` and `package-lock.json` 73 | - **node_modules_mode** *(default `"symlink"`)*: Determines how the `node_modules` should be provided (See [Concepts](#concepts) for details). 74 | - **node_modules_attrs** *(default `{}`)*: Overrides that will be passed to the `node_modules` function (See [Concepts](#concepts) for details). 75 | 76 | 77 | #### Notes 78 | - You may provide additional arguments accepted by `mkDerivation` all of which are going to be passed on. 79 | 80 | --- 81 | 82 | ### build 83 | The `build` function creates a derivation for an arbitrary npm package by letting the user specify how to build and install it. 84 | 85 | #### Arguments 86 | The `build` function takes an attribute set with the following attributes: 87 | 88 | - **src** *(mandatory)*: Path to the source containing `package.json` and `package-lock.json`. 89 | - **installPhase** *(mandatory)*: Commands to install the package 90 | - **buildCommands** *(default `["npm run build"]`)*: List of commands to build the package. 91 | - **node_modules_attrs** *(default `{}`)*: Overrides that will be passed to the `node_modules` function (See [Concepts](#concepts) for details). 92 | - **node_modules_mode** *(default `"symlink"`)*: Determines how the `node_modules` should be provided (See [Concepts](#concepts) for details). 93 | 94 | #### Notes 95 | - You may provide additional arguments accepted by `mkDerivation` all of which are going to be passed on. 96 | 97 | ## Concepts 98 | 99 | ### githubSourceHashMap 100 | When _npmlock2nix_ is used in restricted evaluation mode (hydra for example), `node_modules` needs to be provided with the revision and sha256 of all GitHub dependencies via `githubSourceHashMap`: 101 | 102 | ```nix 103 | npmlock2nix.v1.node_modules { 104 | src = ./.; 105 | githubSourceHashMap = { 106 | tmcw.leftpad.db1442a0556c2b133627ffebf455a78a1ced64b9 = "1zyy1nxbby4wcl30rc8fsis1c3f7nafavnwd3qi4bg0x00gxjdnh"; 107 | }; 108 | } 109 | ``` 110 | 111 | Please refer to [github-dependency](https://github.com/nix-community/npmlock2nix/blob/master/tests/tests-v2/examples-projects/github-dependency) for a fully working example. 112 | 113 | ### preInstallLinks 114 | 115 | *NOTE* In the `v2` API the `preInstallLinks` attribute was removed. Use source derivation overrides `sourceOverrides`. 116 | 117 | Sometimes you may want to augment or populate vendored dependencies in npm packages because they either aren't working or they cannot be fetched during the build phase. This can be achieved by passing a `preInstallLinks` attribute set to `node_modules`. 118 | 119 | If you wanted to patch the [cwebp-bin](https://www.npmjs.com/package/cwebp-bin) package to contain the `cwebp` binary from nixpkgs under `vendor/cwebp-bin` you would do so as follows: 120 | 121 | ```nix 122 | npmlock2nix.v1.node_modules { 123 | src = ./.; 124 | preInstallLinks = { 125 | "cwebp-bin" = { 126 | "vendor/cweb-bin" = "${pkgs.libwebp}/bin/cwebp" 127 | }; 128 | }; 129 | } 130 | ``` 131 | 132 | Please refer to [bin-wrapped-dep](https://github.com/tweag/npmlock2nix/blob/master/tests/examples-projects/bin-wrapped-dep/shell.nix) for a fully working example. 133 | 134 | 135 | ### node_modules_mode 136 | 137 | _npmlock2nix_ can provide the `node_modules` folder to builds and development environment environments in two different ways as designated by `node_modules_mode`: 138 | 139 | - `copy`: The `node_modules/` folder is copied from the nix store 140 | - `symlink` The `node_modules/` folder is symlinked from the nix store 141 | 142 | The first can be useful if you are also using `npm` to interactively update or modify your `node_modules` alongside nix based builds. 143 | 144 | **Note**: If you are entering a shell environment using `npmlock2nix.shell` and there is an existing `node_modules/` _directory_ (instead of a symlink), `npmlock2nix` will print a warning but will not touch this directory. Symlinks on the other hand will be updated. 145 | 146 | ### node_modules_attrs 147 | 148 | When you actually want to describe a shell environment or a build, but you need to pass attributes to `node_modules` you can do so by passing them via `node_modules_attrs` in both `build` and `shell`: 149 | 150 | ```nix 151 | npmlock2nix.v1.build { 152 | src = ./.; 153 | node_modules_attrs = { 154 | buildInputs = [ pkgs.zlib ]; 155 | }; 156 | } 157 | ``` 158 | 159 | ### Source derivation overrides 160 | 161 | `node_modules` takes a `sourceOverrides` argument, which allows you to modify the source derivations of individual npm packages you depend on, mainly useful for adding Nix-specific fixes to packages. This could be used for patching interpreter or paths, or to replace vendored binaries with ones provided by Nix. 162 | 163 | The `sourceOverrides` argument expects an attribute set mapping npm package names to a function describing the modifications of that package. Each function receives an attribute set as a first argument, containing either a `version` attribute if the version is known, or a `github = { org, repo, rev, ref }` attribute if the package is fetched from GitHub. These values can be used to have different overrides depending on the version. The function receives another argument which is the derivation of the fetched source, which can be modified using `.overrideAttrs`. The fetched source mainly runs the [patch phase](https://nixos.org/manual/nixpkgs/stable/#ssec-patch-phase), so of particular interest are the `patches` and `postPatch` attributes, in which `patchShebangs` can be called. Note that `patchShebangs` can only patch shebangs to binaries accessible in the derivation, which you can extend with `buildInputs`. For convenience, the correct version of `nodejs` is always included in `buildInputs`. 164 | 165 | ```nix 166 | npmlock2nix.v1.node_modules { 167 | sourceOverrides = { 168 | # sourceInfo either contains: 169 | # - A version attribute 170 | # - A github = { org, repo, rev, ref } attribute for GitHub sources 171 | package-name = sourceInfo: drv: drv.overrideAttrs (old: { 172 | buildInputs = [ somePackage ]; 173 | patches = [ somePatch ]; 174 | postPatch = '' 175 | some script 176 | ''; 177 | # ... 178 | }); 179 | 180 | # Example 181 | node-pre-gyp = sourceInfo: drv: drv.overrideAttrs (old: { 182 | postPatch = "patchShebangs bin"; 183 | }); 184 | 185 | }; 186 | } 187 | ``` 188 | 189 | *Note* In the `v2` API the following is the case: 190 | 191 | Npm packages binaries interpreter are automatically patched with environment shebangs when an entry exist for the npm package. 192 | 193 | To replace `preInstallLinks` functionality. Symlinked vendored binaries are compiled to be added on node modules installation using npm preinstall script, this is required since npm strip symlinks from package archive. 194 | 195 | The `sourceOverrides` argument expects an attribute set mapping npm package names to a function describing the modifications of that package. Each function receives an attribute set as a first argument, containing either a `version` attribute if the version is known, or a `github = { org, repo, rev, ref }` attribute if the package is fetched from GitHub. These values can be used to have different overrides depending on the version. The function receives another argument which is the derivation of the fetched source, which can be modified using `.overrideAttrs`. The fetched source mainly runs the [patch phase](https://nixos.org/manual/nixpkgs/stable/#ssec-patch-phase), so of particular interest are the `patches` and `postPatch` attributes, in which `patchShebangs` can be called. 196 | 197 | `sourceOverrides` comes with default override mechanism like patching all npm packages binaries dependencies with `buildRequirePatchShebangs` or patching individual npm package binaries with `packageRequirePatchShebangs`. 198 | 199 | ```nix 200 | npmlock2nix.v2.node_modules { 201 | sourceOverrides = { 202 | # sourceInfo either contains: 203 | # - A version attribute 204 | # - A github = { org, repo, rev, ref } attribute for GitHub sources 205 | package-name = sourceInfo: drv: drv.overrideAttrs (old: { 206 | buildInputs = [ somePackage ]; 207 | patches = [ somePatch ]; 208 | postPatch = '' 209 | mkdir -p vendor 210 | ln -sf ${vendored_binary} ./vendor/vendored_binary 211 | ''; 212 | # ... 213 | }) 214 | 215 | # Patch all npm packages 216 | buildRequirePatchShebangs = true; 217 | 218 | # Patch individual package 219 | node-pre-gyp = npmlock2nix.v2.packageRequirePatchShebangs; 220 | 221 | # Link vendored binaries 222 | package-name = sourceInfo: drv: drv.overrideAttrs (old: { 223 | postPatch = '' 224 | mkdir -p vendor 225 | ln -sf ${vendored_binary} ./vendor/vendored_binary 226 | ''; 227 | }); 228 | 229 | }; 230 | } 231 | ``` 232 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | [![License][license-shield]][license-url] 4 | [![Contributors][contributors-shield]][contributors-url] 5 | [![Issues][issues-shield]][issues-url] 6 | [![PRs][pr-shield]][pr-url] 7 | [![Tests][test-shield]][test-url] 8 | [![Matrix][matrix-shield]][matrix-url] 9 | 10 | 11 |
12 |

13 |

npmlock2nix

14 |

15 | Simple and unit tested solution to nixify npm based packages. 16 |

17 |

18 | 19 | ## About 20 | 21 | _npmlock2nix_ is a Nix based library that parses the `package.json` and `package-lock.json` files in order to provide different outputs: 22 | 23 | 1. A `shell` environment 24 | 1. A `node_modules` derivation 25 | 1. A custom `build` derivation 26 | 27 | ### Features 28 | 29 | - No auto-generated code :heavy_check_mark: 30 | - Works in restricted evaluation :heavy_check_mark: 31 | - GitHub dependencies :heavy_check_mark: 32 | - Unit Tests :heavy_check_mark: 33 | - Integration Tests :heavy_check_mark: 34 | 35 | ## Getting Started 36 | 37 | Since `npmlock2nix` is written entirely in Nix, there aren't any additional prerequisites, it just needs to be imported into your project. 38 | 39 | ### Installation 40 | 41 | The preferred way to provide _npmlock2nix_ to your project is via [niv][niv-url]: 42 | 43 | ```shell 44 | $ niv add nix-community/npmlock2nix 45 | ``` 46 | 47 | Assuming you are also tracking nixpkgs via niv, you can then provide _npmlock2nix_ to your project as a [nixpkgs overlay][overlay-link] 48 | 49 | ```nix 50 | # nix/default.nix 51 | let 52 | sources = import ./sources.nix; 53 | in 54 | import sources.nixpkgs { 55 | overlays = [ 56 | (self: super: { 57 | npmlock2nix = pkgs.callPackage sources.npmlock2nix { }; 58 | }) 59 | ]; 60 | } 61 | ``` 62 | 63 | Assuming the setup above, you can import `nix/default.nix` which will yield a nixpkgs set containing _npmlock2nix_. 64 | 65 | ## Usage 66 | 67 | The following sections outline the main use-case scenarios of _npmlock2nix_. 68 | 69 | **Note**: All examples only reflect the most basic scenarios and mandatory arguments. For more details please refer to the [API documentation][api-url]. 70 | 71 | **Note**: All code snippets provided below assume that _npmlock2nix_ has been imported and is inn scope and that there are valid `package.json` and `package-lock.json` files in the project root. 72 | 73 | ### Providing A Shell 74 | 75 | ```nix 76 | npmlock2nix.shell { 77 | src = ./.; 78 | } 79 | ``` 80 | The `shell` function creates an environment with the `node_modules` installed that can be used for development purposes. 81 | 82 | Please refer to the [API documentation][api-url] for additional information on `shell`. 83 | 84 | 85 | ### Building `node_modules` 86 | 87 | ```nix 88 | npmlock2nix.node_modules { 89 | src = ./.; 90 | } 91 | ``` 92 | The `node_modules` function creates a derivation containing the equivalent of running `npm install` in an impure environment. 93 | 94 | Please refer to the [API documentation][api-url] for additional information on `node_modules`. 95 | 96 | 97 | ### Building A Project 98 | 99 | ```nix 100 | npmlock2nix.build { 101 | src = ./.; 102 | installPhase = "cp -r dist $out"; 103 | buildCommands = [ "npm run build" ]; 104 | } 105 | ``` 106 | The `build` function can be used to package arbitrary npm based projects. In order for this to work, 107 | _npmlock2nix_ must be told how to build the project (`buildCommands`) and how to install it (`installPhase`). 108 | 109 | Please refer to the [API documentation][api-url] for additional information on `build`. 110 | 111 | ## Contributing 112 | 113 | Contributions to this project are welcome in the form of GitHub Issues or PRs. Please consider the following before creating PRs: 114 | 115 | - This project uses nixpkgs-fmt for formatting the Nix code. You can use `nix-shell --run "nixpkgs-fmt ."` to format everything. 116 | - If you are planning to make any considerable changes, you should first present your plans in a GitHub issue so it can be discussed 117 | - _npmlock2nix_ is developed with a strong emphasis on testing. Please consider providing tests along with your contributions and don't hesitate to ask for support. 118 | 119 | ## Development 120 | 121 | When working on _npmlock2nix_ it's highly recommended to use [direnv][direnv-url] and the project's `shell.nix` which provides: 122 | 123 | - A commit hook for code formatting via [nix-pre-commit-hooks][nix-pre-commit-hooks-url]. 124 | - A `test-runner` script that watches the source tree and runs the unit tests on changes. 125 | 126 | The integration tests can be executed via `nix-build -A tests.integration-tests`. 127 | 128 | ## License 129 | 130 | Distributed under the Apache 2.0 License. See [license][license-url] for more details 131 | 132 | ## Acknowledgements 133 | 134 | - [nixpkgs-fmt][nixpkgs-fmt-url] 135 | - [direnv][direnv-url] 136 | - [niv][niv-url] 137 | - [nix-pre-commit-hooks][nix-pre-commit-hooks-url] 138 | - [entr][entr-url] 139 | - [smoke][smoke-url] 140 | 141 | 142 | 143 | 144 | 145 | [contributors-shield]: https://img.shields.io/github/contributors/othneildrew/Best-README-Template.svg?style=for-the-badge 146 | [contributors-url]: https://github.com/othneildrew/Best-README-Template/graphs/contributors 147 | [issues-shield]: https://img.shields.io/github/issues/Tweag/npmlock2nix.svg?style=for-the-badge 148 | [issues-url]: https://github.com/Tweag/npmlock2nix/issues 149 | [license-shield]: https://img.shields.io/github/license/Tweag/npmlock2nix.svg?style=for-the-badge 150 | [license-url]: https://github.com/Tweag/npmlock2nix/blob/master/LICENSE 151 | [test-shield]: https://img.shields.io/github/workflow/status/Tweag/npmlock2nix/Tests/master?style=for-the-badge 152 | [test-url]: https://github.com/Tweag/npmlock2nix/actions 153 | [pr-shield]: https://img.shields.io/github/issues-pr/Tweag/npmlock2nix.svg?style=for-the-badge 154 | [pr-url]: https://github.com/Tweag/npmlock2nix/pulls 155 | [matrix-shield]: https://img.shields.io/matrix/npmlock2nix:nixos.dev.svg?server_fqdn=matrix.nixos.dev&style=for-the-badge 156 | [matrix-url]: https://matrix.to/#/#npmlock2nix:nixos.dev 157 | 158 | 159 | 160 | [niv-url]: https://github.com/nmattia/niv 161 | [overlay-link]: https://nixos.org/manual/nixpkgs/stable/#chap-overlays 162 | [api-url]: ./API.md 163 | [direnv-url]: https://direnv.net/ 164 | [nix-pre-commit-hooks-url]: https://github.com/cachix/pre-commit-hooks.nix 165 | [nixpkgs-fmt-url]: https://github.com/nix-community/nixpkgs-fmt 166 | [entr-url]: https://github.com/clibs/entr 167 | [smoke-url]: https://github.com/SamirTalwar/Smoke 168 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ./nix { }, lib ? pkgs.lib }: 2 | let 3 | v1_internal = pkgs.callPackage ./internal-v1.nix { }; 4 | v2_internal = pkgs.callPackage ./internal-v2.nix { }; 5 | separatePublicAndInternalAPI = api: extraAttributes: { 6 | inherit (api) shell build node_modules; 7 | 8 | # *** WARNING **** 9 | # using any of the functions exposed by `internal` is not supported. That 10 | # being said, hiding them would only lead to copy&paste and it is also useful 11 | # for testing internal building blocks. 12 | internal = lib.warn "[npmlock2nix] You are using the unsupported internal API." ( 13 | api 14 | ); 15 | } // (lib.listToAttrs (map (name: lib.nameValuePair name api.${name}) extraAttributes)); 16 | v1 = separatePublicAndInternalAPI v1_internal [ ]; 17 | v2 = separatePublicAndInternalAPI v2_internal [ "packageRequirePatchShebangs" ]; 18 | in 19 | { 20 | inherit v1; 21 | v2 = lib.mapAttrs (_: lib.warn "[npmlock2nix] You are using the new v2 beta api. The interface isn't stable yet. Please report any issues at https://github.com/nix-community/npmlock2nix/issues") v2; 22 | tests = pkgs.callPackage ./tests { }; 23 | } // (lib.mapAttrs 24 | (lib.warn "[npmlock2nix] You are using the unversion prefix for builders. This is fine for now. In the future we will move to a versioned interface (old versions remain as they are). The currently used functions are availabe as `npmlock2nix.v1` for example `npmlock2nix.v1.build`.") 25 | v1) 26 | -------------------------------------------------------------------------------- /internal-v2.nix: -------------------------------------------------------------------------------- 1 | { nodejs-16_x, jq, openssl, coreutils, stdenv, mkShell, lib, fetchurl, writeText, writeShellScript, runCommand, fetchFromGitHub }: 2 | rec { 3 | default_nodejs = nodejs-16_x; 4 | 5 | ## helper functions that allow users to define common source override mechanism 6 | 7 | # no-op function that serves as a marker in the `sourceOverrides` 8 | # code. If an override is set the source is passed through the 9 | # entire override mechanism which does, as a side effect, patch the 10 | # shebangs in the source tarball. 11 | packageRequirePatchShebangs = sourceInfo: drv: drv; 12 | 13 | # builtins.fetchGit wrapper that ensures compatibility with Nix 2.3 and Nix 2.4 14 | # Type: Attrset -> Path 15 | fetchGitWrapped = 16 | let 17 | is24OrNewer = lib.versionAtLeast builtins.nixVersion "2.4"; 18 | in 19 | if is24OrNewer then 20 | # remove the now unsupported / insufficient `ref` paramter 21 | args: builtins.fetchGit (builtins.removeAttrs args [ "ref" ]) 22 | else 23 | # for 2.3 (and older?) we remove the unsupported `allRefs` parameter 24 | args: builtins.fetchGit (builtins.removeAttrs args [ "allRefs" ]) 25 | ; 26 | 27 | # Description: Custom throw function that ensures our error messages have a common prefix. 28 | # Type: String -> Throw 29 | throw = str: builtins.throw "[npmlock2nix] ${str}"; 30 | 31 | # Description: Checks if a string looks like a valid github reference 32 | # Type: String -> Boolean 33 | isGitHubRef = str: 34 | let 35 | parts = builtins.split "[:#/@]" str; 36 | partsLen = builtins.length parts; 37 | in 38 | if partsLen == 7 || partsLen == 9 || partsLen == 15 39 | then 40 | ( 41 | ((builtins.elemAt parts 0) == "github") || 42 | ((builtins.elemAt parts 2) == "github.com") || 43 | ((builtins.elemAt parts 2) == "github") || 44 | ((builtins.elemAt parts 8) == "github.com") 45 | ) 46 | else false; 47 | 48 | # Description: Checks if a string looks like a valid github 49 | # reference who do not have a rev. You shouldn't find any of those 50 | # in a "resolved" field. It's however possible to find them in a 51 | # "dependencies" part of a package. 52 | # Type: String -> Boolean 53 | isGitHubRefWithoutRev = str: 54 | let 55 | parts = builtins.split "[:#/@]" str; 56 | partsLen = builtins.length parts; 57 | in 58 | partsLen == 5 && builtins.elemAt parts 0 == "github"; 59 | 60 | # Description: Takes a string of the format 61 | # "git+ssh://git@github.com/owner/repo.git#revision", 62 | # "git@github.com/owner/repo.git#revision" or 63 | # github:owner/repo#revision and returns an attribute set. 64 | # Type: String -> { org, repo, rev } 65 | parseGitHubRef = str: 66 | let 67 | parts = builtins.split "[:#/]" str; 68 | # Parse a string of the format "git+ssh://git@github.com/owner/repo.git#revision" 69 | ghGitSshFormatParser = str: 70 | let 71 | repoWithGitSuffix = builtins.elemAt parts 10; 72 | in 73 | { 74 | inherit parts; 75 | org = builtins.elemAt parts 8; 76 | repo = builtins.substring 0 ((builtins.stringLength repoWithGitSuffix) - 4) repoWithGitSuffix; 77 | rev = builtins.elemAt parts 12; 78 | }; 79 | # Parse a string of the format github:owner/repo#revision 80 | ghShortnameParser = str: { 81 | inherit parts; 82 | org = builtins.elemAt parts 2; 83 | repo = builtins.elemAt parts 4; 84 | rev = builtins.elemAt parts 6; 85 | }; 86 | # Parse a string of the format owner@github:owner/repo#revision 87 | ghGitRefParser = str: { 88 | inherit parts; 89 | org = builtins.elemAt parts 2; 90 | repo = builtins.elemAt parts 4; 91 | rev = builtins.elemAt parts 6; 92 | }; 93 | partLen = builtins.length parts; 94 | in 95 | assert !((partLen == 13) || (partLen == 7)) -> 96 | throw "failed to parse GitHub reference `${str}`. Expected a string of format `git+ssh://git@github.org/owner/repo.git#revision or `github:owner/repo#revision`"; 97 | if (partLen == 13) 98 | then ghGitSshFormatParser str 99 | else 100 | if (builtins.elemAt parts 0) == "github" 101 | then ghShortnameParser str 102 | else ghGitRefParser str; 103 | 104 | # Description: Takes an attribute set describing a git dependency and returns 105 | # a .tgz of the repository as store path. If the attribute hash contains a 106 | # hash attribute it will provide the value to `fetchFromGitHub` which will 107 | # also work in restricted evaluation. 108 | # Type: { name :: String , org :: String, repo :: String, rev :: String, ref :: String, hash :: String, sourceOptions :: Set } 109 | # -> drv 110 | buildTgzFromGitHub = { name, org, repo, rev, ref, hash ? null, sourceOptions ? { } }: 111 | let 112 | src = 113 | if hash != null then 114 | fetchFromGitHub 115 | { 116 | owner = org; 117 | inherit repo; 118 | inherit rev; 119 | sha256 = hash; # FIXME: what if sha3? 120 | } else 121 | fetchGitWrapped { 122 | url = "https://github.com/${org}/${repo}"; 123 | inherit rev ref; 124 | allRefs = true; 125 | }; 126 | 127 | sourceInfo = { 128 | github = { inherit org repo rev ref; }; 129 | }; 130 | drv = packTgz sourceOptions.nodejs name ref src; 131 | in 132 | if sourceOptions ? sourceOverrides.${name} 133 | then sourceOptions.sourceOverrides.${name} sourceInfo drv 134 | else drv; 135 | 136 | # Description: Packs a source directory into a .tgz tar archive. If the 137 | # source is an archive, it gets unpacked first. 138 | # Type: Path -> String -> String -> Path -> Path 139 | packTgz = nodejs: pname: version: src: stdenv.mkDerivation ( 140 | let 141 | preInstallLinks = writeShellScript "preInstallLinks" '' 142 | # preinstalled.links is a space separated text file in the 143 | # form of: 144 | # $symlink-target $symlink-location 145 | for link in "$( preinstalled.links || { 181 | cp -p ${preInstallLinks} npm-preinstall-links.sh 182 | jq -r '.scripts.preinstall as $preinstall | .scripts.preinstall = "./npm-preinstall-links.sh;" + $preinstall' \ 183 | package.json > package.json.tmp && mv package.json.tmp package.json 184 | } 185 | } 186 | 187 | function patch_node_package_bin() { 188 | for bin in $(jq -r '.bin | (.[]?, (select(.|type=="string")|.))' package.json); do 189 | if [ -f $bin ]; then 190 | chmod 755 $bin 191 | patchShebangs $bin 192 | fi 193 | done 194 | if [[ -d node_modules ]]; then 195 | # Patching shebangs of the bundled dependencies 196 | patchShebangs node_modules 197 | fi 198 | } 199 | 200 | prepare_links_for_npm_preinstall 201 | patch_node_package_bin 202 | 203 | runHook preInstall 204 | tar -C . -czf $out ./ 205 | echo sha512-$(openssl dgst -sha512 -binary $out | openssl base64 -A) > $hash 206 | runHook postInstall 207 | ''; 208 | } 209 | ); 210 | 211 | # Description: Replaces the `resolved` field of a dependency with a 212 | # prefetched version from the Nix store. Patches specified with sourceOverrides 213 | # will be applied, in which case the `integrity` attribute is set to `null`, 214 | # in order to be recomputer later 215 | # Type: { sourceOverrides :: Fn, nodejs :: Package } -> String -> String -> String -> String -> { resolved :: Path, integrity :: String } 216 | makeUrlSource = { sourceOverrides ? { }, nodejs, ... }: name: version: resolved: integrity: 217 | let 218 | src = fetchurl { 219 | # Npm strips the query strings when opening a "file://.*" name. 220 | # We need to make sure we strip the query string before adding 221 | # the file to the store. 222 | name = builtins.elemAt (lib.splitString "?" (builtins.baseNameOf resolved)) 0; 223 | url = resolved; 224 | hash = integrity; 225 | }; 226 | sourceInfo = { 227 | version = version; 228 | }; 229 | drv = packTgz nodejs name version src; 230 | tgz = ( 231 | if sourceOverrides ? ${name} then 232 | # If we have modification to this source, unpack the tgz, apply the 233 | # patches and repack the tgz 234 | sourceOverrides.${name} sourceInfo drv 235 | else 236 | if sourceOverrides.buildRequirePatchShebangs or false then drv else src 237 | ); 238 | in 239 | { 240 | inherit integrity; 241 | resolved = "file://" + (toString tgz); 242 | } // lib.optionalAttrs (tgz != src) { 243 | # Integrity was tampered with due to the source attributes, so it needs 244 | # to be recalculated, which is done in the node_modules builder 245 | integrity = null; 246 | }; 247 | 248 | 249 | # Description: Parses the lock file as json and returns an attribute set 250 | # Type: Path -> Set 251 | readPackageLikeFile = file: 252 | assert (builtins.typeOf file != "path" && builtins.typeOf file != "string") -> 253 | throw "file ${toString file} must be a path or string"; 254 | let 255 | content = builtins.readFile file; 256 | json = builtins.fromJSON content; 257 | in 258 | assert 259 | builtins.typeOf json != "set" -> 260 | throw "The NPM lockfile must be a valid JSON object"; 261 | # if a lockfile doesn't declare dependencies ensure that we have an empty 262 | # set. This makes the consuming code eaiser. 263 | if json ? dependencies then json else json // { dependencies = { }; }; 264 | 265 | # Description: Patch a lockfile v2 package entry. It'll replace the 266 | # URL stored in the integrity field with nix store path. 267 | # spec :: {version :: String, resolved :: String, integrity :: String }. 268 | # Type: { version :: String, resolved :: String, integrity :: String } 269 | patchPackage = sourceOptions@{ sourceHashFunc, ... }: raw_name: spec: 270 | assert (builtins.typeOf raw_name != "string") -> 271 | throw "Name of dependency ${toString raw_name} must be a string"; 272 | assert !(spec ? resolved || (spec ? inBundle && spec.inBundle == true)) -> 273 | throw "Missing resolved field for dependency ${toString raw_name}"; 274 | assert !(spec ? version) -> 275 | throw "Missing version field for dependency ${toString raw_name}"; 276 | let 277 | name = genericPackageName raw_name; 278 | defaultedIntegrity = if spec ? integrity then spec.integrity else null; 279 | # Relaxing dependencies version bounds: it could be a GitHub 280 | # ref, forcing NPM to checkout the remote repo to get the actual 281 | # version. 282 | # We already pinned everything through the "resolved", we can 283 | # relax those. 284 | patchDependencies = deps: lib.mapAttrs (_n: dep: if isGitHubRef dep || isGitHubRefWithoutRev dep then "*" else dep) deps; 285 | patchedResolved = 286 | if (!isGitHubRef spec.resolved) 287 | then makeUrlSource sourceOptions name spec.version spec.resolved defaultedIntegrity 288 | else 289 | let 290 | ghRef = parseGitHubRef spec.resolved; 291 | ghTgz = buildTgzFromGitHub { 292 | inherit name sourceOptions; 293 | inherit (ghRef) org repo rev; 294 | ref = ghRef.rev; 295 | hash = sourceHashFunc { type = "github"; value = { inherit (ghRef) org repo rev; }; }; 296 | }; 297 | in 298 | { 299 | resolved = "file://" + (toString ghTgz); 300 | integrity = null; 301 | }; 302 | in 303 | (builtins.removeAttrs spec [ "peerDependencies" ]) // 304 | lib.optionalAttrs (spec ? resolved) { 305 | inherit (patchedResolved) resolved integrity; 306 | } // lib.optionalAttrs (spec ? dependencies) { 307 | dependencies = (patchDependencies spec.dependencies); 308 | }; 309 | 310 | genericPackageName = name: 311 | (lib.last (lib.strings.splitString "node_modules/" name)); 312 | 313 | # Description: Takes a parsed lockfile and returns the patched version as an attribute set 314 | # Type: { sourceHashFunc :: Fn } -> parsedLockedFile :: Set -> { result :: Set, integrityUpdates :: List { path, file } } 315 | patchLockfile = sourceOptions: content: 316 | let 317 | contentWithoutDependencies = builtins.removeAttrs content [ "dependencies" ]; 318 | packagesWithoutSelf = lib.filterAttrs (n: v: n != "") content.packages; 319 | topLevelPackage = lib.filterAttrs (n: v: n == "") content.packages; 320 | patchedPackages = lib.mapAttrs (name: patchPackage sourceOptions name) packagesWithoutSelf; 321 | in 322 | assert !(content ? packages) -> 323 | throw "Missing the packages top-level key in your lockfile. Are you sure it is a npm lockfile v2?"; 324 | { 325 | result = contentWithoutDependencies // { 326 | packages = patchedPackages // topLevelPackage; 327 | }; 328 | }; 329 | 330 | # Description: Rewrite all the `github:` references to wildcards. 331 | # Type: Path -> Set 332 | patchPackagefile = sourceOptions: content: 333 | let 334 | patchDep = (name: version: 335 | # If the dependency is of the form github:owner/repo#branch or 336 | # http(s)://..., the package-lock.json contains the specific 337 | # revision that the branch was pointing at at the time of npm install. 338 | # The package.json itself does not contain enough information to resolve a specific dependency, 339 | # because it only contains the branch name. Therefore we cannot substitute with a nix store path. 340 | # If we leave the dependency unchanged, npm will try to resolve it and fail. We therefore substitute with a 341 | # wildcard dependency, which will make npm look at the lockfile. 342 | if ((isGitHubRef version) || (lib.hasPrefix "http" version)) then 343 | "*" 344 | else if version == "latest" then sourceOptions.packagesVersions.${name}.version else version); 345 | dependencies = if (content ? dependencies) then lib.mapAttrs patchDep content.dependencies else { }; 346 | devDependencies = if (content ? devDependencies) then lib.mapAttrs patchDep content.devDependencies else { }; 347 | in 348 | content // { inherit devDependencies dependencies; }; 349 | 350 | # Description: Takes a parsed package file and returns the patched version as file in the Nix store 351 | # Type: { sourceHashFunc :: Fn } -> parsedPackageFile :: Set -> Derivation 352 | patchedPackagefile = sourceOptions: parsedPackageFile: writeText "package.json" 353 | ( 354 | builtins.toJSON (patchPackagefile sourceOptions parsedPackageFile) 355 | ); 356 | 357 | # Description: Takes a Path to a lockfile and returns the patched version as file in the Nix store 358 | # Type: { sourceHashFunc :: Fn } -> parsedLockFile :: Set -> Path 359 | patchedLockfile = sourceOptions: parsedLockFile: 360 | let 361 | patched = patchLockfile sourceOptions parsedLockFile; 362 | in 363 | writeText "package-lock.json" (builtins.toJSON patched.result); 364 | 365 | # Description: Turn a derivation (with name & src attribute) into a directory containing the unpacked sources 366 | # Type: Derivation -> Derivation 367 | nodeSource = nodejs: runCommand "node-sources-${nodejs.version}" 368 | { } '' 369 | tar --no-same-owner --no-same-permissions -xf ${nodejs.src} 370 | mv node-* $out 371 | ''; 372 | 373 | # Description: Creates shell scripts to provide node_modules to the environment supporting 374 | # two different modes: "symlink" and "copy" 375 | # Type: Derivation -> String -> String 376 | add_node_modules_to_cwd = node_modules: mode: 377 | '' 378 | # If node_modules is a managed symlink we can safely remove it and install a new one 379 | ${lib.optionalString (mode == "symlink") '' 380 | if [[ "$(readlink -f node_modules)" == ${builtins.storeDir}* ]]; then 381 | rm -f node_modules 382 | fi 383 | ''} 384 | 385 | if test -e node_modules; then 386 | echo '[npmlock2nix] There is already a `node_modules` directory. Not replacing it.' >&2 387 | exit 1 388 | fi 389 | '' + 390 | ( 391 | if mode == "copy" then '' 392 | cp --no-preserve=mode -r ${node_modules}/node_modules node_modules 393 | chmod -R u+rw node_modules 394 | '' else if mode == "symlink" then '' 395 | ln -s ${node_modules}/node_modules node_modules 396 | '' else throw "node_modules_mode must be either `copy` or `symlink`" 397 | ) + '' 398 | export NODE_PATH="$(pwd)/node_modules:$NODE_PATH" 399 | ''; 400 | 401 | # Description: Extract the attributes that are relevant for building node_modules and use 402 | # them as defaults in case the node_modules_attrs attribute doesn't have 403 | # them. 404 | # Type: Set -> Set 405 | get_node_modules_attrs = { node_modules_attrs ? { }, ... }@attrs: 406 | let 407 | getAttr = name: from: lib.optionalAttrs (builtins.hasAttr name from) { "${name}" = from.${name}; }; 408 | getAttrs = names: from: lib.foldl (a: b: a // (getAttr b from)) { } names; 409 | in 410 | (getAttrs [ "src" "nodejs" ] attrs // node_modules_attrs); 411 | 412 | # Description: Takes a dependency spec and a map of github sources/hashes and returns either the map or 'null' 413 | # Type: Set -> Set -> Set | null 414 | sourceHashFunc = githubSourceHashMap: spec: 415 | if spec.type == "github" then 416 | lib.attrByPath 417 | [ spec.value.org spec.value.repo spec.value.rev ] 418 | ( 419 | lib.traceSeq 420 | "[npmlock2nix] warning: missing attr in githubSourceHashMap: ${spec.value.org}.${spec.value.repo}.${spec.value.rev}" 421 | null 422 | ) 423 | githubSourceHashMap 424 | else 425 | throw "sourceHashFunc: spec.type '${spec.type}' is not supported. Supported types: 'github'"; 426 | 427 | node_modules = 428 | { src 429 | , packageJson ? src + "/package.json" 430 | , packageLockJson ? src + "/package-lock.json" 431 | , buildInputs ? [ ] 432 | , nativeBuildInputs ? [ ] 433 | , nodejs ? default_nodejs 434 | , preBuild ? "" 435 | , postBuild ? "" 436 | , preInstallLinks ? null 437 | , sourceOverrides ? { } 438 | , githubSourceHashMap ? { } 439 | , passthru ? { } 440 | , ... 441 | }@args: 442 | assert (preInstallLinks != null) -> 443 | throw "`preInstallLinks` was removed use `sourceOverrides"; 444 | let 445 | cleanArgs = builtins.removeAttrs args [ "src" "packageJson" "packageLockJson" "buildInputs" "nativeBuildInputs" "nodejs" "preBuild" "postBuild" "sourceOverrides" "githubSourceHashMap" ]; 446 | lockfile = readPackageLikeFile packageLockJson; 447 | packagefile = readPackageLikeFile packageJson; 448 | 449 | sourceOptions = { 450 | sourceHashFunc = sourceHashFunc githubSourceHashMap; 451 | inherit nodejs sourceOverrides; 452 | packagesVersions = lockfile.packages or { }; 453 | }; 454 | 455 | allDependenciesNames = builtins.attrNames (packagefile.dependencies // packagefile.devDependencies or { }); 456 | 457 | patchedLockfilePath = patchedLockfile sourceOptions lockfile; 458 | patchedPackagefilePath = patchedPackagefile sourceOptions packagefile; 459 | in 460 | assert lockfile.lockfileVersion == 2 && lib.versionOlder nodejs.version "15.0" 461 | -> throw "npm lockfile V2 require nodejs version >= 15, it is not supported by nodejs ${nodejs.version}"; 462 | stdenv.mkDerivation ({ 463 | pname = lib.strings.sanitizeDerivationName lockfile.name; 464 | version = lockfile.version or "0"; 465 | dontUnpack = true; 466 | 467 | inherit nativeBuildInputs buildInputs preBuild postBuild; 468 | propagatedBuildInputs = [ 469 | nodejs 470 | ]; 471 | 472 | setupHooks = [ 473 | ./set-node-path.sh 474 | ]; 475 | 476 | preConfigure = '' 477 | export HOME=$(mktemp -d) 478 | ''; 479 | 480 | postPatch = '' 481 | ln -sf ${patchedLockfilePath} package-lock.json 482 | ln -sf ${patchedPackagefilePath} package.json 483 | ''; 484 | 485 | buildPhase = '' 486 | runHook preBuild 487 | export HOME=. 488 | npm ci --nodedir=${nodeSource nodejs} --ignore-scripts 489 | test -d node_modules/.bin && patchShebangs node_modules/.bin 490 | npm rebuild --offline --nodedir=${nodeSource nodejs} ${builtins.concatStringsSep " " allDependenciesNames} 491 | npm install --no-save --offline --nodedir=${nodeSource nodejs} 492 | test -d node_modules/.bin && patchShebangs node_modules/.bin 493 | runHook postBuild 494 | ''; 495 | installPhase = '' 496 | mkdir "$out" 497 | if test -d node_modules; then 498 | if [ $(ls -1 node_modules | wc -l) -gt 0 ] || [ -e node_modules/.bin ]; then 499 | mv node_modules $out/ 500 | if test -d $out/node_modules/.bin; then 501 | ln -s $out/node_modules/.bin $out/bin 502 | fi 503 | fi 504 | fi 505 | ''; 506 | 507 | passthru = passthru // { 508 | inherit nodejs; 509 | lockfile = patchedLockfilePath; 510 | packagesfile = patchedPackagefilePath; 511 | }; 512 | } // cleanArgs); 513 | 514 | shell = 515 | { src 516 | , node_modules_mode ? "symlink" 517 | , node_modules_attrs ? { } 518 | , buildInputs ? [ ] 519 | , passthru ? { } 520 | , shellHook ? "" 521 | , ... 522 | }@attrs: 523 | let 524 | nm = node_modules (get_node_modules_attrs attrs); 525 | extraAttrs = builtins.removeAttrs attrs [ "node_modules_attrs" "passthru" "shellHook" "buildInputs" ]; 526 | in 527 | mkShell ({ 528 | buildInputs = buildInputs ++ [ nm.nodejs nm ]; 529 | shellHook = '' 530 | # FIXME: we should somehow register a GC root here in case of a symlink? 531 | ${add_node_modules_to_cwd nm node_modules_mode} 532 | '' + shellHook; 533 | passthru = passthru // { 534 | node_modules = nm; 535 | }; 536 | } // extraAttrs); 537 | 538 | build = 539 | { src 540 | , buildCommands ? [ "npm run build" ] 541 | , installPhase 542 | , node_modules_attrs ? { } 543 | , node_modules_mode ? "symlink" 544 | , buildInputs ? [ ] 545 | , passthru ? { } 546 | , ... 547 | }@attrs: 548 | let 549 | nm = node_modules (get_node_modules_attrs attrs); 550 | extraAttrs = builtins.removeAttrs attrs [ "node_modules_attrs" "passthru" "buildInputs" ]; 551 | in 552 | stdenv.mkDerivation ({ 553 | pname = nm.pname; 554 | version = nm.version; 555 | buildInputs = [ nm ] ++ buildInputs; 556 | inherit src installPhase; 557 | 558 | preConfigure = add_node_modules_to_cwd nm node_modules_mode; 559 | 560 | buildPhase = '' 561 | runHook preBuild 562 | ${lib.concatStringsSep "\n" buildCommands} 563 | runHook postBuild 564 | ''; 565 | 566 | passthru = passthru // { node_modules = nm; }; 567 | } // extraAttrs); 568 | } 569 | -------------------------------------------------------------------------------- /nix/default.nix: -------------------------------------------------------------------------------- 1 | /* 2 | We have to ignore additional parameters passed to us as Nix 2.4 is injecting `inNixShell` without checking if the called expression actually wants it 3 | See: https://github.com/NixOS/nix/pull/5543 4 | */ 5 | { ... }: 6 | let 7 | sources = import ./sources.nix; 8 | in 9 | import sources.nixpkgs { 10 | overlays = [ 11 | (self: super: { 12 | npmlock2nix = self.callPackage ../default.nix { }; 13 | inherit (import sources.smoke { }) smoke; 14 | nix-pre-commit-hooks = import (sources.nix-pre-commit-hooks); 15 | }) 16 | ]; 17 | } 18 | -------------------------------------------------------------------------------- /nix/sources.json: -------------------------------------------------------------------------------- 1 | { 2 | "nix-pre-commit-hooks": { 3 | "branch": "master", 4 | "description": "Seamless integration of https://pre-commit.com git hooks with Nix.", 5 | "homepage": "", 6 | "owner": "cachix", 7 | "repo": "pre-commit-hooks.nix", 8 | "rev": "1303a1a76e9eb074075bfe566518c413f6fc104e", 9 | "sha256": "0nhp1bym29hlcxic7x7jqgqsq60bqzfxd5zbylpsafmdnam1dfrk", 10 | "type": "tarball", 11 | "url": "https://github.com/cachix/pre-commit-hooks.nix/archive/1303a1a76e9eb074075bfe566518c413f6fc104e.tar.gz", 12 | "url_template": "https://github.com///archive/.tar.gz" 13 | }, 14 | "nixpkgs": { 15 | "branch": "nixos-unstable", 16 | "description": "Nix Packages collection", 17 | "homepage": "", 18 | "owner": "nixos", 19 | "repo": "nixpkgs", 20 | "rev": "c5ed8beb478a8ca035f033f659b60c89500a3034", 21 | "sha256": "0i4dxvnz7myn90kfqv8l5nvsk1k6lgmhg1xfags2h5bazyb0rr4c", 22 | "type": "tarball", 23 | "url": "https://github.com/nixos/nixpkgs/archive/c5ed8beb478a8ca035f033f659b60c89500a3034.tar.gz", 24 | "url_template": "https://github.com///archive/.tar.gz" 25 | }, 26 | "smoke": { 27 | "branch": "main", 28 | "description": "Runs tests against anything, using command-line arguments, STDIN, STDOUT and STDERR.", 29 | "homepage": "", 30 | "owner": "SamirTalwar", 31 | "repo": "smoke", 32 | "rev": "61bb7c05bbbaa80df516adfcc60240889ed43fd1", 33 | "sha256": "1axg8cm18w41f4s7ph8qyarrdzazph87b1sb149hbxkpf2ckhahx", 34 | "type": "tarball", 35 | "url": "https://github.com/SamirTalwar/smoke/archive/61bb7c05bbbaa80df516adfcc60240889ed43fd1.tar.gz", 36 | "url_template": "https://github.com///archive/.tar.gz" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /nix/sources.nix: -------------------------------------------------------------------------------- 1 | # This file has been generated by Niv. 2 | let 3 | # 4 | # The fetchers. fetch_ fetches specs of type . 5 | # 6 | 7 | fetch_file = pkgs: spec: 8 | if spec.builtin or true then 9 | builtins_fetchurl { inherit (spec) url sha256; } 10 | else 11 | pkgs.fetchurl { inherit (spec) url sha256; }; 12 | 13 | fetch_tarball = pkgs: spec: 14 | if spec.builtin or true then 15 | builtins_fetchTarball { inherit (spec) url sha256; } 16 | else 17 | pkgs.fetchzip { inherit (spec) url sha256; }; 18 | 19 | fetch_git = spec: 20 | builtins.fetchGit { 21 | url = spec.repo; 22 | inherit (spec) rev; 23 | allRefs = true; 24 | }; 25 | 26 | fetch_builtin-tarball = spec: 27 | builtins.trace 28 | '' 29 | WARNING: 30 | The niv type "builtin-tarball" will soon be deprecated. You should 31 | instead use `builtin = true`. 32 | 33 | $ niv modify -a type=tarball -a builtin=true 34 | '' 35 | builtins_fetchTarball 36 | { inherit (spec) url sha256; }; 37 | 38 | fetch_builtin-url = spec: 39 | builtins.trace 40 | '' 41 | WARNING: 42 | The niv type "builtin-url" will soon be deprecated. You should 43 | instead use `builtin = true`. 44 | 45 | $ niv modify -a type=file -a builtin=true 46 | '' 47 | (builtins_fetchurl { inherit (spec) url sha256; }); 48 | 49 | # 50 | # Various helpers 51 | # 52 | 53 | # The set of packages used when specs are fetched using non-builtins. 54 | mkPkgs = sources: 55 | let 56 | sourcesNixpkgs = 57 | import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { }; 58 | hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath; 59 | hasThisAsNixpkgsPath = == ./.; 60 | in 61 | if builtins.hasAttr "nixpkgs" sources 62 | then sourcesNixpkgs 63 | else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then 64 | import { } 65 | else 66 | abort 67 | '' 68 | Please specify either (through -I or NIX_PATH=nixpkgs=...) or 69 | add a package called "nixpkgs" to your sources.json. 70 | ''; 71 | 72 | # The actual fetching function. 73 | fetch = pkgs: name: spec: 74 | 75 | if ! builtins.hasAttr "type" spec then 76 | abort "ERROR: niv spec ${name} does not have a 'type' attribute" 77 | else if spec.type == "file" then fetch_file pkgs spec 78 | else if spec.type == "tarball" then fetch_tarball pkgs spec 79 | else if spec.type == "git" then fetch_git spec 80 | else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec 81 | else if spec.type == "builtin-url" then fetch_builtin-url spec 82 | else 83 | abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}"; 84 | 85 | # Ports of functions for older nix versions 86 | 87 | # a Nix version of mapAttrs if the built-in doesn't exist 88 | mapAttrs = builtins.mapAttrs or ( 89 | f: set: with builtins; 90 | listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) 91 | ); 92 | 93 | # fetchTarball version that is compatible between all the versions of Nix 94 | builtins_fetchTarball = { url, sha256 }@attrs: 95 | let 96 | inherit (builtins) lessThan nixVersion fetchTarball; 97 | in 98 | if lessThan nixVersion "1.12" then 99 | fetchTarball { inherit url; } 100 | else 101 | fetchTarball attrs; 102 | 103 | # fetchurl version that is compatible between all the versions of Nix 104 | builtins_fetchurl = { url, sha256 }@attrs: 105 | let 106 | inherit (builtins) lessThan nixVersion fetchurl; 107 | in 108 | if lessThan nixVersion "1.12" then 109 | fetchurl { inherit url; } 110 | else 111 | fetchurl attrs; 112 | 113 | # Create the final "sources" from the config 114 | mkSources = config: 115 | mapAttrs 116 | ( 117 | name: spec: 118 | if builtins.hasAttr "outPath" spec 119 | then 120 | abort 121 | "The values in sources.json should not have an 'outPath' attribute" 122 | else 123 | spec // { outPath = fetch config.pkgs name spec; } 124 | ) 125 | config.sources; 126 | 127 | # The "config" used by the fetchers 128 | mkConfig = 129 | { sourcesFile ? ./sources.json 130 | , sources ? builtins.fromJSON (builtins.readFile sourcesFile) 131 | , pkgs ? mkPkgs sources 132 | }: rec { 133 | # The sources, i.e. the attribute set of spec name to spec 134 | inherit sources; 135 | 136 | # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers 137 | inherit pkgs; 138 | }; 139 | in 140 | mkSources (mkConfig { }) // { __functor = _: settings: mkSources (mkConfig settings); } 141 | -------------------------------------------------------------------------------- /set-node-path.sh: -------------------------------------------------------------------------------- 1 | function npmlock2nix_add_node_path() { 2 | addToSearchPath "NODE_PATH" "$1/node_modules" 3 | } 4 | 5 | 6 | addEnvHooks "$targetOffset" npmlock2nix_add_node_path 7 | -------------------------------------------------------------------------------- /shell.nix: -------------------------------------------------------------------------------- 1 | let 2 | pkgs = import ./nix { }; 3 | 4 | test-runner = pkgs.writeScriptBin "test-runner" '' 5 | find . -type f | ${pkgs.entr}/bin/entr -c nix-build -A tests --show-trace 6 | ''; 7 | 8 | pre-commit-hooks = pkgs.nix-pre-commit-hooks.run { 9 | src = ./.; 10 | tools = { 11 | nixpkgs-fmt = pkgs.nixpkgs-fmt; 12 | }; 13 | hooks = { 14 | nixpkgs-fmt.enable = true; 15 | }; 16 | }; 17 | 18 | in 19 | pkgs.mkShell { 20 | buildInputs = [ test-runner pkgs.nodejs pkgs.smoke pkgs.niv pkgs.nixpkgs-fmt ]; 21 | inherit (pre-commit-hooks) shellHook; 22 | } 23 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env nix-shell 2 | #!nix-shell -I nixpkgs=./nix -i bash 3 | 4 | set -e 5 | 6 | export XDG_CONFIG_HOME="/foo/bar" 7 | 8 | echo "========================================" 9 | echo "Running test suite for npm lockfiles V1" 10 | echo "========================================" 11 | 12 | echo -e "Running unit tests for lockfiles v1\n" 13 | nix-build -A tests.v1 --no-build-output --no-out-link --show-trace 14 | 15 | echo -e "\n\nRunning integration tests for lockfiles v1\n" 16 | $(nix-build -A tests.v1.integration-tests --no-out-link --show-trace) 17 | 18 | echo -e "\n\nTest githubSourceHashMap in restricted mode for lockfiles v1\n" 19 | nix-build tests/tests-v1/examples-projects/github-dependency/default.nix --restrict-eval -I . --allowed-uris 'https://github.com/nixos/nixpkgs' --show-trace 20 | 21 | echo "========================================" 22 | echo "Running test suite for npm lockfiles V2" 23 | echo "========================================" 24 | 25 | echo -e "Running unit tests for lockfiles v2\n" 26 | nix-build -A tests.v2 --no-build-output --no-out-link --show-trace 27 | 28 | echo -e "\n\nRunning integration tests for lockfiles v2\n" 29 | $(nix-build -A tests.v2.integration-tests --no-out-link --show-trace) 30 | 31 | echo -e "\n\nTest githubSourceHashMap in restricted mode for lockfiles v2\n" 32 | nix-build tests/tests-v2/examples-projects/github-dependency/default.nix --restrict-eval -I . --allowed-uris 'https://github.com/nixos/nixpkgs' --show-trace 33 | -------------------------------------------------------------------------------- /tests/default.nix: -------------------------------------------------------------------------------- 1 | { newScope }: 2 | let 3 | callPackage = newScope testPkgs; 4 | testPkgs = { 5 | testLib = callPackage ./lib.nix { }; 6 | }; 7 | in 8 | { 9 | v1 = callPackage ./tests-v1 { inherit callPackage; }; 10 | v2 = callPackage ./tests-v2 { inherit callPackage; }; 11 | } 12 | -------------------------------------------------------------------------------- /tests/lib.nix: -------------------------------------------------------------------------------- 1 | { lib 2 | , stdenv 3 | , runCommand 4 | , writeTextFile 5 | , writeShellScript 6 | , nix 7 | , smoke 8 | , coreutils 9 | }: { 10 | # Reads a given file (either drv, path or string) and returns it's sha256 hash 11 | hashFile = filename: builtins.hashString "sha256" (builtins.readFile filename); 12 | 13 | noSourceOptions = { 14 | sourceHashFunc = _: null; 15 | nodejs = null; 16 | }; 17 | 18 | runTests = tests: 19 | let 20 | failures = lib.debug.runTests tests; 21 | msg = "Tests failed:\n" + 22 | lib.concatMapStringsSep "\n" 23 | (v: '' 24 | FAIL: ${v.name} 25 | expected: ${builtins.toJSON v.expected} 26 | got: ${builtins.toJSON v.result} 27 | '') 28 | failures; 29 | in 30 | if builtins.length failures == 0 then [ ] else 31 | builtins.throw msg; 32 | 33 | # Takes an attribute set of tests 34 | # an creates a smoke file that executes them. 35 | # Each tests set has this format: 36 | # { description 37 | # , shell 38 | # , command 39 | # , expected 40 | # , (optional) expected-stderr 41 | # , (optional) status 42 | # , (optional) temporary-directory = true 43 | # , (optional) setup-command 44 | # } 45 | makeIntegrationTests = tests: 46 | let 47 | mkTestScript = name: test: 48 | let 49 | shellDrv = (test.shell.overrideAttrs (_: { phases = [ "noopPhase" ]; noopPhase = "touch $out"; })).drvPath; 50 | temporaryDirectory = test.temporary-directory or true; 51 | in 52 | writeShellScript name '' 53 | export PATH="${nix}/bin:${coreutils}/bin" 54 | set -e 55 | ${lib.optionalString temporaryDirectory '' 56 | WORKING_DIR=$(mktemp -d) 57 | function cleanup { 58 | rm -rf "$WORKING_DIR" 59 | } 60 | trap cleanup EXIT 61 | cd $WORKING_DIR 62 | ''} 63 | ${lib.optionalString (test ? setup-command) test.setup-command} 64 | ${if test.evalFailure or false then '' 65 | nix-shell --pure ${../.} -A tests.integration-tests.shells.${name} --run "exit 23" 66 | '' else '' 67 | nix-shell --pure ${shellDrv} --run "${writeShellScript "${name}-command" test.command}" 68 | ''} 69 | ''; 70 | testScripts = lib.mapAttrs (name: test: test // { script = mkTestScript name test; inherit name; }) tests; 71 | 72 | smokeConfig.tests = map 73 | (test: { 74 | inherit (test) name; 75 | command = test.script; 76 | stdout = test.expected; 77 | exit-status = test.status or 0; 78 | } // lib.optionalAttrs (test ? expected-stderr) { 79 | stderr = test.expected-stderr; 80 | }) 81 | (lib.attrValues testScripts); 82 | 83 | testScriptDir = writeTextFile { 84 | name = "smoke.yml"; 85 | destination = "/smoke.yaml"; 86 | text = builtins.toJSON smokeConfig; 87 | }; 88 | in 89 | runCommand "tests" 90 | { 91 | name = "tests"; 92 | text = '' 93 | #!${stdenv.shell} 94 | exec ${smoke}/bin/smoke ${testScriptDir} 95 | ''; 96 | passthru = { 97 | shells = lib.mapAttrs (_: v: v.shell) testScripts; 98 | }; 99 | passAsFile = [ "text" ]; 100 | } '' 101 | cp $textPath $out 102 | chmod +x $out 103 | ''; 104 | 105 | withoutNodeModules = src: lib.cleanSourceWith { 106 | filter = name: type: ! (type == "directory" && name == "node_modules"); 107 | inherit src; 108 | }; 109 | } 110 | -------------------------------------------------------------------------------- /tests/tests-v1/build-tests.nix: -------------------------------------------------------------------------------- 1 | { lib, symlinkJoin, npmlock2nix, runCommand, libwebp, python3 }: 2 | let 3 | symlinkAttrs = attrs: runCommand "symlink-attrs" 4 | { } 5 | ( 6 | let 7 | drvs = lib.attrValues (lib.mapAttrs (name: drv: { inherit name drv; }) attrs); 8 | in 9 | '' 10 | mkdir $out 11 | ${lib.concatMapStringsSep "\n" (o: "ln -s ${o.drv} $out/${o.name}") drvs} 12 | '' 13 | ); 14 | in 15 | symlinkAttrs { 16 | webpack-cli-project-default-build-command = npmlock2nix.v1.build { 17 | src = ./examples-projects/webpack-cli-project; 18 | installPhase = '' 19 | cp -r dist $out 20 | ''; 21 | }; 22 | 23 | webpack-cli-project-custom-build-command = npmlock2nix.v1.build { 24 | src = ./examples-projects/webpack-cli-project; 25 | buildCommands = [ "webpack --mode=production" ]; 26 | installPhase = '' 27 | cp -r dist $out 28 | ''; 29 | }; 30 | 31 | node-modules-attributes-are-passed-through = npmlock2nix.v1.build { 32 | src = ./examples-projects/bin-wrapped-dep; 33 | buildCommands = [ 34 | '' 35 | readlink -f $(node -e "console.log(require('cwebp-bin'))") > actual 36 | echo ${libwebp}/bin/cwebp > expected 37 | '' 38 | ]; 39 | installPhase = '' 40 | cp actual $out 41 | ''; 42 | 43 | doCheck = true; 44 | checkPhase = '' 45 | cmp actual expected || exit 1 46 | ''; 47 | 48 | node_modules_attrs = { 49 | preInstallLinks = { 50 | "cwebp-bin"."vendor/cwebp" = "${libwebp}/bin/cwebp"; 51 | }; 52 | }; 53 | }; 54 | 55 | passsing-buildInputs-doesnt-break-the-build = npmlock2nix.v1.build { 56 | src = ./examples-projects/webpack-cli-project; 57 | installPhase = '' 58 | cp -r dist $out 59 | ''; 60 | 61 | buildInputs = [ python3 ]; 62 | }; 63 | } 64 | -------------------------------------------------------------------------------- /tests/tests-v1/build.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib }: 2 | testLib.runTests { 3 | testPassthruIsHonored = 4 | let 5 | drv = npmlock2nix.v1.build { 6 | src = ./examples-projects/single-dependency; 7 | installPhase = " 8 | # should never run as we only test eval here 9 | exit 123 10 | "; 11 | passthru.test-attr = 123; 12 | }; 13 | in 14 | { 15 | expr = { 16 | inherit (drv.passthru) test-attr; 17 | has_node_modules = drv.passthru ? node_modules; 18 | }; 19 | expected = { 20 | test-attr = 123; 21 | has_node_modules = true; 22 | }; 23 | }; 24 | } 25 | -------------------------------------------------------------------------------- /tests/tests-v1/default.nix: -------------------------------------------------------------------------------- 1 | { callPackage }: 2 | { 3 | build = callPackage ./build.nix { }; 4 | build-tests = callPackage ./build-tests.nix { }; 5 | integration-tests = callPackage ./integration-tests { }; 6 | make-github-source = callPackage ./make-github-source.nix { }; 7 | make-source = callPackage ./make-source.nix { }; 8 | make-source-urls = callPackage ./make-source-urls.nix { }; 9 | node-modules = callPackage ./node-modules.nix { }; 10 | parse-github-ref = callPackage ./parse-github-ref.nix { }; 11 | patch-lockfile = callPackage ./patch-lockfile.nix { }; 12 | patch-packagefile = callPackage ./patch-packagefile.nix { }; 13 | read-lockfile = callPackage ./read-lockfile { }; 14 | shell = callPackage ./shell.nix { }; 15 | source-hash-func = callPackage ./source-hash-func.nix { }; 16 | } 17 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/bin-project/index.js: -------------------------------------------------------------------------------- 1 | console.log("hello world"); 2 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/bin-project/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bin-project", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "mkdirp": { 8 | "version": "1.0.4", 9 | "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", 10 | "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/bin-project/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bin-project", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "mkdirp": "^1.0.4" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/bin-project/shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | let 3 | node = pkgs.nodejs-10_x; 4 | in 5 | # We need make sure that `nodejs` does not default to `nodejs-10_x` because 6 | # then our test cannot ensure that we can override the default. If the assert 7 | # below throws, change `node` above to a different version. 8 | assert pkgs.nodejs == node -> throw "`nodejs` is refering to `nodejs-10_x` rendering this test ineffective."; 9 | pkgs.npmlock2nix.v1.shell { 10 | src = ./.; 11 | nodejs = node; 12 | } 13 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/bin-wrapped-dep/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bin-wrapped-dep", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "cwebp-bin": "5.1.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/bin-wrapped-dep/shell.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, libwebp, python3 }: 2 | npmlock2nix.v1.shell { 3 | src = ./.; 4 | node_modules_attrs = { 5 | buildInputs = [ 6 | python3 # for node-gyp 7 | libwebp # cwebp-bin 8 | ]; 9 | 10 | preInstallLinks = { 11 | "cwebp-bin"."vendor/cwebp" = "${libwebp}/bin/cwebp"; 12 | }; 13 | }; 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/github-dependency-branch/default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | 3 | pkgs.npmlock2nix.v1.node_modules { 4 | src = ./.; 5 | packageJson = ./package.json; 6 | packageLockJson = ./package-lock.json; 7 | githubSourceHashMap = { 8 | tmcw.leftpad.db1442a0556c2b133627ffebf455a78a1ced64b9 = "1zyy1nxbby4wcl30rc8fsis1c3f7nafavnwd3qi4bg0x00gxjdnh"; 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/github-dependency-branch/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "leftpad": { 8 | "version": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9", 9 | "from": "github:tmcw/leftpad#v0.0.1" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/github-dependency-branch/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "github:tmcw/leftpad#v0.0.1" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/github-dependency/default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../../nix { } }: 2 | 3 | pkgs.npmlock2nix.v1.node_modules { 4 | src = ./.; 5 | packageJson = ./package.json; 6 | packageLockJson = ./package-lock.json; 7 | githubSourceHashMap = { 8 | tmcw.leftpad.db1442a0556c2b133627ffebf455a78a1ced64b9 = "1zyy1nxbby4wcl30rc8fsis1c3f7nafavnwd3qi4bg0x00gxjdnh"; 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/github-dependency/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "leftpad": { 8 | "version": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9", 9 | "from": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/github-dependency/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/github-dev-dependency/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "leftpad": { 8 | "version": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9", 9 | "from": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9", 10 | "dev": true 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/github-dev-dependency/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "devDependencies": { 12 | "leftpad": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/native-extensions/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "native-extensions", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "abbrev": { 8 | "version": "1.1.1", 9 | "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", 10 | "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==" 11 | }, 12 | "ansi-regex": { 13 | "version": "2.1.1", 14 | "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", 15 | "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" 16 | }, 17 | "aproba": { 18 | "version": "1.2.0", 19 | "resolved": "https://registry.npmjs.org/aproba/-/aproba-1.2.0.tgz", 20 | "integrity": "sha512-Y9J6ZjXtoYh8RnXVCMOU/ttDmk1aBjunq9vO0ta5x85WDQiQfUF9sIPBITdbiiIVcBo03Hi3jMxigBtsddlXRw==" 21 | }, 22 | "are-we-there-yet": { 23 | "version": "1.1.5", 24 | "resolved": "https://registry.npmjs.org/are-we-there-yet/-/are-we-there-yet-1.1.5.tgz", 25 | "integrity": "sha512-5hYdAkZlcG8tOLujVDTgCT+uPX0VnpAH28gWsLfzpXYm7wP6mp5Q/gYyR7YQ0cKVJcXJnl3j2kpBan13PtQf6w==", 26 | "requires": { 27 | "delegates": "^1.0.0", 28 | "readable-stream": "^2.0.6" 29 | } 30 | }, 31 | "balanced-match": { 32 | "version": "1.0.0", 33 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", 34 | "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" 35 | }, 36 | "bcrypt": { 37 | "version": "5.0.0", 38 | "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-5.0.0.tgz", 39 | "integrity": "sha512-jB0yCBl4W/kVHM2whjfyqnxTmOHkCX4kHEa5nYKSoGeYe8YrjTYTc87/6bwt1g8cmV0QrbhKriETg9jWtcREhg==", 40 | "requires": { 41 | "node-addon-api": "^3.0.0", 42 | "node-pre-gyp": "0.15.0" 43 | } 44 | }, 45 | "brace-expansion": { 46 | "version": "1.1.11", 47 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", 48 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", 49 | "requires": { 50 | "balanced-match": "^1.0.0", 51 | "concat-map": "0.0.1" 52 | } 53 | }, 54 | "chownr": { 55 | "version": "1.1.4", 56 | "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", 57 | "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==" 58 | }, 59 | "code-point-at": { 60 | "version": "1.1.0", 61 | "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", 62 | "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" 63 | }, 64 | "concat-map": { 65 | "version": "0.0.1", 66 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", 67 | "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" 68 | }, 69 | "console-control-strings": { 70 | "version": "1.1.0", 71 | "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz", 72 | "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=" 73 | }, 74 | "core-util-is": { 75 | "version": "1.0.2", 76 | "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", 77 | "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" 78 | }, 79 | "debug": { 80 | "version": "3.2.6", 81 | "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", 82 | "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", 83 | "requires": { 84 | "ms": "^2.1.1" 85 | } 86 | }, 87 | "deep-extend": { 88 | "version": "0.6.0", 89 | "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", 90 | "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==" 91 | }, 92 | "delegates": { 93 | "version": "1.0.0", 94 | "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", 95 | "integrity": "sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=" 96 | }, 97 | "detect-libc": { 98 | "version": "1.0.3", 99 | "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", 100 | "integrity": "sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=" 101 | }, 102 | "fs-minipass": { 103 | "version": "1.2.7", 104 | "resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz", 105 | "integrity": "sha512-GWSSJGFy4e9GUeCcbIkED+bgAoFyj7XF1mV8rma3QW4NIqX9Kyx79N/PF61H5udOV3aY1IaMLs6pGbH71nlCTA==", 106 | "requires": { 107 | "minipass": "^2.6.0" 108 | } 109 | }, 110 | "fs.realpath": { 111 | "version": "1.0.0", 112 | "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", 113 | "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" 114 | }, 115 | "gauge": { 116 | "version": "2.7.4", 117 | "resolved": "https://registry.npmjs.org/gauge/-/gauge-2.7.4.tgz", 118 | "integrity": "sha1-LANAXHU4w51+s3sxcCLjJfsBi/c=", 119 | "requires": { 120 | "aproba": "^1.0.3", 121 | "console-control-strings": "^1.0.0", 122 | "has-unicode": "^2.0.0", 123 | "object-assign": "^4.1.0", 124 | "signal-exit": "^3.0.0", 125 | "string-width": "^1.0.1", 126 | "strip-ansi": "^3.0.1", 127 | "wide-align": "^1.1.0" 128 | } 129 | }, 130 | "glob": { 131 | "version": "7.1.6", 132 | "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", 133 | "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", 134 | "requires": { 135 | "fs.realpath": "^1.0.0", 136 | "inflight": "^1.0.4", 137 | "inherits": "2", 138 | "minimatch": "^3.0.4", 139 | "once": "^1.3.0", 140 | "path-is-absolute": "^1.0.0" 141 | } 142 | }, 143 | "has-unicode": { 144 | "version": "2.0.1", 145 | "resolved": "https://registry.npmjs.org/has-unicode/-/has-unicode-2.0.1.tgz", 146 | "integrity": "sha1-4Ob+aijPUROIVeCG0Wkedx3iqLk=" 147 | }, 148 | "iconv-lite": { 149 | "version": "0.4.24", 150 | "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", 151 | "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", 152 | "requires": { 153 | "safer-buffer": ">= 2.1.2 < 3" 154 | } 155 | }, 156 | "ignore-walk": { 157 | "version": "3.0.3", 158 | "resolved": "https://registry.npmjs.org/ignore-walk/-/ignore-walk-3.0.3.tgz", 159 | "integrity": "sha512-m7o6xuOaT1aqheYHKf8W6J5pYH85ZI9w077erOzLje3JsB1gkafkAhHHY19dqjulgIZHFm32Cp5uNZgcQqdJKw==", 160 | "requires": { 161 | "minimatch": "^3.0.4" 162 | } 163 | }, 164 | "inflight": { 165 | "version": "1.0.6", 166 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", 167 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", 168 | "requires": { 169 | "once": "^1.3.0", 170 | "wrappy": "1" 171 | } 172 | }, 173 | "inherits": { 174 | "version": "2.0.4", 175 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", 176 | "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" 177 | }, 178 | "ini": { 179 | "version": "1.3.8", 180 | "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", 181 | "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==" 182 | }, 183 | "is-fullwidth-code-point": { 184 | "version": "1.0.0", 185 | "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", 186 | "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", 187 | "requires": { 188 | "number-is-nan": "^1.0.0" 189 | } 190 | }, 191 | "isarray": { 192 | "version": "1.0.0", 193 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", 194 | "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" 195 | }, 196 | "minimatch": { 197 | "version": "3.0.4", 198 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", 199 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", 200 | "requires": { 201 | "brace-expansion": "^1.1.7" 202 | } 203 | }, 204 | "minimist": { 205 | "version": "1.2.5", 206 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", 207 | "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==" 208 | }, 209 | "minipass": { 210 | "version": "2.9.0", 211 | "resolved": "https://registry.npmjs.org/minipass/-/minipass-2.9.0.tgz", 212 | "integrity": "sha512-wxfUjg9WebH+CUDX/CdbRlh5SmfZiy/hpkxaRI16Y9W56Pa75sWgd/rvFilSgrauD9NyFymP/+JFV3KwzIsJeg==", 213 | "requires": { 214 | "safe-buffer": "^5.1.2", 215 | "yallist": "^3.0.0" 216 | } 217 | }, 218 | "minizlib": { 219 | "version": "1.3.3", 220 | "resolved": "https://registry.npmjs.org/minizlib/-/minizlib-1.3.3.tgz", 221 | "integrity": "sha512-6ZYMOEnmVsdCeTJVE0W9ZD+pVnE8h9Hma/iOwwRDsdQoePpoX56/8B6z3P9VNwppJuBKNRuFDRNRqRWexT9G9Q==", 222 | "requires": { 223 | "minipass": "^2.9.0" 224 | } 225 | }, 226 | "mkdirp": { 227 | "version": "0.5.5", 228 | "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz", 229 | "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==", 230 | "requires": { 231 | "minimist": "^1.2.5" 232 | } 233 | }, 234 | "ms": { 235 | "version": "2.1.2", 236 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", 237 | "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" 238 | }, 239 | "needle": { 240 | "version": "2.5.0", 241 | "resolved": "https://registry.npmjs.org/needle/-/needle-2.5.0.tgz", 242 | "integrity": "sha512-o/qITSDR0JCyCKEQ1/1bnUXMmznxabbwi/Y4WwJElf+evwJNFNwIDMCCt5IigFVxgeGBJESLohGtIS9gEzo1fA==", 243 | "requires": { 244 | "debug": "^3.2.6", 245 | "iconv-lite": "^0.4.4", 246 | "sax": "^1.2.4" 247 | } 248 | }, 249 | "node-addon-api": { 250 | "version": "3.0.0", 251 | "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.0.0.tgz", 252 | "integrity": "sha512-sSHCgWfJ+Lui/u+0msF3oyCgvdkhxDbkCS6Q8uiJquzOimkJBvX6hl5aSSA7DR1XbMpdM8r7phjcF63sF4rkKg==" 253 | }, 254 | "node-pre-gyp": { 255 | "version": "0.15.0", 256 | "resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.15.0.tgz", 257 | "integrity": "sha512-7QcZa8/fpaU/BKenjcaeFF9hLz2+7S9AqyXFhlH/rilsQ/hPZKK32RtR5EQHJElgu+q5RfbJ34KriI79UWaorA==", 258 | "requires": { 259 | "detect-libc": "^1.0.2", 260 | "mkdirp": "^0.5.3", 261 | "needle": "^2.5.0", 262 | "nopt": "^4.0.1", 263 | "npm-packlist": "^1.1.6", 264 | "npmlog": "^4.0.2", 265 | "rc": "^1.2.7", 266 | "rimraf": "^2.6.1", 267 | "semver": "^5.3.0", 268 | "tar": "^4.4.2" 269 | } 270 | }, 271 | "nopt": { 272 | "version": "4.0.3", 273 | "resolved": "https://registry.npmjs.org/nopt/-/nopt-4.0.3.tgz", 274 | "integrity": "sha512-CvaGwVMztSMJLOeXPrez7fyfObdZqNUK1cPAEzLHrTybIua9pMdmmPR5YwtfNftIOMv3DPUhFaxsZMNTQO20Kg==", 275 | "requires": { 276 | "abbrev": "1", 277 | "osenv": "^0.1.4" 278 | } 279 | }, 280 | "npm-bundled": { 281 | "version": "1.1.1", 282 | "resolved": "https://registry.npmjs.org/npm-bundled/-/npm-bundled-1.1.1.tgz", 283 | "integrity": "sha512-gqkfgGePhTpAEgUsGEgcq1rqPXA+tv/aVBlgEzfXwA1yiUJF7xtEt3CtVwOjNYQOVknDk0F20w58Fnm3EtG0fA==", 284 | "requires": { 285 | "npm-normalize-package-bin": "^1.0.1" 286 | } 287 | }, 288 | "npm-normalize-package-bin": { 289 | "version": "1.0.1", 290 | "resolved": "https://registry.npmjs.org/npm-normalize-package-bin/-/npm-normalize-package-bin-1.0.1.tgz", 291 | "integrity": "sha512-EPfafl6JL5/rU+ot6P3gRSCpPDW5VmIzX959Ob1+ySFUuuYHWHekXpwdUZcKP5C+DS4GEtdJluwBjnsNDl+fSA==" 292 | }, 293 | "npm-packlist": { 294 | "version": "1.4.8", 295 | "resolved": "https://registry.npmjs.org/npm-packlist/-/npm-packlist-1.4.8.tgz", 296 | "integrity": "sha512-5+AZgwru5IevF5ZdnFglB5wNlHG1AOOuw28WhUq8/8emhBmLv6jX5by4WJCh7lW0uSYZYS6DXqIsyZVIXRZU9A==", 297 | "requires": { 298 | "ignore-walk": "^3.0.1", 299 | "npm-bundled": "^1.0.1", 300 | "npm-normalize-package-bin": "^1.0.1" 301 | } 302 | }, 303 | "npmlog": { 304 | "version": "4.1.2", 305 | "resolved": "https://registry.npmjs.org/npmlog/-/npmlog-4.1.2.tgz", 306 | "integrity": "sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg==", 307 | "requires": { 308 | "are-we-there-yet": "~1.1.2", 309 | "console-control-strings": "~1.1.0", 310 | "gauge": "~2.7.3", 311 | "set-blocking": "~2.0.0" 312 | } 313 | }, 314 | "number-is-nan": { 315 | "version": "1.0.1", 316 | "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", 317 | "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" 318 | }, 319 | "object-assign": { 320 | "version": "4.1.1", 321 | "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", 322 | "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" 323 | }, 324 | "once": { 325 | "version": "1.4.0", 326 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", 327 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", 328 | "requires": { 329 | "wrappy": "1" 330 | } 331 | }, 332 | "os-homedir": { 333 | "version": "1.0.2", 334 | "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", 335 | "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=" 336 | }, 337 | "os-tmpdir": { 338 | "version": "1.0.2", 339 | "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", 340 | "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" 341 | }, 342 | "osenv": { 343 | "version": "0.1.5", 344 | "resolved": "https://registry.npmjs.org/osenv/-/osenv-0.1.5.tgz", 345 | "integrity": "sha512-0CWcCECdMVc2Rw3U5w9ZjqX6ga6ubk1xDVKxtBQPK7wis/0F2r9T6k4ydGYhecl7YUBxBVxhL5oisPsNxAPe2g==", 346 | "requires": { 347 | "os-homedir": "^1.0.0", 348 | "os-tmpdir": "^1.0.0" 349 | } 350 | }, 351 | "path-is-absolute": { 352 | "version": "1.0.1", 353 | "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", 354 | "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" 355 | }, 356 | "process-nextick-args": { 357 | "version": "2.0.1", 358 | "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", 359 | "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" 360 | }, 361 | "rc": { 362 | "version": "1.2.8", 363 | "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", 364 | "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", 365 | "requires": { 366 | "deep-extend": "^0.6.0", 367 | "ini": "~1.3.0", 368 | "minimist": "^1.2.0", 369 | "strip-json-comments": "~2.0.1" 370 | } 371 | }, 372 | "readable-stream": { 373 | "version": "2.3.7", 374 | "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", 375 | "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", 376 | "requires": { 377 | "core-util-is": "~1.0.0", 378 | "inherits": "~2.0.3", 379 | "isarray": "~1.0.0", 380 | "process-nextick-args": "~2.0.0", 381 | "safe-buffer": "~5.1.1", 382 | "string_decoder": "~1.1.1", 383 | "util-deprecate": "~1.0.1" 384 | } 385 | }, 386 | "rimraf": { 387 | "version": "2.7.1", 388 | "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", 389 | "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", 390 | "requires": { 391 | "glob": "^7.1.3" 392 | } 393 | }, 394 | "safe-buffer": { 395 | "version": "5.1.2", 396 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", 397 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" 398 | }, 399 | "safer-buffer": { 400 | "version": "2.1.2", 401 | "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", 402 | "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" 403 | }, 404 | "sax": { 405 | "version": "1.2.4", 406 | "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", 407 | "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" 408 | }, 409 | "semver": { 410 | "version": "5.7.1", 411 | "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", 412 | "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" 413 | }, 414 | "set-blocking": { 415 | "version": "2.0.0", 416 | "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz", 417 | "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=" 418 | }, 419 | "signal-exit": { 420 | "version": "3.0.3", 421 | "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.3.tgz", 422 | "integrity": "sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA==" 423 | }, 424 | "string-width": { 425 | "version": "1.0.2", 426 | "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", 427 | "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", 428 | "requires": { 429 | "code-point-at": "^1.0.0", 430 | "is-fullwidth-code-point": "^1.0.0", 431 | "strip-ansi": "^3.0.0" 432 | } 433 | }, 434 | "string_decoder": { 435 | "version": "1.1.1", 436 | "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", 437 | "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", 438 | "requires": { 439 | "safe-buffer": "~5.1.0" 440 | } 441 | }, 442 | "strip-ansi": { 443 | "version": "3.0.1", 444 | "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", 445 | "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", 446 | "requires": { 447 | "ansi-regex": "^2.0.0" 448 | } 449 | }, 450 | "strip-json-comments": { 451 | "version": "2.0.1", 452 | "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", 453 | "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=" 454 | }, 455 | "tar": { 456 | "version": "4.4.13", 457 | "resolved": "https://registry.npmjs.org/tar/-/tar-4.4.13.tgz", 458 | "integrity": "sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA==", 459 | "requires": { 460 | "chownr": "^1.1.1", 461 | "fs-minipass": "^1.2.5", 462 | "minipass": "^2.8.6", 463 | "minizlib": "^1.2.1", 464 | "mkdirp": "^0.5.0", 465 | "safe-buffer": "^5.1.2", 466 | "yallist": "^3.0.3" 467 | } 468 | }, 469 | "util-deprecate": { 470 | "version": "1.0.2", 471 | "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", 472 | "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" 473 | }, 474 | "wide-align": { 475 | "version": "1.1.3", 476 | "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", 477 | "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==", 478 | "requires": { 479 | "string-width": "^1.0.2 || 2" 480 | } 481 | }, 482 | "wrappy": { 483 | "version": "1.0.2", 484 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", 485 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" 486 | }, 487 | "yallist": { 488 | "version": "3.1.1", 489 | "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", 490 | "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==" 491 | } 492 | } 493 | } 494 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/native-extensions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "native-extensions", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "bcrypt": "^5.0.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/nested-dependencies/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nested-dependencies", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "rxjs": { 8 | "version": "6.6.0", 9 | "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.0.tgz", 10 | "integrity": "sha512-3HMA8z/Oz61DUHe+SdOiQyzIf4tOx5oQHmMir7IZEu6TMqCLHT4LRcmNaUS0NwOz8VLvmmBduMsoaUvMaIiqzg==", 11 | "requires": { 12 | "tslib": "^1.9.0" 13 | }, 14 | "dependencies": { 15 | "tslib": { 16 | "version": "1.13.0", 17 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz", 18 | "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==" 19 | } 20 | } 21 | }, 22 | "tslib": { 23 | "version": "1.7.0", 24 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.7.0.tgz", 25 | "integrity": "sha1-boNmaV9ylhJSs1FnsN1Pvur7pJE=" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/nested-dependencies/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nested-dependencies", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "rxjs": "^6.6.0", 13 | "tslib": "=1.7.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/no-dependencies/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "no-dependencies", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1 5 | } 6 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/no-dependencies/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "no-dependencies", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC" 11 | } 12 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/no-version/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "no-version", 3 | "private": true, 4 | "scripts": { 5 | "dev": "next dev", 6 | "build": "next build", 7 | "start": "next start", 8 | "lint": "next lint" 9 | }, 10 | "dependencies": { 11 | "@emotion/react": "^11.7.1", 12 | "@emotion/styled": "^11.6.0", 13 | "@mui/icons-material": "^5.4.1", 14 | "@mui/material": "^5.4.0", 15 | "axios": "^0.25.0", 16 | "formik": "^2.2.9", 17 | "next": "12.0.10", 18 | "next-auth": "^4.2.1", 19 | "react": "17.0.2", 20 | "react-dom": "17.0.2", 21 | "react-toast-notifications": "^2.5.1", 22 | "swr": "^1.2.1" 23 | }, 24 | "devDependencies": { 25 | "@types/lodash": "^4.14.178", 26 | "@types/next-auth": "^3.15.0", 27 | "@types/node": "17.0.15", 28 | "@types/react": "17.0.39", 29 | "@types/yup": "^0.29.13", 30 | "eslint": "8.8.0", 31 | "eslint-config-next": "12.0.10", 32 | "restapify": "^2.3.4", 33 | "typescript": "4.5.5" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/nodejs-version-shell/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "leftpad": { 8 | "version": "0.0.1", 9 | "resolved": "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz", 10 | "integrity": "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU=" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/nodejs-version-shell/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "0.0.1" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/nodejs-version-shell/shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../../nix { } }: 2 | let 3 | node = pkgs.nodejs-10_x; 4 | in 5 | # We need make sure that `nodejs` does not default to `nodejs-10_x` because 6 | # then our test cannot ensure that we can override the default. If the assert 7 | # below throws, change `node` above to a different version. 8 | assert pkgs.nodejs == node -> throw "`nodejs` is refering to `nodejs-10_x` rendering this test ineffective."; 9 | pkgs.npmlock2nix.v1.shell { 10 | src = ./.; 11 | nodejs = node; 12 | } 13 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/single-dependency/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "leftpad": { 8 | "version": "0.0.1", 9 | "resolved": "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz", 10 | "integrity": "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU=" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/single-dependency/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "0.0.1" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/single-dependency/shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | pkgs.npmlock2nix.v1.shell { 3 | src = ./.; 4 | } 5 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/source-patching/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "source-patching", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "custom-hello-world": { 8 | "version": "1.0.3", 9 | "resolved": "https://registry.npmjs.org/custom-hello-world/-/custom-hello-world-1.0.3.tgz", 10 | "integrity": "sha512-Rinkq1q+uLmgbDLZRNZrlyeK3G9e+o0gbVy6r948kySbau0ymwOPNybu4iIkb4R1gE5aZsQUHgtfCg2oZ4zfbw==" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/source-patching/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "source-patching", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "author": "", 7 | "license": "ISC", 8 | "dependencies": { 9 | "custom-hello-world": "^1.0.0" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/source-patching/shell.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix }: 2 | npmlock2nix.v1.shell { 3 | src = ./.; 4 | node_modules_attrs = { 5 | sourceOverrides = { 6 | custom-hello-world = sourceInfo: drv: drv.overrideAttrs (old: { 7 | patches = builtins.toFile "custom-hello-world.patch" '' 8 | diff --git a/lib/index.js b/lib/index.js 9 | index 1f66513..64391a7 100644 10 | --- a/lib/index.js 11 | +++ b/lib/index.js 12 | @@ -21,7 +21,7 @@ function generateHelloWorld({ comma, exclamation, lowercase }) { 13 | if (comma) 14 | helloWorldStr += ','; 15 | 16 | - helloWorldStr += ' World'; 17 | + helloWorldStr += ' Nix'; 18 | 19 | if (exclamation) 20 | helloWorldStr += '!'; 21 | ''; 22 | }); 23 | }; 24 | }; 25 | } 26 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/unsanitized-package-name/default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | pkgs.npmlock2nix.v1.build { 3 | src = ./.; 4 | buildCommands = [ "echo BUILDING" ]; 5 | installPhase = "mkdir $out"; 6 | } 7 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/unsanitized-package-name/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@foo/bar", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "left-pad": { 8 | "version": "1.3.0", 9 | "resolved": "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz", 10 | "integrity": "sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/unsanitized-package-name/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@foo/bar", 3 | "version": "1.0.0", 4 | "description": "foo with bar", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "left-pad": "^1.3.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/unsanitized-package-name/shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | pkgs.npmlock2nix.v1.shell { 3 | src = ./.; 4 | } 5 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/url-as-version/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "url-as-version", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@matrix-org/olm": { 8 | "version": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.4.tgz", 9 | "integrity": "sha512-ddaXWILlm1U0Z9qpcZffJjBFZRpz/GxQ1n/Qth3xKvYRUbniuPOgftNTDaxkEC4h04uJG5Ls/OdI1YJUyfuRzQ==" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/url-as-version/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "url-as-version", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.4.tgz" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/webpack-cli-project/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-cli-project", 3 | "version": "1.0.0", 4 | "description": "", 5 | "license": "ISC", 6 | "main": "index.js", 7 | "scripts": { 8 | "build": "webpack --mode=production" 9 | }, 10 | "dependencies": { 11 | "bootstrap": "^4.5.0", 12 | "html-webpack-plugin": "^4.3.0", 13 | "jquery": "^3.5.1", 14 | "popper.js": "^1.16.1", 15 | "webpack": "^4.44.0", 16 | "webpack-cli": "^3.3.12" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/webpack-cli-project/src/index.js: -------------------------------------------------------------------------------- 1 | import 'bootstrap' 2 | -------------------------------------------------------------------------------- /tests/tests-v1/examples-projects/webpack-cli-project/webpack.config.js: -------------------------------------------------------------------------------- 1 | const HtmlWebpackPlugin = require("html-webpack-plugin"); 2 | // const path = require('path'); 3 | 4 | module.exports = { 5 | plugins: [ 6 | new HtmlWebpackPlugin({ 7 | title: "Webpack Output", 8 | }), 9 | ], 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /tests/tests-v1/integration-tests/default.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, nodejs-16_x, callPackage, libwebp, runCommandNoCC, python3 }: 2 | testLib.makeIntegrationTests { 3 | leftpad = { 4 | description = "Require a node dependency inside the shell environment"; 5 | shell = npmlock2nix.v1.shell { src = ../examples-projects/single-dependency; }; 6 | command = '' 7 | node -e 'console.log(require("leftpad")(123, 7));' 8 | ''; 9 | expected = "0000123\n"; 10 | }; 11 | nodejsVersion = { 12 | description = "Specify nodejs version to use"; 13 | shell = import ../examples-projects/nodejs-version-shell/shell.nix { }; 14 | command = '' 15 | node -e 'console.log(process.versions.node.split(".")[0]);' 16 | ''; 17 | expected = "10\n"; 18 | }; 19 | pathContainsNodeApplications = { 20 | description = "Applications from the node_modules/.bin folder should be available on $PATH in the shell expression"; 21 | shell = npmlock2nix.v1.shell { src = ../examples-projects/bin-project; }; 22 | command = '' 23 | mkdirp --version 24 | ''; 25 | expected = "1.0.4\n"; 26 | }; 27 | 28 | symlinkNodeModules = 29 | let 30 | shell = npmlock2nix.v1.shell { 31 | src = ../examples-projects/bin-project; 32 | node_modules_mode = "symlink"; 33 | }; 34 | in 35 | { 36 | description = '' 37 | The shell builder supports linking the nix build node_modules folder into 38 | the current working directory via the `shellHook`. Verify taht we are 39 | indeed doing that. 40 | ''; 41 | inherit shell; 42 | command = '' 43 | readlink -f node_modules 44 | ''; 45 | expected = toString (shell.node_modules + "/node_modules\n"); 46 | }; 47 | 48 | symlinkNodeModulesDoesNotOverrideExistingNodeModules = 49 | let 50 | shell = npmlock2nix.v1.shell { 51 | src = ../examples-projects/bin-project; 52 | node_modules_mode = "symlink"; 53 | }; 54 | in 55 | { 56 | description = '' 57 | Ensure the shellHook doesn't override node_modules directory. 58 | ''; 59 | inherit shell; 60 | setup-command = '' 61 | mkdir node_modules 62 | ''; 63 | command = '' 64 | readlink -f node_modules 65 | ''; 66 | status = 1; 67 | expected = ""; 68 | expected-stderr = '' 69 | [npmlock2nix] There is already a `node_modules` directory. Not replacing it. 70 | ''; 71 | }; 72 | 73 | symlinkNodeModulesDoesOverrideExistingNodeModulesWhenInStore = 74 | let 75 | shell = npmlock2nix.v1.shell { 76 | src = ../examples-projects/bin-project; 77 | node_modules_mode = "symlink"; 78 | }; 79 | in 80 | { 81 | description = '' 82 | Ensure the shellHook doesn't override node_modules directory. 83 | ''; 84 | inherit shell; 85 | setup-command = '' 86 | ln -s ${runCommandNoCC "node_modules-fake" { } "mkdir $out; touch $out/.fake"} node_modules 87 | ''; 88 | command = '' 89 | if [ -e node_modules/.fake ]; then 90 | echo "expected the node_modules to be removed" 91 | exit 1 92 | fi 93 | exit 0 94 | ''; 95 | status = 0; 96 | expected = ""; 97 | }; 98 | 99 | 100 | copyNodeModulesDoesNotOverrideExistingNodeModules = 101 | let 102 | shell = npmlock2nix.v1.shell { 103 | src = ../examples-projects/bin-project; 104 | node_modules_mode = "copy"; 105 | }; 106 | in 107 | { 108 | description = '' 109 | Ensure the shellHook doesn't override node_modules directory. 110 | ''; 111 | inherit shell; 112 | setup-command = '' 113 | mkdir node_modules 114 | ''; 115 | command = '' 116 | readlink -f node_modules 117 | ''; 118 | status = 1; 119 | expected = ""; 120 | expected-stderr = '' 121 | [npmlock2nix] There is already a `node_modules` directory. Not replacing it. 122 | ''; 123 | }; 124 | 125 | symlinkNodeModulesCreatesALink = 126 | let 127 | shell = npmlock2nix.v1.shell { 128 | src = ../examples-projects/bin-project; 129 | node_modules_mode = "symlink"; 130 | }; 131 | in 132 | { 133 | description = '' 134 | Ensure the shellHook does create a symlink. 135 | ''; 136 | inherit shell; 137 | command = '' 138 | test -L node_modules || exit 1 139 | ''; 140 | expected = ""; 141 | }; 142 | 143 | copyNodeModulesCreatesANewDirectory = 144 | let 145 | shell = npmlock2nix.v1.shell { 146 | src = ../examples-projects/bin-project; 147 | node_modules_mode = "copy"; 148 | }; 149 | in 150 | { 151 | description = '' 152 | Ensure the shellHook does create a directory. 153 | ''; 154 | inherit shell; 155 | command = '' 156 | test -d node_modules || exit 1 157 | ''; 158 | expected = ""; 159 | }; 160 | 161 | buildInputsDoesntRemoveDefaultValues = 162 | let 163 | shell = npmlock2nix.v1.shell { 164 | src = ../examples-projects/bin-project; 165 | buildInputs = [ python3 ]; 166 | }; 167 | in 168 | { 169 | description = '' 170 | Ensure that providing additional buildInputs doesn't break our default buildInputs (e.g. nodejs). 171 | ''; 172 | inherit shell; 173 | command = '' 174 | node --version > /dev/null || exit 1 175 | python3 --version > /dev/null || exit 1 176 | ''; 177 | expected = ""; 178 | }; 179 | 180 | webpackCli = { 181 | description = '' 182 | We should be able to invoke the webpack(-cli) to build a very simple bootstrap based project 183 | ''; 184 | shell = npmlock2nix.v1.shell { 185 | src = ../examples-projects/webpack-cli-project; 186 | }; 187 | setup-command = '' 188 | cp --no-preserve=mode -r ${testLib.withoutNodeModules ../examples-projects/webpack-cli-project} workspace 189 | cd workspace 190 | ''; 191 | command = '' 192 | webpack-cli --version 193 | if ! webpack --mode production > .webpack.log 2>&1; then 194 | cat .webpack.log 195 | exit 1 196 | fi 197 | if ! test -e dist/main.js; then 198 | echo "dist/main.js missing" 199 | exit 1 200 | fi 201 | if ! test -e dist/index.html; then 202 | echo "dist/index.html missing" 203 | exit 1 204 | fi 205 | ''; 206 | expected = '' 207 | 3.3.12 208 | ''; 209 | }; 210 | 211 | bin-wrapped-dep = { 212 | description = '' 213 | Some packages try to download files into their source folder when they 214 | are missing. 215 | One of these packages is `cwebp-bin` that is based on the `bin-wrapper` 216 | package. The common pattern here is that they check for their binary (in 217 | this case `cwebp`) in the `vendor` directory of the package in the 218 | node_modules folder. 219 | 220 | In this test we are verifying that those node projects can be built and 221 | the binaries are availble during runtime (through node). 222 | 223 | We expect the path returned by nodejs to be a symlink to the actual store path. 224 | ''; 225 | 226 | shell = callPackage ../examples-projects/bin-wrapped-dep/shell.nix { }; 227 | command = '' 228 | readlink -f $(node -e 'console.log(require("cwebp-bin"))') 229 | ''; 230 | 231 | expected = '' 232 | ${libwebp}/bin/cwebp 233 | ''; 234 | }; 235 | 236 | sanitizes-package-names = { 237 | description = '' 238 | packages sometimes use `@` or `/` in package names which has to be sanitized 239 | to avoid nix errors with invalid store paths. 240 | ''; 241 | shell = callPackage ../examples-projects/unsanitized-package-name/shell.nix { }; 242 | command = '' 243 | node -e 'console.log("works")' 244 | ''; 245 | expected = '' 246 | works 247 | ''; 248 | }; 249 | 250 | source-patching = { 251 | description = "Source patching works"; 252 | shell = callPackage ../examples-projects/source-patching/shell.nix { }; 253 | command = '' 254 | node -e 'console.log(require("custom-hello-world")({}));' 255 | ''; 256 | expected = "Hello Nix\n"; 257 | }; 258 | } 259 | -------------------------------------------------------------------------------- /tests/tests-v1/integration-tests/leftpad-shell.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 4 | 5 | nix-shell $DIR/../tests/examples-projects/single-dependency/shell.nix --run "node -e 'require(\"leftpad\")(123, 7);'" 6 | -------------------------------------------------------------------------------- /tests/tests-v1/make-github-source.nix: -------------------------------------------------------------------------------- 1 | { lib, npmlock2nix, testLib }: 2 | let 3 | inherit (testLib) noSourceOptions; 4 | i = npmlock2nix.v1.internal; 5 | 6 | testDependency = { 7 | version = "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 8 | from = "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 9 | }; 10 | in 11 | (testLib.runTests { 12 | testSimpleCase = { 13 | expr = 14 | let 15 | version = (i.makeGithubSource noSourceOptions "leftpad" testDependency).version; 16 | in 17 | lib.hasPrefix "file:///nix/store" version; 18 | expected = true; 19 | }; 20 | 21 | testDropsFrom = { 22 | expr = 23 | let 24 | dep = i.makeGithubSource noSourceOptions "leftpad" testDependency; 25 | in 26 | dep ? from; 27 | expected = false; 28 | }; 29 | }) 30 | -------------------------------------------------------------------------------- /tests/tests-v1/make-source-urls.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib }: 2 | testLib.runTests { 3 | testUrlForDependency = { 4 | expr = npmlock2nix.v1.internal.makeSourceAttrs "test" { 5 | resolved = "https://example.com/package.tgz"; 6 | integrity = "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU="; 7 | }; 8 | expected = { 9 | url = "https://example.com/package.tgz"; 10 | hash = "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU="; 11 | }; 12 | }; 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v1/make-source.nix: -------------------------------------------------------------------------------- 1 | { testLib, npmlock2nix }: 2 | let 3 | i = npmlock2nix.v1.internal; 4 | f = { 5 | sourceHashFunc = builtins.throw "Shouldn't be called"; 6 | nodejs = null; 7 | }; 8 | in 9 | testLib.runTests { 10 | testMakeSourceRegular = { 11 | expr = i.makeSource f "regular" { 12 | resolved = "https://example.com/package.tgz"; 13 | integrity = "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU="; 14 | }; 15 | expected = { 16 | integrity = "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU="; 17 | resolved = "file:///nix/store/rm32fd9z9snwr3i1v0gv6f5fh4abzqf3-package.tgz"; 18 | }; 19 | }; 20 | 21 | testMakeSourceUrlFromVersion = 22 | let 23 | version = "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.4.tgz"; 24 | in 25 | { 26 | expr = i.makeSource f "url-from-version" { 27 | integrity = "sha512-ddaXWILlm1U0Z9qpcZffJjBFZRpz/GxQ1n/Qth3xKvYRUbniuPOgftNTDaxkEC4h04uJG5Ls/OdI1YJUyfuRzQ=="; 28 | inherit version; 29 | }; 30 | expected = { 31 | integrity = "sha512-ddaXWILlm1U0Z9qpcZffJjBFZRpz/GxQ1n/Qth3xKvYRUbniuPOgftNTDaxkEC4h04uJG5Ls/OdI1YJUyfuRzQ=="; 32 | resolved = "file:///nix/store/qn1b7cpsw383kprpzvq4r1x3yis9bczn-olm-3.2.4.tgz"; 33 | inherit version; 34 | }; 35 | }; 36 | } 37 | -------------------------------------------------------------------------------- /tests/tests-v1/node-modules.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, runCommand, nodejs, python3 }: 2 | testLib.runTests { 3 | testNodeModulesForEmptyDependencies = { 4 | expr = 5 | let 6 | drv = npmlock2nix.v1.node_modules { 7 | src = ./examples-projects/no-dependencies; 8 | }; 9 | in 10 | { 11 | inherit (drv) version name; 12 | }; 13 | expected = { 14 | name = "no-dependencies-1.0.0"; 15 | version = "1.0.0"; 16 | }; 17 | }; 18 | 19 | testNodeModulesWithNoVersion = { 20 | expr = 21 | let 22 | drv = npmlock2nix.v1.node_modules { 23 | src = ./examples-projects/no-version; 24 | }; 25 | in 26 | { 27 | inherit (drv) version name; 28 | }; 29 | expected = { 30 | name = "no-version-0"; 31 | version = "0"; 32 | }; 33 | }; 34 | 35 | testNodeModulesForEmptyDependenciesHasNodeModulesFolder = { 36 | expr = 37 | let 38 | drv = npmlock2nix.v1.node_modules { 39 | src = ./examples-projects/no-dependencies; 40 | }; 41 | in 42 | builtins.pathExists (drv + "/node_modules"); 43 | expected = false; 44 | }; 45 | 46 | testNodeModulesForSimpleProjectHasLeftPad = { 47 | expr = 48 | let 49 | drv = npmlock2nix.v1.node_modules { 50 | src = ./examples-projects/single-dependency; 51 | }; 52 | in 53 | builtins.pathExists (drv + "/node_modules/leftpad"); 54 | expected = true; 55 | }; 56 | testNodeModulesForSimpleProjectCanUseLeftPad = { 57 | expr = 58 | let 59 | drv = npmlock2nix.v1.node_modules { 60 | src = ./examples-projects/single-dependency; 61 | }; 62 | in 63 | builtins.pathExists (runCommand "test-leftpad" 64 | { 65 | buildInputs = [ nodejs ]; 66 | } '' 67 | ln -s ${drv}/node_modules node_modules 68 | node -e "require('leftpad')" 69 | touch $out 70 | '' 71 | ); 72 | expected = true; 73 | }; 74 | 75 | testNodeModulesAcceptsCustomNodejs = { 76 | expr = (npmlock2nix.v1.node_modules { 77 | src = ./examples-projects/no-dependencies; 78 | nodejs = { 79 | pname = "our-custom-nodejs-package"; 80 | version = "14.12.34"; 81 | }; 82 | }).nodejs; 83 | expected = { 84 | pname = "our-custom-nodejs-package"; 85 | version = "14.12.34"; 86 | }; 87 | }; 88 | 89 | testNodeModulesPropagatesNodejs = 90 | let 91 | drv = npmlock2nix.v1.node_modules { 92 | src = ./examples-projects/no-dependencies; 93 | nodejs = nodejs; 94 | }; 95 | in 96 | { 97 | expr = drv.propagatedBuildInputs; 98 | expected = [ nodejs ]; 99 | }; 100 | 101 | testHonorsPrePostBuildHook = 102 | let 103 | drv = npmlock2nix.v1.node_modules { 104 | src = ./examples-projects/single-dependency; 105 | preBuild = '' 106 | echo -n "preBuild" > preBuild-test 107 | ''; 108 | postBuild = '' 109 | echo -n "postBuild" > postBuild-test 110 | mv *Build-test node_modules 111 | ''; 112 | }; 113 | in 114 | { 115 | expr = builtins.readFile (runCommand "concat" 116 | { } '' 117 | cat ${drv + "/node_modules/preBuild-test"} ${drv + "/node_modules/postBuild-test"} > $out 118 | '' 119 | ); 120 | expected = "preBuildpostBuild"; 121 | }; 122 | 123 | testBuildsNativeExtensions = 124 | let 125 | drv = npmlock2nix.v1.node_modules { 126 | src = ./examples-projects/native-extensions; 127 | buildInputs = [ python3 ]; 128 | }; 129 | in 130 | { 131 | expr = builtins.pathExists drv.outPath; 132 | expected = true; 133 | }; 134 | 135 | testPassesExtraParameters = { 136 | expr = (npmlock2nix.v1.node_modules { 137 | src = ./examples-projects/single-dependency; 138 | SOME_EXTRA_PARAMETER = "123"; 139 | }).SOME_EXTRA_PARAMETER or "attribute missing"; 140 | expected = "123"; 141 | }; 142 | 143 | testHonorsPassedPassthru = { 144 | expr = (npmlock2nix.v1.node_modules { 145 | src = ./examples-projects/single-dependency; 146 | passthru.test-param = 123; 147 | }).passthru.test-param; 148 | expected = 123; 149 | }; 150 | 151 | testVersionAsResolvedUrl = 152 | let 153 | drv = npmlock2nix.v1.node_modules { 154 | src = ./examples-projects/url-as-version; 155 | }; 156 | in 157 | { 158 | expr = builtins.pathExists drv.outPath; 159 | expected = true; 160 | }; 161 | } 162 | -------------------------------------------------------------------------------- /tests/tests-v1/parse-github-ref.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib }: 2 | let 3 | i = npmlock2nix.v1.internal; 4 | in 5 | (testLib.runTests { 6 | testParsesARef = 7 | { 8 | expr = i.parseGitHubRef "github:foo/bar#939360f9d1bafa9019b6ff8739495c6c9101c4a1"; 9 | expected = { 10 | parts = [ "github" [ ] "foo" [ ] "bar" [ ] "939360f9d1bafa9019b6ff8739495c6c9101c4a1" ]; 11 | org = "foo"; 12 | repo = "bar"; 13 | rev = "939360f9d1bafa9019b6ff8739495c6c9101c4a1"; 14 | }; 15 | }; 16 | 17 | # It would be nice if there was a way to test the failure 18 | # case but unfortunately we can't catch exceptions... 19 | }) 20 | -------------------------------------------------------------------------------- /tests/tests-v1/patch-lockfile.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, lib }: 2 | let 3 | inherit (testLib) noSourceOptions; 4 | in 5 | testLib.runTests { 6 | 7 | testPatchDependencyHandlesGitHubRefsInRequires = { 8 | expr = 9 | let 10 | libxmljsUrl = (npmlock2nix.v1.internal.patchDependency [ ] noSourceOptions "test" { 11 | version = "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 12 | from = "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 13 | integrity = "sha512-8/UvHFG90J4O4QNRzb0jB5Ni1QuvuB7XFTLfDMQnCzAsFemF29VKnNGUESFFcSP/r5WWh/PMe0YRz90+3IqsUA=="; 14 | requires = { 15 | libxmljs = "github:znerol/libxmljs#0517e063347ea2532c9fdf38dc47878c628bf0ae"; 16 | }; 17 | } 18 | ).result.requires.libxmljs; 19 | in 20 | lib.hasPrefix builtins.storeDir libxmljsUrl; 21 | expected = true; 22 | }; 23 | 24 | testBundledDependenciesAreRetained = { 25 | expr = (npmlock2nix.v1.internal.patchDependency [ ] noSourceOptions "test" { 26 | bundled = true; 27 | integrity = "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU="; 28 | something = "bar"; 29 | dependencies = { }; 30 | }).result; 31 | expected = { 32 | bundled = true; 33 | integrity = "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU="; 34 | something = "bar"; 35 | dependencies = { }; 36 | }; 37 | }; 38 | 39 | testPatchLockfileWithoutDependencies = { 40 | expr = (npmlock2nix.v1.internal.patchLockfile noSourceOptions ./examples-projects/no-dependencies/package-lock.json).result.dependencies; 41 | expected = { }; 42 | }; 43 | 44 | testPatchDependencyDoesntDropAttributes = { 45 | expr = (npmlock2nix.v1.internal.patchDependency [ ] noSourceOptions "test" { 46 | a = 1; 47 | foo = "something"; 48 | resolved = "https://examples.com/something.tgz"; 49 | integrity = "sha1-00000000000000000000000+0RU="; 50 | dependencies = { }; 51 | }).result; 52 | expected = { 53 | a = 1; 54 | foo = "something"; 55 | resolved = "file:///nix/store/k2rgngn9cmhz4g3kzxmvhx5r40qvnwcf-something.tgz"; 56 | integrity = "sha1-00000000000000000000000+0RU="; 57 | dependencies = { }; 58 | }; 59 | }; 60 | 61 | testPatchDependencyPatchesDependenciesRecursively = { 62 | expr = (npmlock2nix.v1.internal.patchDependency [ ] noSourceOptions "test" { 63 | a = 1; 64 | foo = "something"; 65 | resolved = "https://examples.com/something.tgz"; 66 | integrity = "sha1-00000000000000000000000+0RU="; 67 | dependencies.a = { 68 | resolved = "https://examples.com/somethingelse.tgz"; 69 | integrity = "sha1-00000000000000000000000+00U="; 70 | }; 71 | }).result; 72 | 73 | expected = { 74 | a = 1; 75 | foo = "something"; 76 | resolved = "file:///nix/store/k2rgngn9cmhz4g3kzxmvhx5r40qvnwcf-something.tgz"; 77 | integrity = "sha1-00000000000000000000000+0RU="; 78 | dependencies.a = { 79 | resolved = "file:///nix/store/1cf0n1xb5pad8ib3xyzbzzddfknfxvkc-somethingelse.tgz"; 80 | integrity = "sha1-00000000000000000000000+00U="; 81 | }; 82 | }; 83 | }; 84 | 85 | testPatchLockfileTurnsUrlsIntoStorePaths = { 86 | expr = 87 | let 88 | deps = (npmlock2nix.v1.internal.patchLockfile noSourceOptions ./examples-projects/single-dependency/package-lock.json).result.dependencies; 89 | in 90 | lib.count (dep: lib.hasPrefix "file:///nix/store/" dep.resolved) (lib.attrValues deps); 91 | expected = 1; 92 | }; 93 | 94 | testPatchLockfileTurnsGitHubUrlsIntoStorePaths = { 95 | expr = 96 | let 97 | leftpad = (npmlock2nix.v1.internal.patchLockfile noSourceOptions ./examples-projects/github-dependency/package-lock.json).result.dependencies.leftpad; 98 | in 99 | lib.hasPrefix ("file://" + builtins.storeDir) leftpad.version; 100 | expected = true; 101 | }; 102 | 103 | testConvertPatchedLockfileToJSON = { 104 | expr = builtins.typeOf (builtins.toJSON (npmlock2nix.v1.internal.patchLockfile noSourceOptions ./examples-projects/nested-dependencies/package-lock.json).result) == "string"; 105 | expected = true; 106 | }; 107 | 108 | testPatchedLockFile = { 109 | expr = testLib.hashFile (npmlock2nix.v1.internal.patchedLockfile noSourceOptions ./examples-projects/nested-dependencies/package-lock.json).result; 110 | expected = "980323c3a53d86ab6886f21882936cfe7c06ac633993f16431d79e3185084414"; 111 | }; 112 | 113 | } 114 | -------------------------------------------------------------------------------- /tests/tests-v1/patch-packagefile.nix: -------------------------------------------------------------------------------- 1 | { lib, npmlock2nix, testLib }: 2 | let 3 | i = npmlock2nix.v1.internal; 4 | in 5 | (testLib.runTests { 6 | testTurnsGitHubRefsToWildcards = { 7 | expr = (npmlock2nix.v1.internal.patchPackagefile ./examples-projects/github-dependency/package.json).dependencies.leftpad; 8 | expected = "*"; 9 | }; 10 | testHandlesBranches = { 11 | expr = (npmlock2nix.v1.internal.patchPackagefile ./examples-projects/github-dependency-branch/package.json).dependencies.leftpad; 12 | expected = "*"; 13 | }; 14 | testHandlesDevDependencies = { 15 | expr = (npmlock2nix.v1.internal.patchPackagefile ./examples-projects/github-dev-dependency/package.json).devDependencies.leftpad; 16 | expected = "*"; 17 | }; 18 | }) 19 | -------------------------------------------------------------------------------- /tests/tests-v1/read-lockfile/default.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib }: 2 | let 3 | i = npmlock2nix.v1.internal; 4 | in 5 | (testLib.runTests { 6 | testParsesSimpleLockfile = 7 | { 8 | expr = i.readLockfile ./simple.json; 9 | expected = { 10 | name = "simple"; 11 | version = "1.0.0"; 12 | lockfileVersion = 1; 13 | requires = true; 14 | dependencies = { 15 | leftpad = { 16 | version = "0.0.1"; 17 | resolved = "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz"; 18 | integrity = "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU="; 19 | }; 20 | }; 21 | }; 22 | }; 23 | 24 | # parse a lockfile that doesn't have dependencies so we can test that we 25 | # always have at least an empty attribute set of dependencies. 26 | testParsesLockfileWithoutDependencies = 27 | { 28 | expr = i.readLockfile ../examples-projects/no-dependencies/package-lock.json; 29 | expected = { 30 | name = "no-dependencies"; 31 | version = "1.0.0"; 32 | lockfileVersion = 1; 33 | dependencies = { }; 34 | }; 35 | }; 36 | }) 37 | -------------------------------------------------------------------------------- /tests/tests-v1/read-lockfile/empty.json: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nix-community/npmlock2nix/9197bbf397d76059a76310523d45df10d2e4ca81/tests/tests-v1/read-lockfile/empty.json -------------------------------------------------------------------------------- /tests/tests-v1/read-lockfile/simple.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "leftpad": { 8 | "version": "0.0.1", 9 | "resolved": "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz", 10 | "integrity": "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU=" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v1/shell.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, symlinkJoin, runCommand, nodejs, lib }: 2 | testLib.runTests { 3 | # test that the shell expression uses the same (given) nodejs package for 4 | # both the shell and node_modules 5 | testUsesGivenNodeJSPackage = 6 | let 7 | custom_nodejs = symlinkJoin { 8 | name = "custom-nodejs"; 9 | paths = [ nodejs ]; 10 | version = "12.8.3"; 11 | src = "/foo"; 12 | }; 13 | drv = npmlock2nix.v1.shell { 14 | src = ./examples-projects/single-dependency; 15 | nodejs = custom_nodejs; 16 | }; 17 | in 18 | { 19 | expr = { 20 | inherit (drv) buildInputs; 21 | node_modules_nodejs = drv.node_modules.nodejs; 22 | }; 23 | expected = { 24 | buildInputs = [ custom_nodejs drv.node_modules ]; 25 | node_modules_nodejs = custom_nodejs; 26 | }; 27 | }; 28 | 29 | # test that we are passing the pre- & postBuild attributes to node_modules 30 | testPassPreBuildAttributeToNodeModules = 31 | let 32 | drv = npmlock2nix.v1.shell { 33 | src = ./examples-projects/single-dependency; 34 | node_modules_attrs.preBuild = "foobar in preBuild"; 35 | }; 36 | in 37 | { 38 | expr = drv.node_modules.preBuild; 39 | expected = "foobar in preBuild"; 40 | }; 41 | 42 | testPassPostBuildAttributeToNodeModules = 43 | let 44 | drv = npmlock2nix.v1.shell { 45 | src = ./examples-projects/single-dependency; 46 | node_modules_attrs.postBuild = "foobar in postBuild"; 47 | }; 48 | in 49 | { 50 | expr = drv.node_modules.postBuild; 51 | expected = "foobar in postBuild"; 52 | }; 53 | 54 | testPassthruIsHonored = 55 | let 56 | drv = npmlock2nix.v1.shell { 57 | src = ./examples-projects/single-dependency; 58 | passthru.test-attribute = 123; 59 | }; 60 | in 61 | { 62 | expr = { 63 | inherit (drv.passthru) test-attribute; 64 | has_node_modules = drv.passthru ? node_modules; 65 | }; 66 | expected = { 67 | test-attribute = 123; 68 | has_node_modules = true; 69 | }; 70 | }; 71 | 72 | testShellHookIsHonored = 73 | let 74 | drv = npmlock2nix.v1.shell { 75 | src = ./examples-projects/single-dependency; 76 | shellHook = "magic-string"; 77 | }; 78 | in 79 | { 80 | # the shellHook should now contain a bunch of lines (for setting up the node_modules symlink / copy) and the given line. Our line must be on a line of its own. 81 | expr = 82 | let 83 | lines = lib.splitString "\n" drv.shellHook; 84 | more_than_one_line = (builtins.length lines) > 1; 85 | last_line = builtins.head (lib.reverseList lines); 86 | in 87 | { 88 | inherit more_than_one_line last_line; 89 | }; 90 | expected = { 91 | more_than_one_line = true; 92 | last_line = "magic-string"; 93 | }; 94 | }; 95 | 96 | } 97 | -------------------------------------------------------------------------------- /tests/tests-v1/source-hash-func.nix: -------------------------------------------------------------------------------- 1 | { lib, npmlock2nix, testLib }: 2 | let 3 | i = npmlock2nix.v1.internal; 4 | 5 | testGitHubMap = { 6 | foo-org.foo-repo."db1442a0556c2b133627ffebf455a78a1ced64b9" = "github-repo-hash"; 7 | }; 8 | testSpec = { 9 | type = "github"; 10 | value = { 11 | org = "foo-org"; 12 | repo = "foo-repo"; 13 | rev = "db1442a0556c2b133627ffebf455a78a1ced64b9"; 14 | }; 15 | }; 16 | in 17 | (testLib.runTests { 18 | testSimpleCase = { 19 | expr = i.sourceHashFunc testGitHubMap testSpec; 20 | expected = "github-repo-hash"; 21 | }; 22 | }) 23 | -------------------------------------------------------------------------------- /tests/tests-v2/build-tests.nix: -------------------------------------------------------------------------------- 1 | { lib, symlinkJoin, npmlock2nix, runCommand, libwebp, python3 }: 2 | let 3 | symlinkAttrs = attrs: runCommand "symlink-attrs" 4 | { } 5 | ( 6 | let 7 | drvs = lib.attrValues (lib.mapAttrs (name: drv: { inherit name drv; }) attrs); 8 | in 9 | '' 10 | mkdir $out 11 | ${lib.concatMapStringsSep "\n" (o: "ln -s ${o.drv} $out/${o.name}") drvs} 12 | '' 13 | ); 14 | in 15 | symlinkAttrs { 16 | webpack-cli-project-default-build-command = npmlock2nix.v2.build { 17 | src = ./examples-projects/webpack-cli-project; 18 | installPhase = '' 19 | cp -r dist $out 20 | ''; 21 | }; 22 | 23 | webpack-cli-project-custom-build-command = npmlock2nix.v2.build { 24 | src = ./examples-projects/webpack-cli-project; 25 | buildCommands = [ "webpack --mode=production" ]; 26 | installPhase = '' 27 | cp -r dist $out 28 | ''; 29 | }; 30 | 31 | node-modules-attributes-are-passed-through = npmlock2nix.v2.build { 32 | src = ./examples-projects/bin-wrapped-dep; 33 | buildCommands = [ 34 | '' 35 | readlink -f $(node -e "console.log(require('cwebp-bin'))") > actual 36 | echo ${libwebp}/bin/cwebp > expected 37 | '' 38 | ]; 39 | installPhase = '' 40 | cp actual $out 41 | ''; 42 | 43 | doCheck = true; 44 | checkPhase = '' 45 | cmp actual expected || exit 1 46 | ''; 47 | 48 | node_modules_attrs = { 49 | sourceOverrides = { 50 | cwebp-bin = sourceInfo: drv: drv.overrideAttrs (old: { 51 | postPatch = '' 52 | mkdir -p vendor 53 | ln -sf "${libwebp}/bin/cwebp" vendor/cwebp 54 | ''; 55 | }); 56 | }; 57 | }; 58 | }; 59 | 60 | passsing-buildInputs-doesnt-break-the-build = npmlock2nix.v2.build { 61 | src = ./examples-projects/webpack-cli-project; 62 | installPhase = '' 63 | cp -r dist $out 64 | ''; 65 | 66 | buildInputs = [ python3 ]; 67 | }; 68 | } 69 | -------------------------------------------------------------------------------- /tests/tests-v2/build.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib }: 2 | testLib.runTests { 3 | testPassthruIsHonored = 4 | let 5 | drv = npmlock2nix.v2.build { 6 | src = ./examples-projects/single-dependency; 7 | installPhase = " 8 | # should never run as we only test eval here 9 | exit 123 10 | "; 11 | passthru.test-attr = 123; 12 | }; 13 | in 14 | { 15 | expr = { 16 | inherit (drv.passthru) test-attr; 17 | has_node_modules = drv.passthru ? node_modules; 18 | }; 19 | expected = { 20 | test-attr = 123; 21 | has_node_modules = true; 22 | }; 23 | }; 24 | } 25 | -------------------------------------------------------------------------------- /tests/tests-v2/default.nix: -------------------------------------------------------------------------------- 1 | { callPackage }: 2 | { 3 | build = callPackage ./build.nix { }; 4 | build-tests = callPackage ./build-tests.nix { }; 5 | in-bundle-dep-test = callPackage ./examples-projects/in-bundle-dependency { }; 6 | bundle-shebang-tests = callPackage ./examples-projects/bundled-dep-require-patch-shebang { }; 7 | integration-tests = callPackage ./integration-tests { }; 8 | patch-package = callPackage ./patch-package.nix { }; 9 | make-url-source = callPackage ./make-url-source.nix { }; 10 | node-modules = callPackage ./node-modules.nix { }; 11 | parse-github-ref = callPackage ./parse-github-ref.nix { }; 12 | patch-packagefile = callPackage ./patch-packagefile.nix { }; 13 | read-lockfile = callPackage ./read-lockfile { }; 14 | shell = callPackage ./shell.nix { }; 15 | source-hash-func = callPackage ./source-hash-func.nix { }; 16 | } 17 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/bin-project/index.js: -------------------------------------------------------------------------------- 1 | console.log("hello world"); 2 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/bin-project/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bin-project", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "bin-project", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "mkdirp": "^1.0.4" 13 | } 14 | }, 15 | "node_modules/mkdirp": { 16 | "version": "1.0.4", 17 | "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", 18 | "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", 19 | "bin": { 20 | "mkdirp": "bin/cmd.js" 21 | }, 22 | "engines": { 23 | "node": ">=10" 24 | } 25 | } 26 | }, 27 | "dependencies": { 28 | "mkdirp": { 29 | "version": "1.0.4", 30 | "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", 31 | "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==" 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/bin-project/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bin-project", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "mkdirp": "^1.0.4" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/bin-project/shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | let 3 | node = pkgs.nodejs-14_x; 4 | in 5 | # We need make sure that `nodejs` does not default to `nodejs-10_x` because 6 | # then our test cannot ensure that we can override the default. If the assert 7 | # below throws, change `node` above to a different version. 8 | assert pkgs.nodejs == node -> throw "`nodejs` is refering to `nodejs-10_x` rendering this test ineffective."; 9 | pkgs.npmlock2nix.v2.shell { 10 | src = ./.; 11 | nodejs = node; 12 | } 13 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/bin-wrapped-dep/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bin-wrapped-dep", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "cwebp-bin": "5.1.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/bin-wrapped-dep/shell.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, libwebp, python3 }: 2 | npmlock2nix.v2.shell { 3 | src = ./.; 4 | node_modules_attrs = { 5 | buildInputs = [ 6 | python3 # for node-gyp 7 | libwebp # cwebp-bin 8 | ]; 9 | 10 | sourceOverrides = { 11 | cwebp-bin = sourceInfo: drv: drv.overrideAttrs (old: { 12 | postPatch = '' 13 | mkdir -p vendor 14 | ln -sf "${libwebp}/bin/cwebp" vendor/cwebp 15 | ''; 16 | }); 17 | }; 18 | }; 19 | } 20 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/bundled-dep-require-patch-shebang/default.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, runCommand, nodejs-16_x, nodejs-17_x, python3 }: 2 | npmlock2nix.v2.node_modules { 3 | src = ./.; 4 | sourceOverrides = with npmlock2nix.v2; { 5 | "ganache" = packageRequirePatchShebangs; 6 | }; 7 | postCheck = '' 8 | echo "[+] Checking if the bundled binary shebang has been correctly patched." 9 | if cat "node_modules/ganache/node_modules/node-gyp-build/bin.js" | head -1 | grep -c "/usr/bin/env" 10 | then 11 | echo "ERROR: the bundled node-gyp-build/bin.js file's shebang has not been patched." 12 | exit 1 13 | else 14 | echo "[+] Ok" 15 | fi 16 | ''; 17 | } 18 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/bundled-dep-require-patch-shebang/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bundled-dep-require-patch-shebang", 3 | "version": "0.0.0", 4 | "description": "", 5 | "devDependencies": { 6 | "ganache": "^7.4.3" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/github-dependency-branch/default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | 3 | pkgs.npmlock2nix.v2.node_modules { 4 | src = ./.; 5 | packageJson = ./package.json; 6 | packageLockJson = ./package-lock.json; 7 | githubSourceHashMap = { 8 | tmcw.leftpad.db1442a0556c2b133627ffebf455a78a1ced64b9 = "1zyy1nxbby4wcl30rc8fsis1c3f7nafavnwd3qi4bg0x00gxjdnh"; 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/github-dependency-branch/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "github-dependency", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "github:tmcw/leftpad#v0.0.1" 13 | } 14 | }, 15 | "node_modules/leftpad": { 16 | "version": "0.0.1", 17 | "resolved": "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9", 18 | "license": "BSD-3-Clause" 19 | } 20 | }, 21 | "dependencies": { 22 | "leftpad": { 23 | "version": "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9", 24 | "from": "leftpad@github:tmcw/leftpad#v0.0.1" 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/github-dependency-branch/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "github:tmcw/leftpad#v0.0.1" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/github-dependency/default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../../nix { } }: 2 | 3 | pkgs.npmlock2nix.v2.node_modules { 4 | src = ./.; 5 | packageJson = ./package.json; 6 | packageLockJson = ./package-lock.json; 7 | githubSourceHashMap = { 8 | tmcw.leftpad.db1442a0556c2b133627ffebf455a78a1ced64b9 = "1zyy1nxbby4wcl30rc8fsis1c3f7nafavnwd3qi4bg0x00gxjdnh"; 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/github-dependency/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "github-dependency", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 13 | } 14 | }, 15 | "node_modules/leftpad": { 16 | "version": "0.0.1", 17 | "resolved": "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9", 18 | "integrity": "sha512-8NCRwDs07XJJnyO7d6fVbrKjpW1nkbH0dFH5v2/U7md4+4y2hL8+S9OpRqY54W22Dq45yFKuVUYmLlrjGeFLOw==", 19 | "license": "BSD-3-Clause" 20 | } 21 | }, 22 | "dependencies": { 23 | "leftpad": { 24 | "version": "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9", 25 | "integrity": "sha512-8NCRwDs07XJJnyO7d6fVbrKjpW1nkbH0dFH5v2/U7md4+4y2hL8+S9OpRqY54W22Dq45yFKuVUYmLlrjGeFLOw==", 26 | "from": "leftpad@github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/github-dependency/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/github-dev-dependency/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "github-dependency", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "devDependencies": { 12 | "leftpad": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 13 | } 14 | }, 15 | "node_modules/leftpad": { 16 | "version": "0.0.1", 17 | "resolved": "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9", 18 | "integrity": "sha512-8NCRwDs07XJJnyO7d6fVbrKjpW1nkbH0dFH5v2/U7md4+4y2hL8+S9OpRqY54W22Dq45yFKuVUYmLlrjGeFLOw==", 19 | "dev": true, 20 | "license": "BSD-3-Clause" 21 | } 22 | }, 23 | "dependencies": { 24 | "leftpad": { 25 | "version": "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9", 26 | "integrity": "sha512-8NCRwDs07XJJnyO7d6fVbrKjpW1nkbH0dFH5v2/U7md4+4y2hL8+S9OpRqY54W22Dq45yFKuVUYmLlrjGeFLOw==", 27 | "dev": true, 28 | "from": "leftpad@github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/github-dev-dependency/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "github-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "devDependencies": { 12 | "leftpad": "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/in-bundle-dependency/default.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, runCommand, nodejs-16_x, nodejs-17_x, python3 }: 2 | npmlock2nix.v2.node_modules { 3 | src = ./.; 4 | } 5 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/in-bundle-dependency/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "in-bundle-dependency", 3 | "version": "1.0.0", 4 | "scripts": { 5 | }, 6 | "devDependencies": { 7 | }, 8 | "dependencies": { 9 | "vega-embed": "6.20.8" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/native-extensions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "native-extensions", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "bcrypt": "^5.0.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/nested-dependencies/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nested-dependencies", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "nested-dependencies", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "rxjs": "^6.6.0", 13 | "tslib": "=1.7.0" 14 | } 15 | }, 16 | "node_modules/rxjs": { 17 | "version": "6.6.7", 18 | "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.7.tgz", 19 | "integrity": "sha512-hTdwr+7yYNIT5n4AMYp85KA6yw2Va0FLa3Rguvbpa4W3I5xynaBZo41cM3XM+4Q6fRMj3sBYIR1VAmZMXYJvRQ==", 20 | "dependencies": { 21 | "tslib": "^1.9.0" 22 | }, 23 | "engines": { 24 | "npm": ">=2.0.0" 25 | } 26 | }, 27 | "node_modules/rxjs/node_modules/tslib": { 28 | "version": "1.14.1", 29 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", 30 | "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" 31 | }, 32 | "node_modules/tslib": { 33 | "version": "1.7.0", 34 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.7.0.tgz", 35 | "integrity": "sha512-w53V7tqqsLjhlMYrjZQOdT38bslIXQ1xEl1j0AY+myeEzZ2EtNdw0o2C21Wpgnfb2j8nNzun33zUuRzGeoW1ng==" 36 | } 37 | }, 38 | "dependencies": { 39 | "rxjs": { 40 | "version": "6.6.7", 41 | "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.7.tgz", 42 | "integrity": "sha512-hTdwr+7yYNIT5n4AMYp85KA6yw2Va0FLa3Rguvbpa4W3I5xynaBZo41cM3XM+4Q6fRMj3sBYIR1VAmZMXYJvRQ==", 43 | "requires": { 44 | "tslib": "^1.9.0" 45 | }, 46 | "dependencies": { 47 | "tslib": { 48 | "version": "1.14.1", 49 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", 50 | "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" 51 | } 52 | } 53 | }, 54 | "tslib": { 55 | "version": "1.7.0", 56 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.7.0.tgz", 57 | "integrity": "sha512-w53V7tqqsLjhlMYrjZQOdT38bslIXQ1xEl1j0AY+myeEzZ2EtNdw0o2C21Wpgnfb2j8nNzun33zUuRzGeoW1ng==" 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/nested-dependencies/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nested-dependencies", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "rxjs": "^6.6.0", 13 | "tslib": "=1.7.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/no-dependencies/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "no-dependencies", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "no-dependencies", 9 | "version": "1.0.0", 10 | "license": "ISC" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/no-dependencies/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "no-dependencies", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC" 11 | } 12 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/no-version/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "no-version", 3 | "private": true, 4 | "scripts": { 5 | "dev": "next dev", 6 | "build": "next build", 7 | "start": "next start", 8 | "lint": "next lint" 9 | }, 10 | "dependencies": { 11 | "@emotion/react": "^11.7.1", 12 | "@emotion/styled": "^11.6.0", 13 | "@mui/icons-material": "^5.4.1", 14 | "@mui/material": "^5.4.0", 15 | "axios": "^0.25.0", 16 | "formik": "^2.2.9", 17 | "next": "12.0.10", 18 | "next-auth": "^4.2.1", 19 | "react": "17.0.2", 20 | "react-dom": "17.0.2", 21 | "react-toast-notifications": "^2.5.1", 22 | "swr": "^1.2.1" 23 | }, 24 | "devDependencies": { 25 | "@types/lodash": "^4.14.178", 26 | "@types/next-auth": "^3.15.0", 27 | "@types/node": "17.0.15", 28 | "@types/react": "17.0.39", 29 | "@types/yup": "^0.29.13", 30 | "eslint": "8.8.0", 31 | "eslint-config-next": "12.0.10", 32 | "restapify": "^2.3.4", 33 | "typescript": "4.5.5" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/nodejs-version-shell/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "single-dependency", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "0.0.1" 13 | } 14 | }, 15 | "node_modules/leftpad": { 16 | "version": "0.0.1", 17 | "resolved": "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz", 18 | "integrity": "sha512-kBAuxBQJlJ85LDc+SnGSX6gWJnJR9Qk4lbgXmz/qPfCOCieCk7BgoN3YvzoNr5BUjqxQDOQxawJJvXXd6c+6Mg==", 19 | "deprecated": "Use the built-in String.padStart function instead" 20 | } 21 | }, 22 | "dependencies": { 23 | "leftpad": { 24 | "version": "0.0.1", 25 | "resolved": "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz", 26 | "integrity": "sha512-kBAuxBQJlJ85LDc+SnGSX6gWJnJR9Qk4lbgXmz/qPfCOCieCk7BgoN3YvzoNr5BUjqxQDOQxawJJvXXd6c+6Mg==" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/nodejs-version-shell/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "0.0.1" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/nodejs-version-shell/shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../../nix { } }: 2 | let 3 | node = pkgs.nodejs-17_x; 4 | in 5 | # We need make sure that `nodejs` does not default to `nodejs-14_x` because 6 | # then our test cannot ensure that we can override the default. If the assert 7 | # below throws, change `node` above to a different version. 8 | assert pkgs.nodejs == node -> throw "`pkgs.nodejs` is refering to `nodejs-17_x` rendering this test ineffective."; 9 | pkgs.npmlock2nix.v2.shell { 10 | src = ./.; 11 | nodejs = node; 12 | } 13 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/single-dependency/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-dependency", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "single-dependency", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "0.0.1" 13 | } 14 | }, 15 | "node_modules/leftpad": { 16 | "version": "0.0.1", 17 | "resolved": "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz", 18 | "integrity": "sha512-kBAuxBQJlJ85LDc+SnGSX6gWJnJR9Qk4lbgXmz/qPfCOCieCk7BgoN3YvzoNr5BUjqxQDOQxawJJvXXd6c+6Mg==", 19 | "deprecated": "Use the built-in String.padStart function instead" 20 | } 21 | }, 22 | "dependencies": { 23 | "leftpad": { 24 | "version": "0.0.1", 25 | "resolved": "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz", 26 | "integrity": "sha512-kBAuxBQJlJ85LDc+SnGSX6gWJnJR9Qk4lbgXmz/qPfCOCieCk7BgoN3YvzoNr5BUjqxQDOQxawJJvXXd6c+6Mg==" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/single-dependency/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "single-dependency", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "leftpad": "0.0.1" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/single-dependency/shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | pkgs.npmlock2nix.v2.shell { 3 | src = ./.; 4 | } 5 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/source-patching/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "source-patching", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "source-patching", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "custom-hello-world": "^1.0.0" 13 | } 14 | }, 15 | "node_modules/custom-hello-world": { 16 | "version": "1.0.3", 17 | "resolved": "https://registry.npmjs.org/custom-hello-world/-/custom-hello-world-1.0.3.tgz", 18 | "integrity": "sha512-Rinkq1q+uLmgbDLZRNZrlyeK3G9e+o0gbVy6r948kySbau0ymwOPNybu4iIkb4R1gE5aZsQUHgtfCg2oZ4zfbw==" 19 | } 20 | }, 21 | "dependencies": { 22 | "custom-hello-world": { 23 | "version": "1.0.3", 24 | "resolved": "https://registry.npmjs.org/custom-hello-world/-/custom-hello-world-1.0.3.tgz", 25 | "integrity": "sha512-Rinkq1q+uLmgbDLZRNZrlyeK3G9e+o0gbVy6r948kySbau0ymwOPNybu4iIkb4R1gE5aZsQUHgtfCg2oZ4zfbw==" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/source-patching/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "source-patching", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "author": "", 7 | "license": "ISC", 8 | "dependencies": { 9 | "custom-hello-world": "^1.0.0" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/source-patching/shell.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix }: 2 | npmlock2nix.v2.shell { 3 | src = ./.; 4 | node_modules_attrs = { 5 | sourceOverrides = { 6 | custom-hello-world = sourceInfo: drv: drv.overrideAttrs (old: { 7 | patches = builtins.toFile "custom-hello-world.patch" '' 8 | diff --git a/lib/index.js b/lib/index.js 9 | index 1f66513..64391a7 100644 10 | --- a/lib/index.js 11 | +++ b/lib/index.js 12 | @@ -21,7 +21,7 @@ function generateHelloWorld({ comma, exclamation, lowercase }) { 13 | if (comma) 14 | helloWorldStr += ','; 15 | 16 | - helloWorldStr += ' World'; 17 | + helloWorldStr += ' Nix'; 18 | 19 | if (exclamation) 20 | helloWorldStr += '!'; 21 | ''; 22 | }); 23 | }; 24 | }; 25 | } 26 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/unsanitized-package-name/default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | pkgs.npmlock2nix.v2.build { 3 | src = ./.; 4 | buildCommands = [ "echo BUILDING" ]; 5 | installPhase = "mkdir $out"; 6 | } 7 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/unsanitized-package-name/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@foo/bar", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "@foo/bar", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "left-pad": "^1.3.0" 13 | } 14 | }, 15 | "node_modules/left-pad": { 16 | "version": "1.3.0", 17 | "resolved": "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz", 18 | "integrity": "sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==", 19 | "deprecated": "use String.prototype.padStart()" 20 | } 21 | }, 22 | "dependencies": { 23 | "left-pad": { 24 | "version": "1.3.0", 25 | "resolved": "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz", 26 | "integrity": "sha512-XI5MPzVNApjAyhQzphX8BkmKsKUxD4LdyK24iZeQGinBN9yTQT3bFlCBy/aVx2HrNcqQGsdot8ghrjyrvMCoEA==" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/unsanitized-package-name/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@foo/bar", 3 | "version": "1.0.0", 4 | "description": "foo with bar", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "left-pad": "^1.3.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/unsanitized-package-name/shell.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? import ../../../nix { } }: 2 | pkgs.npmlock2nix.v2.shell { 3 | src = ./.; 4 | } 5 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/url-as-version/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "url-as-version", 3 | "version": "1.0.0", 4 | "lockfileVersion": 2, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "url-as-version", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.4.tgz" 13 | } 14 | }, 15 | "node_modules/@matrix-org/olm": { 16 | "version": "3.2.4", 17 | "resolved": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.4.tgz", 18 | "integrity": "sha512-ddaXWILlm1U0Z9qpcZffJjBFZRpz/GxQ1n/Qth3xKvYRUbniuPOgftNTDaxkEC4h04uJG5Ls/OdI1YJUyfuRzQ==", 19 | "license": "Apache-2.0" 20 | } 21 | }, 22 | "dependencies": { 23 | "@matrix-org/olm": { 24 | "version": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.4.tgz", 25 | "integrity": "sha512-ddaXWILlm1U0Z9qpcZffJjBFZRpz/GxQ1n/Qth3xKvYRUbniuPOgftNTDaxkEC4h04uJG5Ls/OdI1YJUyfuRzQ==" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/url-as-version/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "url-as-version", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.4.tgz" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/webpack-cli-project/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-cli-project", 3 | "version": "1.0.0", 4 | "description": "", 5 | "license": "ISC", 6 | "main": "index.js", 7 | "scripts": { 8 | "build": "webpack --mode=production" 9 | }, 10 | "dependencies": { 11 | "bootstrap": "^4.5.0", 12 | "html-webpack-plugin": "^4.3.0", 13 | "jquery": "^3.5.1", 14 | "popper.js": "^1.16.1", 15 | "webpack": "^4.44.0", 16 | "webpack-cli": "^3.3.12" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/webpack-cli-project/src/index.js: -------------------------------------------------------------------------------- 1 | import 'bootstrap' 2 | -------------------------------------------------------------------------------- /tests/tests-v2/examples-projects/webpack-cli-project/webpack.config.js: -------------------------------------------------------------------------------- 1 | const HtmlWebpackPlugin = require("html-webpack-plugin"); 2 | // const path = require('path'); 3 | 4 | module.exports = { 5 | plugins: [ 6 | new HtmlWebpackPlugin({ 7 | title: "Webpack Output", 8 | }), 9 | ], 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /tests/tests-v2/integration-tests/default.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, callPackage, libwebp, runCommandNoCC, python3 }: 2 | testLib.makeIntegrationTests { 3 | leftpad = { 4 | description = "Require a node dependency inside the shell environment"; 5 | shell = npmlock2nix.v2.shell { src = ../examples-projects/single-dependency; }; 6 | command = '' 7 | node -e 'console.log(require("leftpad")(123, 7));' 8 | ''; 9 | expected = "0000123\n"; 10 | }; 11 | nodejsVersion = { 12 | description = "Specify nodejs version to use"; 13 | shell = import ../examples-projects/nodejs-version-shell/shell.nix { }; 14 | command = '' 15 | node -e 'console.log(process.versions.node.split(".")[0]);' 16 | ''; 17 | expected = "17\n"; 18 | }; 19 | pathContainsNodeApplications = { 20 | description = "Applications from the node_modules/.bin folder should be available on $PATH in the shell expression"; 21 | shell = npmlock2nix.v2.shell { src = ../examples-projects/bin-project; }; 22 | command = '' 23 | mkdirp --version 24 | ''; 25 | expected = "1.0.4\n"; 26 | }; 27 | 28 | symlinkNodeModules = 29 | let 30 | shell = npmlock2nix.v2.shell { 31 | src = ../examples-projects/bin-project; 32 | node_modules_mode = "symlink"; 33 | }; 34 | in 35 | { 36 | description = '' 37 | The shell builder supports linking the nix build node_modules folder into 38 | the current working directory via the `shellHook`. Verify taht we are 39 | indeed doing that. 40 | ''; 41 | inherit shell; 42 | command = '' 43 | readlink -f node_modules 44 | ''; 45 | expected = toString (shell.node_modules + "/node_modules\n"); 46 | }; 47 | 48 | symlinkNodeModulesDoesNotOverrideExistingNodeModules = 49 | let 50 | shell = npmlock2nix.v2.shell { 51 | src = ../examples-projects/bin-project; 52 | node_modules_mode = "symlink"; 53 | }; 54 | in 55 | { 56 | description = '' 57 | Ensure the shellHook doesn't override node_modules directory. 58 | ''; 59 | inherit shell; 60 | setup-command = '' 61 | mkdir node_modules 62 | ''; 63 | command = '' 64 | readlink -f node_modules 65 | ''; 66 | status = 1; 67 | expected = ""; 68 | expected-stderr = '' 69 | [npmlock2nix] There is already a `node_modules` directory. Not replacing it. 70 | ''; 71 | }; 72 | 73 | symlinkNodeModulesDoesOverrideExistingNodeModulesWhenInStore = 74 | let 75 | shell = npmlock2nix.v2.shell { 76 | src = ../examples-projects/bin-project; 77 | node_modules_mode = "symlink"; 78 | }; 79 | in 80 | { 81 | description = '' 82 | Ensure the shellHook doesn't override node_modules directory. 83 | ''; 84 | inherit shell; 85 | setup-command = '' 86 | ln -s ${runCommandNoCC "node_modules-fake" { } "mkdir $out; touch $out/.fake"} node_modules 87 | ''; 88 | command = '' 89 | if [ -e node_modules/.fake ]; then 90 | echo "expected the node_modules to be removed" 91 | exit 1 92 | fi 93 | exit 0 94 | ''; 95 | status = 0; 96 | expected = ""; 97 | }; 98 | 99 | 100 | copyNodeModulesDoesNotOverrideExistingNodeModules = 101 | let 102 | shell = npmlock2nix.v2.shell { 103 | src = ../examples-projects/bin-project; 104 | node_modules_mode = "copy"; 105 | }; 106 | in 107 | { 108 | description = '' 109 | Ensure the shellHook doesn't override node_modules directory. 110 | ''; 111 | inherit shell; 112 | setup-command = '' 113 | mkdir node_modules 114 | ''; 115 | command = '' 116 | readlink -f node_modules 117 | ''; 118 | status = 1; 119 | expected = ""; 120 | expected-stderr = '' 121 | [npmlock2nix] There is already a `node_modules` directory. Not replacing it. 122 | ''; 123 | }; 124 | 125 | symlinkNodeModulesCreatesALink = 126 | let 127 | shell = npmlock2nix.v2.shell { 128 | src = ../examples-projects/bin-project; 129 | node_modules_mode = "symlink"; 130 | }; 131 | in 132 | { 133 | description = '' 134 | Ensure the shellHook does create a symlink. 135 | ''; 136 | inherit shell; 137 | command = '' 138 | test -L node_modules || exit 1 139 | ''; 140 | expected = ""; 141 | }; 142 | 143 | copyNodeModulesCreatesANewDirectory = 144 | let 145 | shell = npmlock2nix.v2.shell { 146 | src = ../examples-projects/bin-project; 147 | node_modules_mode = "copy"; 148 | }; 149 | in 150 | { 151 | description = '' 152 | Ensure the shellHook does create a directory. 153 | ''; 154 | inherit shell; 155 | command = '' 156 | test -d node_modules || exit 1 157 | ''; 158 | expected = ""; 159 | }; 160 | 161 | buildInputsDoesntRemoveDefaultValues = 162 | let 163 | shell = npmlock2nix.v2.shell { 164 | src = ../examples-projects/bin-project; 165 | buildInputs = [ python3 ]; 166 | }; 167 | in 168 | { 169 | description = '' 170 | Ensure that providing additional buildInputs doesn't break our default buildInputs (e.g. nodejs). 171 | ''; 172 | inherit shell; 173 | command = '' 174 | node --version > /dev/null || exit 1 175 | python3 --version > /dev/null || exit 1 176 | ''; 177 | expected = ""; 178 | }; 179 | 180 | webpackCli = { 181 | description = '' 182 | We should be able to invoke the webpack(-cli) to build a very simple bootstrap based project 183 | ''; 184 | shell = npmlock2nix.v2.shell { 185 | src = ../examples-projects/webpack-cli-project; 186 | }; 187 | setup-command = '' 188 | cp --no-preserve=mode -r ${testLib.withoutNodeModules ../examples-projects/webpack-cli-project} workspace 189 | cd workspace 190 | ''; 191 | command = '' 192 | webpack-cli --version 193 | if ! webpack --mode production > .webpack.log 2>&1; then 194 | cat .webpack.log 195 | exit 1 196 | fi 197 | if ! test -e dist/main.js; then 198 | echo "dist/main.js missing" 199 | exit 1 200 | fi 201 | if ! test -e dist/index.html; then 202 | echo "dist/index.html missing" 203 | exit 1 204 | fi 205 | ''; 206 | expected = '' 207 | 3.3.12 208 | ''; 209 | }; 210 | 211 | bin-wrapped-dep = { 212 | description = '' 213 | Some packages try to download files into their source folder when they 214 | are missing. 215 | One of these packages is `cwebp-bin` that is based on the `bin-wrapper` 216 | package. The common pattern here is that they check for their binary (in 217 | this case `cwebp`) in the `vendor` directory of the package in the 218 | node_modules folder. 219 | 220 | In this test we are verifying that those node projects can be built and 221 | the binaries are availble during runtime (through node). 222 | 223 | We expect the path returned by nodejs to be a symlink to the actual store path. 224 | ''; 225 | 226 | shell = callPackage ../examples-projects/bin-wrapped-dep/shell.nix { }; 227 | command = '' 228 | readlink -f $(node -e 'console.log(require("cwebp-bin"))') 229 | ''; 230 | 231 | expected = '' 232 | ${libwebp}/bin/cwebp 233 | ''; 234 | }; 235 | 236 | sanitizes-package-names = { 237 | description = '' 238 | packages sometimes use `@` or `/` in package names which has to be sanitized 239 | to avoid nix errors with invalid store paths. 240 | ''; 241 | shell = callPackage ../examples-projects/unsanitized-package-name/shell.nix { }; 242 | command = '' 243 | node -e 'console.log("works")' 244 | ''; 245 | expected = '' 246 | works 247 | ''; 248 | }; 249 | 250 | source-patching = { 251 | description = "Source patching works"; 252 | shell = callPackage ../examples-projects/source-patching/shell.nix { }; 253 | command = '' 254 | node -e 'console.log(require("custom-hello-world")({}));' 255 | ''; 256 | expected = "Hello Nix\n"; 257 | }; 258 | } 259 | -------------------------------------------------------------------------------- /tests/tests-v2/integration-tests/leftpad-shell.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 4 | 5 | nix-shell $DIR/../tests/examples-projects/single-dependency/shell.nix --run "node -e 'require(\"leftpad\")(123, 7);'" 6 | -------------------------------------------------------------------------------- /tests/tests-v2/make-github-source.nix: -------------------------------------------------------------------------------- 1 | { lib, npmlock2nix, testLib }: 2 | let 3 | inherit (testLib) noSourceOptions; 4 | i = npmlock2nix.v2.internal; 5 | 6 | specRef = { 7 | resolved = "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 8 | version = "0.0"; 9 | }; 10 | specGitRef = { 11 | resolved = "leftpad@github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 12 | version = "0.0"; 13 | }; 14 | specGitFull = { 15 | resolved = "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9"; 16 | version = "0.0"; 17 | }; 18 | in 19 | (testLib.runTests { 20 | testGhSourceRef = { 21 | expr = 22 | let 23 | version = (i.patchPackage noSourceOptions "leftpad" specRef).resolved; 24 | in 25 | lib.hasPrefix "file:///nix/store" version; 26 | expected = true; 27 | }; 28 | testGhSourceGitRef = { 29 | expr = 30 | let 31 | version = (i.patchPackage noSourceOptions "leftpad" specGitRef).resolved; 32 | in 33 | lib.hasPrefix "file:///nix/store" version; 34 | expected = true; 35 | }; 36 | testGhSourceGitRefFull = { 37 | expr = 38 | let 39 | version = (i.patchPackage noSourceOptions "leftpad" specGitFull).resolved; 40 | in 41 | lib.hasPrefix "file:///nix/store" version; 42 | expected = true; 43 | }; 44 | }) 45 | -------------------------------------------------------------------------------- /tests/tests-v2/make-url-source.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, lib }: 2 | let 3 | inherit (testLib) noSourceOptions; 4 | name = "utils.logger"; 5 | version = "1.0.0"; 6 | resolved = "https://registry.npmjs.org/@apollo/utils.logger/-/utils.logger-1.0.0.tgz"; 7 | integrity = "sha512-dx9XrjyisD2pOa+KsB5RcDbWIAdgC91gJfeyLCgy0ctJMjQe7yZK5kdWaWlaOoCeX0z6YI9iYlg7vMPyMpQF3Q=="; 8 | in 9 | testLib.runTests { 10 | testUrlForDependency = { 11 | expr = 12 | let 13 | res = npmlock2nix.v2.internal.makeUrlSource noSourceOptions name version resolved integrity; 14 | in 15 | integrity == res.integrity && lib.hasPrefix "file:///nix/store" res.resolved; 16 | expected = true; 17 | }; 18 | } 19 | -------------------------------------------------------------------------------- /tests/tests-v2/node-modules.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, runCommand, nodejs-16_x, nodejs-17_x, python3 }: 2 | testLib.runTests { 3 | testNodeModulesForEmptyDependencies = { 4 | expr = 5 | let 6 | drv = npmlock2nix.v2.node_modules { 7 | src = ./examples-projects/no-dependencies; 8 | }; 9 | in 10 | { 11 | inherit (drv) version name; 12 | }; 13 | expected = { 14 | name = "no-dependencies-1.0.0"; 15 | version = "1.0.0"; 16 | }; 17 | }; 18 | 19 | testNodeModulesWithNoVersion = { 20 | expr = 21 | let 22 | drv = npmlock2nix.v2.node_modules { 23 | src = ./examples-projects/no-version; 24 | }; 25 | in 26 | { 27 | inherit (drv) version name; 28 | }; 29 | expected = { 30 | name = "no-version-0"; 31 | version = "0"; 32 | }; 33 | }; 34 | 35 | testNodeModulesForEmptyDependenciesHasNodeModulesFolder = { 36 | expr = 37 | let 38 | drv = npmlock2nix.v2.node_modules { 39 | src = ./examples-projects/no-dependencies; 40 | }; 41 | in 42 | builtins.pathExists (drv + "/node_modules"); 43 | expected = false; 44 | }; 45 | 46 | testNodeModulesForSimpleProjectHasLeftPad = { 47 | expr = 48 | let 49 | drv = npmlock2nix.v2.node_modules { 50 | src = ./examples-projects/single-dependency; 51 | }; 52 | in 53 | builtins.pathExists (drv + "/node_modules/leftpad"); 54 | expected = true; 55 | }; 56 | testNodeModulesForSimpleProjectCanUseLeftPad = { 57 | expr = 58 | let 59 | drv = npmlock2nix.v2.node_modules { 60 | src = ./examples-projects/single-dependency; 61 | }; 62 | in 63 | builtins.pathExists (runCommand "test-leftpad" 64 | { 65 | buildInputs = [ nodejs-17_x ]; 66 | } '' 67 | ln -s ${drv}/node_modules node_modules 68 | node -e "require('leftpad')" 69 | touch $out 70 | '' 71 | ); 72 | expected = true; 73 | }; 74 | 75 | testNodeModulesAcceptsCustomNodejs = { 76 | expr = (npmlock2nix.v2.node_modules { 77 | src = ./examples-projects/no-dependencies; 78 | nodejs = { 79 | pname = "our-custom-nodejs-package"; 80 | version = "17.12.34"; 81 | }; 82 | }).nodejs; 83 | expected = { 84 | pname = "our-custom-nodejs-package"; 85 | version = "17.12.34"; 86 | }; 87 | }; 88 | 89 | testNodeModulesPropagatesNodejs = 90 | let 91 | drv = npmlock2nix.v2.node_modules { 92 | src = ./examples-projects/no-dependencies; 93 | nodejs = nodejs-16_x; 94 | }; 95 | in 96 | { 97 | expr = drv.propagatedBuildInputs; 98 | expected = [ nodejs-16_x ]; 99 | }; 100 | 101 | testHonorsPrePostBuildHook = 102 | let 103 | drv = npmlock2nix.v2.node_modules { 104 | src = ./examples-projects/single-dependency; 105 | preBuild = '' 106 | echo -n "preBuild" > preBuild-test 107 | ''; 108 | postBuild = '' 109 | echo -n "postBuild" > postBuild-test 110 | mv *Build-test node_modules 111 | ''; 112 | }; 113 | in 114 | { 115 | expr = builtins.readFile (runCommand "concat" 116 | { } '' 117 | cat ${drv + "/node_modules/preBuild-test"} ${drv + "/node_modules/postBuild-test"} > $out 118 | '' 119 | ); 120 | expected = "preBuildpostBuild"; 121 | }; 122 | 123 | testBuildsNativeExtensions = 124 | let 125 | drv = npmlock2nix.v2.node_modules { 126 | src = ./examples-projects/native-extensions; 127 | buildInputs = [ python3 ]; 128 | sourceOverrides = with npmlock2nix.v2; { 129 | "@mapbox/node-pre-gyp" = packageRequirePatchShebangs; 130 | }; 131 | }; 132 | in 133 | { 134 | expr = builtins.pathExists drv.outPath; 135 | expected = true; 136 | }; 137 | 138 | testPassesExtraParameters = { 139 | expr = (npmlock2nix.v2.node_modules { 140 | src = ./examples-projects/single-dependency; 141 | SOME_EXTRA_PARAMETER = "123"; 142 | }).SOME_EXTRA_PARAMETER or "attribute missing"; 143 | expected = "123"; 144 | }; 145 | 146 | testHonorsPassedPassthru = { 147 | expr = (npmlock2nix.v2.node_modules { 148 | src = ./examples-projects/single-dependency; 149 | passthru.test-param = 123; 150 | }).passthru.test-param; 151 | expected = 123; 152 | }; 153 | 154 | testVersionAsResolvedUrl = 155 | let 156 | drv = npmlock2nix.v2.node_modules { 157 | src = ./examples-projects/url-as-version; 158 | }; 159 | in 160 | { 161 | expr = builtins.pathExists drv.outPath; 162 | expected = true; 163 | }; 164 | } 165 | -------------------------------------------------------------------------------- /tests/tests-v2/parse-github-ref.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib }: 2 | let 3 | i = npmlock2nix.v2.internal; 4 | in 5 | (testLib.runTests { 6 | testParsesASshGitRef = 7 | { 8 | expr = i.parseGitHubRef "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9"; 9 | expected = { 10 | parts = [ "git+ssh" [ ] "" [ ] "" [ ] "git@github.com" [ ] "tmcw" [ ] "leftpad.git" [ ] "db1442a0556c2b133627ffebf455a78a1ced64b9" ]; 11 | org = "tmcw"; 12 | repo = "leftpad"; 13 | rev = "db1442a0556c2b133627ffebf455a78a1ced64b9"; 14 | }; 15 | }; 16 | testParsesARef = 17 | { 18 | expr = i.parseGitHubRef "github:foo/bar#939360f9d1bafa9019b6ff8739495c6c9101c4a1"; 19 | expected = { 20 | parts = [ "github" [ ] "foo" [ ] "bar" [ ] "939360f9d1bafa9019b6ff8739495c6c9101c4a1" ]; 21 | org = "foo"; 22 | repo = "bar"; 23 | rev = "939360f9d1bafa9019b6ff8739495c6c9101c4a1"; 24 | }; 25 | }; 26 | testParsesAGitRef = 27 | { 28 | expr = i.parseGitHubRef "leftpad@github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 29 | expected = { 30 | parts = [ "leftpad@github" [ ] "tmcw" [ ] "leftpad" [ ] "db1442a0556c2b133627ffebf455a78a1ced64b9" ]; 31 | org = "tmcw"; 32 | repo = "leftpad"; 33 | rev = "db1442a0556c2b133627ffebf455a78a1ced64b9"; 34 | }; 35 | }; 36 | 37 | testIsGitHubRefShort = { 38 | expr = i.isGitHubRef "github:znerol/libxmljs#0517e063347ea2532c9fdf38dc47878c628bf0ae"; 39 | expected = true; 40 | }; 41 | 42 | testIsGitHubRefFull = { 43 | expr = i.isGitHubRef "git+ssh://git@github.com/znerol/libxmljs.git#0517e063347ea2532c9fdf38dc47878c628bf0ae"; 44 | expected = true; 45 | }; 46 | 47 | testIsGitHubRefImplicitSsh = { 48 | expr = i.isGitHubRef "git@github.com/znerol/libxmljs.git#0517e063347ea2532c9fdf38dc47878c628bf0ae"; 49 | expected = true; 50 | }; 51 | 52 | testIsGitHubRefWithoutRef = { 53 | expr = i.isGitHubRefWithoutRev "github:frozeman/bignumber.js-nolookahead"; 54 | expected = true; 55 | }; 56 | 57 | testIsNotGitHubRefWithoutRef = { 58 | expr = i.isGitHubRefWithoutRev "github:znerol/libxmljs#0517e063347ea2532c9fdf38dc47878c628bf0ae"; 59 | expected = false; 60 | }; 61 | }) 62 | -------------------------------------------------------------------------------- /tests/tests-v2/patch-package.nix: -------------------------------------------------------------------------------- 1 | { lib, npmlock2nix, testLib }: 2 | let 3 | inherit (testLib) noSourceOptions; 4 | i = npmlock2nix.v2.internal; 5 | 6 | specRef = { 7 | resolved = "github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 8 | version = "0.0"; 9 | }; 10 | specGitRef = { 11 | resolved = "leftpad@github:tmcw/leftpad#db1442a0556c2b133627ffebf455a78a1ced64b9"; 12 | version = "0.0"; 13 | }; 14 | specGitFull = { 15 | resolved = "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9"; 16 | version = "0.0"; 17 | }; 18 | specWithGhDependencies = { 19 | resolved = "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9"; 20 | version = "0.0"; 21 | dependencies = { 22 | utf8 = "^2.1.1"; 23 | "bignumber.js" = "github:frozeman/bignumber.js-nolookahead"; 24 | }; 25 | }; 26 | specWithPeerDependency = { 27 | inherit (specRef) resolved version; 28 | peerDependencies = { 29 | "@babel/core" = "^7.11.5"; 30 | react = "^16.8.0 || ^17.0.0 || ^18.0.0"; 31 | react-dom = "^16.8.0 || ^17.0.0 || ^18.0.0"; 32 | require-from-string = "^2.0.2"; 33 | }; 34 | }; 35 | in 36 | (testLib.runTests { 37 | testGhSourceRef = { 38 | expr = 39 | let 40 | version = (i.patchPackage noSourceOptions "leftpad" specRef).resolved; 41 | in 42 | lib.hasPrefix "file:///nix/store" version; 43 | expected = true; 44 | }; 45 | testGhSourceGitRef = { 46 | expr = 47 | let 48 | version = (i.patchPackage noSourceOptions "leftpad" specGitRef).resolved; 49 | in 50 | lib.hasPrefix "file:///nix/store" version; 51 | expected = true; 52 | }; 53 | testGhSourceGitRefFull = { 54 | expr = 55 | let 56 | version = (i.patchPackage noSourceOptions "leftpad" specGitFull).resolved; 57 | in 58 | lib.hasPrefix "file:///nix/store" version; 59 | expected = true; 60 | }; 61 | testPatchDepRegistry = { 62 | expr = 63 | let 64 | res = (i.patchPackage noSourceOptions "node_modules/yallist" { 65 | version = "4.0.0"; 66 | resolved = "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz"; 67 | integrity = "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="; 68 | }); 69 | in 70 | [ (res.version == "4.0.0") (lib.hasPrefix "file:///nix/store" res.resolved) (res.integrity == "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==") ]; 71 | expected = [ true true true ]; 72 | }; 73 | testPatchDepRegistryWithQueryString = { 74 | expr = 75 | let 76 | res = (i.patchPackage noSourceOptions "node_modules/@rjsf/bootstrap-5" { 77 | version = "4.2.0"; 78 | resolved = "https://github.com/nurikk/fileshare/blob/main/rjsf-bootstrap-5-4.2.0.tgz?raw=true"; 79 | integrity = "sha512-gHwtGSeteSl3LiSOk+rIENiVjI7yaMTYcxqroXZxErstz/5WcZV5Wme+8XCYBB7yLhMiWPvNlDS9Nr4urADIdQ=="; 80 | }); 81 | in 82 | [ (res.version == "4.2.0") (lib.hasPrefix "file:///nix/store" res.resolved) (lib.hasSuffix ".tgz" res.resolved) (res.integrity == "sha512-gHwtGSeteSl3LiSOk+rIENiVjI7yaMTYcxqroXZxErstz/5WcZV5Wme+8XCYBB7yLhMiWPvNlDS9Nr4urADIdQ==") ]; 83 | expected = [ true true true true ]; 84 | }; 85 | testPatchDepGithub = { 86 | expr = 87 | let 88 | res = (i.patchPackage noSourceOptions "node_modules/leftpad" { 89 | version = "0.0.1"; 90 | resolved = "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9"; 91 | integrity = "sha512-8NCRwDs07XJJnyO7d6fVbrKjpW1nkbH0dFH5v2/U7md4+4y2hL8+S9OpRqY54W22Dq45yFKuVUYmLlrjGeFLOw=="; 92 | license = "BSD-3-Clause"; 93 | }); 94 | in 95 | [ (lib.hasPrefix "file:///nix/store" res.resolved) (res.integrity == null) ]; 96 | expected = [ true true ]; 97 | }; 98 | testSpecWithGhDependencies = { 99 | expr = 100 | let 101 | result = (i.patchPackage noSourceOptions "leftpad" specWithGhDependencies); 102 | in 103 | [ 104 | (lib.hasPrefix "file:///nix/store" result.resolved) 105 | (result.dependencies.utf8 == "^2.1.1") 106 | (result.dependencies."bignumber.js" == "*") 107 | ]; 108 | expected = [ true true true ]; 109 | }; 110 | testSpecWithPeerDependency = { 111 | expr = 112 | let 113 | result = (i.patchPackage noSourceOptions "leftpad" specWithPeerDependency); 114 | in 115 | result ? peerDependencies; 116 | expected = false; 117 | }; 118 | }) 119 | -------------------------------------------------------------------------------- /tests/tests-v2/patch-packagefile.nix: -------------------------------------------------------------------------------- 1 | { lib, npmlock2nix, testLib }: 2 | let 3 | i = npmlock2nix.v2.internal; 4 | noSourceOptions = { 5 | sourceHashFunc = _: null; 6 | nodejs = null; 7 | }; 8 | in 9 | (testLib.runTests { 10 | testTurnsGitHubRefsToWildcards = { 11 | expr = (i.patchPackagefile noSourceOptions (i.readPackageLikeFile ./examples-projects/github-dependency/package.json)).dependencies.leftpad; 12 | expected = "*"; 13 | }; 14 | testHandlesBranches = { 15 | expr = (i.patchPackagefile noSourceOptions (i.readPackageLikeFile ./examples-projects/github-dependency-branch/package.json)).dependencies.leftpad; 16 | expected = "*"; 17 | }; 18 | testHandlesDevDependencies = { 19 | expr = (i.patchPackagefile noSourceOptions (i.readPackageLikeFile ./examples-projects/github-dev-dependency/package.json)).devDependencies.leftpad; 20 | expected = "*"; 21 | }; 22 | }) 23 | -------------------------------------------------------------------------------- /tests/tests-v2/patch-v2-package.nix: -------------------------------------------------------------------------------- 1 | { testLib, npmlock2nix, lib }: 2 | let 3 | inherit (testLib) noSourceOptions; 4 | i = npmlock2nix.v2.internal; 5 | in 6 | testLib.runTests { 7 | testPatchDepRegistry = { 8 | expr = 9 | let 10 | res = (i.patchV2Package noSourceOptions "node_modules/yallist" { 11 | version = "4.0.0"; 12 | resolved = "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz"; 13 | integrity = "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="; 14 | }); 15 | in 16 | res.version == "4.0.0" && lib.hasPrefix "file:///nix/store" res.resolved && res.integrity == "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="; 17 | expected = true; 18 | }; 19 | testPatchDepGithub = { 20 | expr = 21 | let 22 | res = (i.patchV2Package noSourceOptions "node_modules/leftpad" { 23 | version = "0.0.1"; 24 | resolved = "git+ssh://git@github.com/tmcw/leftpad.git#db1442a0556c2b133627ffebf455a78a1ced64b9"; 25 | integrity = "sha512-8NCRwDs07XJJnyO7d6fVbrKjpW1nkbH0dFH5v2/U7md4+4y2hL8+S9OpRqY54W22Dq45yFKuVUYmLlrjGeFLOw=="; 26 | license = "BSD-3-Clause"; 27 | }); 28 | in 29 | lib.hasPrefix "file:///nix/store" res.resolved && lib.hasPrefix "@id_leftpad" res.integrity; 30 | expected = true; 31 | }; 32 | } 33 | -------------------------------------------------------------------------------- /tests/tests-v2/read-lockfile/default.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib }: 2 | let 3 | i = npmlock2nix.v2.internal; 4 | in 5 | (testLib.runTests { 6 | testParsesSimpleLockfile = 7 | { 8 | expr = i.readPackageLikeFile ./simple.json; 9 | expected = { 10 | name = "simple"; 11 | version = "1.0.0"; 12 | lockfileVersion = 1; 13 | requires = true; 14 | dependencies = { 15 | leftpad = { 16 | version = "0.0.1"; 17 | resolved = "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz"; 18 | integrity = "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU="; 19 | }; 20 | }; 21 | }; 22 | }; 23 | 24 | # parse a lockfile that doesn't have dependencies so we can test that we 25 | # always have at least an empty attribute set of dependencies. 26 | testParsesLockfileWithoutDependencies = 27 | { 28 | expr = i.readPackageLikeFile ../examples-projects/no-dependencies/package-lock.json; 29 | expected = { 30 | name = "no-dependencies"; 31 | version = "1.0.0"; 32 | requires = true; 33 | lockfileVersion = 2; 34 | dependencies = { }; 35 | packages = { 36 | "" = { 37 | license = "ISC"; 38 | name = "no-dependencies"; 39 | version = "1.0.0"; 40 | }; 41 | }; 42 | }; 43 | }; 44 | }) 45 | -------------------------------------------------------------------------------- /tests/tests-v2/read-lockfile/empty.json: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nix-community/npmlock2nix/9197bbf397d76059a76310523d45df10d2e4ca81/tests/tests-v2/read-lockfile/empty.json -------------------------------------------------------------------------------- /tests/tests-v2/read-lockfile/simple.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simple", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "leftpad": { 8 | "version": "0.0.1", 9 | "resolved": "https://registry.npmjs.org/leftpad/-/leftpad-0.0.1.tgz", 10 | "integrity": "sha1-hrGk3k+s4YCsVFqD8VA1I9j+0RU=" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/tests-v2/shell.nix: -------------------------------------------------------------------------------- 1 | { npmlock2nix, testLib, symlinkJoin, runCommand, nodejs-17_x, lib }: 2 | let 3 | v2 = npmlock2nix.v2; 4 | in 5 | testLib.runTests { 6 | # test that the shell expression uses the same (given) nodejs package for 7 | # both the shell and node_modules 8 | testUsesGivenNodeJSPackage = 9 | let 10 | custom_nodejs = symlinkJoin { 11 | name = "custom-nodejs"; 12 | paths = [ nodejs-17_x ]; 13 | version = "17.8.3"; 14 | src = "/foo"; 15 | }; 16 | drv = v2.shell { 17 | src = ./examples-projects/single-dependency; 18 | nodejs = custom_nodejs; 19 | }; 20 | in 21 | { 22 | expr = { 23 | inherit (drv) buildInputs; 24 | node_modules_nodejs = drv.node_modules.nodejs; 25 | }; 26 | expected = { 27 | buildInputs = [ custom_nodejs drv.node_modules ]; 28 | node_modules_nodejs = custom_nodejs; 29 | }; 30 | }; 31 | 32 | # test that we are passing the pre- & postBuild attributes to node_modules 33 | testPassPreBuildAttributeToNodeModules = 34 | let 35 | drv = v2.shell { 36 | src = ./examples-projects/single-dependency; 37 | node_modules_attrs.preBuild = "foobar in preBuild"; 38 | }; 39 | in 40 | { 41 | expr = drv.node_modules.preBuild; 42 | expected = "foobar in preBuild"; 43 | }; 44 | 45 | testPassPostBuildAttributeToNodeModules = 46 | let 47 | drv = v2.shell { 48 | src = ./examples-projects/single-dependency; 49 | node_modules_attrs.postBuild = "foobar in postBuild"; 50 | }; 51 | in 52 | { 53 | expr = drv.node_modules.postBuild; 54 | expected = "foobar in postBuild"; 55 | }; 56 | 57 | testPassthruIsHonored = 58 | let 59 | drv = v2.shell { 60 | src = ./examples-projects/single-dependency; 61 | passthru.test-attribute = 123; 62 | }; 63 | in 64 | { 65 | expr = { 66 | inherit (drv.passthru) test-attribute; 67 | has_node_modules = drv.passthru ? node_modules; 68 | }; 69 | expected = { 70 | test-attribute = 123; 71 | has_node_modules = true; 72 | }; 73 | }; 74 | 75 | testShellHookIsHonored = 76 | let 77 | drv = v2.shell { 78 | src = ./examples-projects/single-dependency; 79 | shellHook = "magic-string"; 80 | }; 81 | in 82 | { 83 | # the shellHook should now contain a bunch of lines (for setting up the node_modules symlink / copy) and the given line. Our line must be on a line of its own. 84 | expr = 85 | let 86 | lines = lib.splitString "\n" drv.shellHook; 87 | more_than_one_line = (builtins.length lines) > 1; 88 | last_line = builtins.head (lib.reverseList lines); 89 | in 90 | { 91 | inherit more_than_one_line last_line; 92 | }; 93 | expected = { 94 | more_than_one_line = true; 95 | last_line = "magic-string"; 96 | }; 97 | }; 98 | 99 | } 100 | -------------------------------------------------------------------------------- /tests/tests-v2/source-hash-func.nix: -------------------------------------------------------------------------------- 1 | { lib, npmlock2nix, testLib }: 2 | let 3 | i = npmlock2nix.v2.internal; 4 | 5 | testGitHubMap = { 6 | foo-org.foo-repo."db1442a0556c2b133627ffebf455a78a1ced64b9" = "github-repo-hash"; 7 | }; 8 | testSpec = { 9 | type = "github"; 10 | value = { 11 | org = "foo-org"; 12 | repo = "foo-repo"; 13 | rev = "db1442a0556c2b133627ffebf455a78a1ced64b9"; 14 | }; 15 | }; 16 | in 17 | (testLib.runTests { 18 | testSimpleCase = { 19 | expr = i.sourceHashFunc testGitHubMap testSpec; 20 | expected = "github-repo-hash"; 21 | }; 22 | }) 23 | --------------------------------------------------------------------------------