@procedures/pnpm 0.34.0 → 0.35.1

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.
@@ -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)