Skip to content

Commit

Permalink
pnpm.fetchDeps: Add support for multiple workspaces; additional insta…
Browse files Browse the repository at this point in the history
…llFlags support
  • Loading branch information
pyrox0 committed Oct 23, 2024
1 parent 4545ad6 commit ac9bcc3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/languages-frameworks/javascript.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ pnpmDeps = pnpm.fetchDeps {
The above would make `pnpm.fetchDeps` call only install dependencies for the `@astrojs/language-server` workspace package.
Note that you do not need to set `sourceRoot` to make this work.

If you need to install packages for multiple workspaces, pass a list of workspaces to `pnpmWorkspaces`:

```nix
pnpmWorkspaces = ["@atproto/pds" "@atproto/did"]
```

Usually in such cases, you'd want to use `pnpm --filter=$pnpmWorkspace build` to build your project, as `npmHooks.npmBuildHook` probably won't work. A `buildPhase` based on the following example will probably fit most workspace projects:

```nix
Expand Down
9 changes: 7 additions & 2 deletions pkgs/development/tools/pnpm/fetch-deps/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
hash ? "",
pname,
pnpmWorkspace ? "",
pnpmWorkspaces ? [ ],
prePnpmInstall ? "",
pnpmInstallFlags ? [ ],
...
}@args:
let
Expand All @@ -32,7 +34,9 @@
outputHash = "";
outputHashAlgo = "sha256";
};
installFlags = lib.optionalString (pnpmWorkspace != "") "--filter=${pnpmWorkspace}";
filterFlags =
lib.optional (pnpmWorkspace != "") "--filter=${pnpmWorkspace}"
++ lib.map (package: "--filter=${package}") pnpmWorkspaces;
in
stdenvNoCC.mkDerivation (
finalAttrs:
Expand Down Expand Up @@ -73,7 +77,8 @@
pnpm install \
--force \
--ignore-scripts \
${installFlags} \
${lib.escapeShellArgs filterFlags} \
${lib.escapeShellArgs pnpmInstallFlags} \
--frozen-lockfile
runHook postInstall
Expand Down
8 changes: 8 additions & 0 deletions pkgs/development/tools/pnpm/fetch-deps/pnpm-config-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ pnpmConfigHook() {
if [[ -n "$pnpmWorkspace" ]]; then
pnpmInstallFlags+=("--filter=$pnpmWorkspace")
fi

if [[ -n "$pnpmWorkspaces" ]]; then
local IFS=" "
for ws in $pnpmWorkspaces; do
pnpmInstallFlags+=("--filter=$ws")
done
fi

runHook prePnpmInstall

pnpm install \
Expand Down

0 comments on commit ac9bcc3

Please sign in to comment.