@procedures/pnpm 0.33.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/module.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
 
10
- import { createProcedure, zodAdapter, defineModule, type AnyProcedure, type FsRootRules, type ProcedureModule, PROCEDURE_REGISTRY, registerModules, type ProcedureRegistry } from "@procedures/client";
10
+ import { createProcedure, defineModule, type AnyProcedure, type FsRootRules, type ProcedureModule, PROCEDURE_REGISTRY, registerModules, type ProcedureRegistry } from "@procedures/client";
11
11
  import shellModule from "@procedures/shell/module";
12
12
  import { pnpmInstall, pnpmAdd, pnpmRemove, pnpmLink, pnpmRun, pnpmStorePath } from "./procedures/pnpm/index.js";
13
13
  import {
@@ -68,8 +68,8 @@ import {
68
68
 
69
69
  const pnpmInstallProcedure = createProcedure()
70
70
  .path(["pnpm", "install"])
71
- .input(zodAdapter<PnpmInstallInput>(PnpmInstallInputSchema))
72
- .output(zodAdapter<PnpmCommandOutput>(PnpmCommandOutputSchema))
71
+ .input(PnpmInstallInputSchema)
72
+ .output(PnpmCommandOutputSchema)
73
73
  .meta({
74
74
  description: "Install packages using pnpm",
75
75
  calls: ["shell.run"],
@@ -86,8 +86,8 @@ const pnpmInstallProcedure = createProcedure()
86
86
 
87
87
  const pnpmAddProcedure = createProcedure()
88
88
  .path(["pnpm", "add"])
89
- .input(zodAdapter<PnpmAddInput>(PnpmAddInputSchema))
90
- .output(zodAdapter<PnpmCommandOutput>(PnpmCommandOutputSchema))
89
+ .input(PnpmAddInputSchema)
90
+ .output(PnpmCommandOutputSchema)
91
91
  .meta({
92
92
  description: "Add packages using pnpm",
93
93
  calls: ["shell.run"],
@@ -103,8 +103,8 @@ const pnpmAddProcedure = createProcedure()
103
103
 
104
104
  const pnpmRemoveProcedure = createProcedure()
105
105
  .path(["pnpm", "remove"])
106
- .input(zodAdapter<PnpmRemoveInput>(PnpmRemoveInputSchema))
107
- .output(zodAdapter<PnpmCommandOutput>(PnpmCommandOutputSchema))
106
+ .input(PnpmRemoveInputSchema)
107
+ .output(PnpmCommandOutputSchema)
108
108
  .meta({
109
109
  description: "Remove packages using pnpm",
110
110
  calls: ["shell.run"],
@@ -121,8 +121,8 @@ const pnpmRemoveProcedure = createProcedure()
121
121
 
122
122
  const pnpmLinkProcedure = createProcedure()
123
123
  .path(["pnpm", "link"])
124
- .input(zodAdapter<PnpmLinkInput>(PnpmLinkInputSchema))
125
- .output(zodAdapter<PnpmCommandOutput>(PnpmCommandOutputSchema))
124
+ .input(PnpmLinkInputSchema)
125
+ .output(PnpmCommandOutputSchema)
126
126
  .meta({
127
127
  description: "Link packages using pnpm",
128
128
  calls: ["shell.run"],
@@ -137,8 +137,8 @@ const pnpmLinkProcedure = createProcedure()
137
137
 
138
138
  const pnpmRunProcedure = createProcedure()
139
139
  .path(["pnpm", "run"])
140
- .input(zodAdapter<PnpmRunInput>(PnpmRunInputSchema))
141
- .output(zodAdapter<PnpmCommandOutput>(PnpmCommandOutputSchema))
140
+ .input(PnpmRunInputSchema)
141
+ .output(PnpmCommandOutputSchema)
142
142
  .meta({
143
143
  description: "Run package scripts using pnpm",
144
144
  calls: ["shell.run"],
@@ -155,18 +155,19 @@ const pnpmRunProcedure = createProcedure()
155
155
 
156
156
  const pnpmStorePathProcedure = createProcedure()
157
157
  .path(["pnpm", "store", "path"])
158
- .input(zodAdapter<PnpmStorePathInput>(PnpmStorePathInputSchema))
159
- .output(zodAdapter<PnpmStorePathOutput>(PnpmStorePathOutputSchema))
158
+ .input(PnpmStorePathInputSchema)
159
+ .output(PnpmStorePathOutputSchema)
160
160
  .meta({
161
161
  description: "Get pnpm store path for snapshot/restore",
162
+ calls: ["shell.run"],
162
163
  // `pnpm store path` only reads the configuration of pnpm
163
164
  effects: { reads: ["fs"], writes: [] },
164
165
  args: [],
165
166
  shorts: { cwd: "C" },
166
167
  output: "json",
167
168
  })
168
- .handler(async (input: PnpmStorePathInput): Promise<PnpmStorePathOutput> => {
169
- return pnpmStorePath(input);
169
+ .handler(async (input: PnpmStorePathInput, ctx: ProcedureContext): Promise<PnpmStorePathOutput> => {
170
+ return pnpmStorePath(input, ctx);
170
171
  })
171
172
  .build();
172
173
 
@@ -176,8 +177,8 @@ const pnpmStorePathProcedure = createProcedure()
176
177
 
177
178
  const pnpmExecProcedure = createProcedure()
178
179
  .path(["pnpm", "exec"])
179
- .input(zodAdapter<PnpmExecInput>(PnpmExecInputSchema))
180
- .output(zodAdapter<PnpmExecOutput>(PnpmExecOutputSchema))
180
+ .input(PnpmExecInputSchema)
181
+ .output(PnpmExecOutputSchema)
181
182
  .meta({
182
183
  description: "Run a program of the dependencies (node_modules/.bin) with pnpm exec",
183
184
  calls: ["shell.run"],
@@ -192,8 +193,8 @@ const pnpmExecProcedure = createProcedure()
192
193
 
193
194
  const pnpmListProcedure = createProcedure()
194
195
  .path(["pnpm", "list"])
195
- .input(zodAdapter<PnpmListInput>(PnpmListInputSchema))
196
- .output(zodAdapter<PnpmListOutput>(PnpmListOutputSchema))
196
+ .input(PnpmListInputSchema)
197
+ .output(PnpmListOutputSchema)
197
198
  .meta({
198
199
  description: "List the installed packages: data is the JSON of pnpm list --json",
199
200
  calls: ["shell.run"],
@@ -208,8 +209,8 @@ const pnpmListProcedure = createProcedure()
208
209
 
209
210
  const pnpmWhyProcedure = createProcedure()
210
211
  .path(["pnpm", "why"])
211
- .input(zodAdapter<PnpmWhyInput>(PnpmWhyInputSchema))
212
- .output(zodAdapter<PnpmWhyOutput>(PnpmWhyOutputSchema))
212
+ .input(PnpmWhyInputSchema)
213
+ .output(PnpmWhyOutputSchema)
213
214
  .meta({
214
215
  description: "Show the packages that depend on a package: data is the JSON of pnpm why --json",
215
216
  calls: ["shell.run"],
@@ -223,8 +224,8 @@ const pnpmWhyProcedure = createProcedure()
223
224
 
224
225
  const pnpmOutdatedProcedure = createProcedure()
225
226
  .path(["pnpm", "outdated"])
226
- .input(zodAdapter<PnpmOutdatedInput>(PnpmOutdatedInputSchema))
227
- .output(zodAdapter<PnpmOutdatedOutput>(PnpmOutdatedOutputSchema))
227
+ .input(PnpmOutdatedInputSchema)
228
+ .output(PnpmOutdatedOutputSchema)
228
229
  .meta({
229
230
  description: "List the outdated packages: data is the JSON of pnpm outdated",
230
231
  calls: ["shell.run"],
@@ -239,8 +240,8 @@ const pnpmOutdatedProcedure = createProcedure()
239
240
 
240
241
  const pnpmUpdateProcedure = createProcedure()
241
242
  .path(["pnpm", "update"])
242
- .input(zodAdapter<PnpmUpdateInput>(PnpmUpdateInputSchema))
243
- .output(zodAdapter<PnpmUpdateOutput>(PnpmUpdateOutputSchema))
243
+ .input(PnpmUpdateInputSchema)
244
+ .output(PnpmUpdateOutputSchema)
244
245
  .meta({
245
246
  description: "Update packages to their newest versions in the ranges, or with latest to the latest versions",
246
247
  calls: ["shell.run"],
@@ -255,8 +256,8 @@ const pnpmUpdateProcedure = createProcedure()
255
256
 
256
257
  const pnpmPackProcedure = createProcedure()
257
258
  .path(["pnpm", "pack"])
258
- .input(zodAdapter<PnpmPackInput>(PnpmPackInputSchema))
259
- .output(zodAdapter<PnpmPackOutput>(PnpmPackOutputSchema))
259
+ .input(PnpmPackInputSchema)
260
+ .output(PnpmPackOutputSchema)
260
261
  .meta({
261
262
  description: "Make a tarball of the package: data gives its file name and files",
262
263
  calls: ["shell.run"],
@@ -1,5 +1,6 @@
1
1
  /**
2
- * The way every pnpm procedure runs pnpm: an argument list through `shell.run`, no shell.
2
+ * The way every pnpm procedure runs pnpm: an argument list through `shell.run`, no shell. The
3
+ * runner is `defineCliTool()` of `@procedures/shell/cli`.
3
4
  *
4
5
  * Before, each procedure joined its arguments into one string for `shell.exec`, which runs a
5
6
  * shell, so a package name such as `x & calc` ran a second command (deep dive WRP-5).
@@ -10,16 +11,14 @@
10
11
  */
11
12
 
12
13
  import { existsSync } from "node:fs";
13
- import { delimiter, join } from "node:path";
14
+ import { join } from "node:path";
14
15
  import type { ProcedureContext } from "@procedures/client";
16
+ import { defineCliTool, execPathProgram, findInPath, nodeScript, parseJsonDocument } from "@procedures/shell/cli";
15
17
  import type { PnpmCommandOutput, PnpmJsonOutput } from "../../types.js";
16
18
 
17
19
  /** A value that pnpm reads as an argument (a package, a script, a path): it must not start with "-". */
18
20
  export function pnpmArg(name: string, value: string): string {
19
- if (value.startsWith("-")) {
20
- throw new Error(`Invalid ${name} (starts with "-"): ${value}`);
21
- }
22
- return value;
21
+ return pnpm.arg(name, value);
23
22
  }
24
23
 
25
24
  /** The selection of the workspace packages of a command (roadmap R41). */
@@ -42,66 +41,37 @@ export function selectionArgs(input: PnpmSelection): string[] {
42
41
  /** The program and the first arguments that start pnpm without a shell. */
43
42
  export function resolvePnpm(): { command: string; prefix: string[] } {
44
43
  // When pnpm runs this process, npm_execpath is its script, or its executable from pnpm 12 on
45
- const execPath = process.env["npm_execpath"];
46
- if (execPath && /pnpm\.c?js$/.test(execPath) && existsSync(execPath)) {
47
- return { command: process.execPath, prefix: [execPath] };
48
- }
49
- if (execPath && /(^|[\\/])pnpm(\.exe)?$/i.test(execPath) && existsSync(execPath)) {
50
- return { command: execPath, prefix: [] };
51
- }
44
+ const fromExecPath = execPathProgram({ script: /pnpm\.c?js$/, executable: /(^|[\\/])pnpm(\.exe)?$/i });
45
+ if (fromExecPath) return fromExecPath;
52
46
  if (process.platform !== "win32") {
53
47
  return { command: "pnpm", prefix: [] };
54
48
  }
55
- for (const dir of (process.env["PATH"] ?? "").split(delimiter)) {
56
- if (!dir) continue;
57
- if (existsSync(join(dir, "pnpm.exe"))) {
58
- return { command: join(dir, "pnpm.exe"), prefix: [] };
59
- }
49
+ const found = findInPath((dir) => {
50
+ if (existsSync(join(dir, "pnpm.exe"))) return { command: join(dir, "pnpm.exe"), prefix: [] };
60
51
  const script = join(dir, "node_modules", "pnpm", "bin", "pnpm.cjs");
61
- if (existsSync(script)) {
62
- return { command: process.execPath, prefix: [script] };
63
- }
64
- }
52
+ return existsSync(script) ? nodeScript(script) : undefined;
53
+ });
54
+ if (found) return found;
55
+ const execPath = process.env["npm_execpath"];
65
56
  throw new Error(`pnpm was not found: install pnpm, or run this from a pnpm script (npm_execpath: ${execPath ?? "not set"})`);
66
57
  }
67
58
 
59
+ /** The runner of pnpm. It finds pnpm at each command. */
60
+ const pnpm = defineCliTool({ name: "pnpm", resolve: resolvePnpm });
61
+
68
62
  /** This function runs `pnpm <args>` and gives its result. It does not throw for a failed command. */
69
63
  export async function runPnpm(
70
64
  args: string[],
71
65
  input: { cwd?: string | undefined; timeout?: number | undefined },
72
66
  ctx: ProcedureContext
73
67
  ): Promise<PnpmCommandOutput> {
74
- const startTime = Date.now();
75
- try {
76
- const pnpm = resolvePnpm();
77
- const shellInput: { command: string; args: string[]; cwd?: string; timeout?: number } = {
78
- command: pnpm.command,
79
- args: [...pnpm.prefix, ...args],
80
- };
81
- if (input.cwd !== undefined) shellInput.cwd = input.cwd;
82
- if (input.timeout !== undefined) shellInput.timeout = input.timeout;
83
-
84
- const result = await ctx.client.call<
85
- typeof shellInput,
86
- { exitCode: number; stdout: string; stderr: string }
87
- >(["shell", "run"], shellInput);
68
+ return pnpm.run(args, { cwd: input.cwd, timeout: input.timeout }, ctx);
69
+ }
88
70
 
89
- return {
90
- exitCode: result.exitCode,
91
- stdout: result.stdout,
92
- stderr: result.stderr,
93
- success: result.exitCode === 0,
94
- duration: Date.now() - startTime,
95
- };
96
- } catch (error) {
97
- return {
98
- exitCode: 1,
99
- stdout: "",
100
- stderr: error instanceof Error ? error.message : String(error),
101
- success: false,
102
- duration: Date.now() - startTime,
103
- };
104
- }
71
+ /** The JSON of a pnpm output: a document that starts with "{" or "[". Other output gives undefined. */
72
+ function parsePnpmJson(stdout: string): unknown {
73
+ const text = stdout.trim();
74
+ return text.startsWith("{") || text.startsWith("[") ? parseJsonDocument(text) : undefined;
105
75
  }
106
76
 
107
77
  /**
@@ -113,14 +83,5 @@ export async function runPnpmJson(
113
83
  input: { cwd?: string | undefined; timeout?: number | undefined },
114
84
  ctx: ProcedureContext
115
85
  ): Promise<PnpmJsonOutput> {
116
- const result: PnpmJsonOutput = await runPnpm(args, input, ctx);
117
- const text = result.stdout.trim();
118
- if (text.startsWith("{") || text.startsWith("[")) {
119
- try {
120
- result.data = JSON.parse(text);
121
- } catch {
122
- // Not JSON: stdout has the text
123
- }
124
- }
125
- return result;
86
+ return pnpm.runJson(args, { cwd: input.cwd, timeout: input.timeout }, ctx, parsePnpmJson);
126
87
  }
@@ -6,14 +6,15 @@
6
6
  */
7
7
 
8
8
  import { existsSync } from "node:fs";
9
- import { execSync } from "node:child_process";
9
+ import type { ProcedureContext } from "@procedures/client";
10
+ import { runPnpm } from "./shared.js";
10
11
  import type { PnpmStorePathInput, PnpmStorePathOutput } from "../../types.js";
11
12
 
12
13
  /**
13
14
  * Get pnpm store path
14
15
  *
15
- * Uses `pnpm store path` command to get the actual store location.
16
- * Falls back to platform-specific defaults if command fails.
16
+ * Runs `pnpm store path` through `shell.run` (argv, no shell) to get the actual store location.
17
+ * Falls back to platform-specific defaults if the command fails.
17
18
  *
18
19
  * Store locations by platform:
19
20
  * - Windows: %LocalAppData%\pnpm\store or %AppData%\pnpm\store
@@ -23,22 +24,12 @@ import type { PnpmStorePathInput, PnpmStorePathOutput } from "../../types.js";
23
24
  * const result = await client.call(["pnpm", "store", "path"], {});
24
25
  * console.log(result.path); // "C:\Users\user\AppData\Local\pnpm\store\v3"
25
26
  */
26
- export async function pnpmStorePath(input: PnpmStorePathInput): Promise<PnpmStorePathOutput> {
27
+ export async function pnpmStorePath(input: PnpmStorePathInput, ctx: ProcedureContext): Promise<PnpmStorePathOutput> {
27
28
  const platform = process.platform;
28
- let storePath = "";
29
-
30
- try {
31
- // Try to get store path from pnpm directly
32
- const opts: { cwd?: string; encoding: "utf8" } = { encoding: "utf8" };
33
- if (input.cwd) {
34
- opts.cwd = input.cwd;
35
- }
36
-
37
- storePath = execSync("pnpm store path", opts).trim();
38
- } catch {
39
- // Fallback to platform-specific defaults
40
- storePath = getDefaultStorePath(platform);
41
- }
29
+ // runPnpm does not throw: pnpm not found or a failed command gives a result that is not a success
30
+ const result = await runPnpm(["store", "path"], { cwd: input.cwd }, ctx);
31
+ const printed = result.success ? result.stdout.trim() : "";
32
+ const storePath = printed || getDefaultStorePath(platform);
42
33
 
43
34
  return {
44
35
  path: storePath,
package/src/types.ts CHANGED
@@ -5,23 +5,14 @@
5
5
  */
6
6
 
7
7
  import { z } from "zod";
8
+ import { cliToolJsonOutputSchema, cliToolOutputSchema, type CliToolOutput } from "@procedures/shell/cli";
8
9
 
9
10
  // =============================================================================
10
11
  // Shared Output Type
11
12
  // =============================================================================
12
13
 
13
- export interface PnpmCommandOutput {
14
- /** Exit code of the command */
15
- exitCode: number;
16
- /** Standard output */
17
- stdout: string;
18
- /** Standard error */
19
- stderr: string;
20
- /** Whether command succeeded (exit code 0) */
21
- success: boolean;
22
- /** Duration in milliseconds */
23
- duration: number;
24
- }
14
+ /** The result of a pnpm command: the shared result of a CLI tool (`@procedures/shell/cli`). */
15
+ export interface PnpmCommandOutput extends CliToolOutput {}
25
16
 
26
17
  /** The result of a pnpm command that writes JSON: `data` is its parsed standard output. */
27
18
  export interface PnpmJsonOutput extends PnpmCommandOutput {
@@ -200,13 +191,7 @@ export interface PnpmStorePathOutput {
200
191
  // result (a strict schema would remove it when the output is validated).
201
192
 
202
193
  /** The result of a pnpm command: add, install, remove, link and run give it. */
203
- export const PnpmCommandOutputSchema: z.ZodType<PnpmCommandOutput> = z.looseObject({
204
- exitCode: z.number().describe("The exit code of pnpm (0 is success)"),
205
- stdout: z.string().describe("The standard output of pnpm"),
206
- stderr: z.string().describe("The standard error of pnpm"),
207
- success: z.boolean().describe("True when the exit code is 0"),
208
- duration: z.number().describe("The time of the command, in milliseconds"),
209
- });
194
+ export const PnpmCommandOutputSchema: z.ZodType<PnpmCommandOutput> = cliToolOutputSchema("pnpm");
210
195
 
211
196
  export const PnpmStorePathOutputSchema: z.ZodType<PnpmStorePathOutput> = z.looseObject({
212
197
  path: z.string().describe("The absolute path of the pnpm store"),
@@ -215,14 +200,7 @@ export const PnpmStorePathOutputSchema: z.ZodType<PnpmStorePathOutput> = z.loose
215
200
  });
216
201
 
217
202
  /** The result of a pnpm command that writes JSON: list, why, outdated and pack give it. */
218
- export const PnpmJsonOutputSchema: z.ZodType<PnpmJsonOutput> = z.looseObject({
219
- exitCode: z.number().describe("The exit code of pnpm (0 is success)"),
220
- stdout: z.string().describe("The standard output of pnpm"),
221
- stderr: z.string().describe("The standard error of pnpm"),
222
- success: z.boolean().describe("True when the command succeeded"),
223
- duration: z.number().describe("The time of the command, in milliseconds"),
224
- data: z.unknown().optional().describe("The parsed JSON of the standard output, when it is JSON"),
225
- });
203
+ export const PnpmJsonOutputSchema: z.ZodType<PnpmJsonOutput> = cliToolJsonOutputSchema("pnpm");
226
204
 
227
205
  // =============================================================================
228
206
  // pnpm.exec Types - Run a program of the dependencies (roadmap R41)