printable-shell-command 2.1.2 → 2.3.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/README.md CHANGED
@@ -51,7 +51,8 @@ const child_process = spawn(...command.toCommandWithFlatArgs()); // Note the `..
51
51
 
52
52
  // or directly
53
53
  await command.spawn().success;
54
- await command.spawnInherit().success;
54
+ await command.spawnTransparently().success;
55
+ await command.spawnDetached().success;
55
56
  ```
56
57
 
57
58
  ### Spawn a process in `bun`
@@ -3,9 +3,8 @@ import { styleText } from "node:util";
3
3
  import type { SpawnOptions as BunSpawnOptions, Subprocess as BunSubprocess, SpawnOptions } from "bun";
4
4
  import { Path } from "path-class";
5
5
  import type { SetFieldType } from "type-fest";
6
- type SingleArgument = string;
7
- type FlagArgumentGroup = string[];
8
- type ArgsEntry = SingleArgument | FlagArgumentGroup;
6
+ type SingleArgument = string | Path;
7
+ type ArgsEntry = SingleArgument | SingleArgument[];
9
8
  type Args = ArgsEntry[];
10
9
  export interface PrintOptions {
11
10
  /** Defaults to "" */
@@ -117,17 +116,52 @@ export declare class PrintableShellCommand {
117
116
  NodeChildProcess & {
118
117
  success: Promise<void>;
119
118
  };
120
- /** A wrapper for `.spawnNode(…)` that sets stdio to `"inherit"` (common for
119
+ /** A wrapper for `.spawn(…)` that sets stdio to `"inherit"` (common for
121
120
  * invoking commands from scripts whose output and interaction should be
122
- * surfaced to the user). */
121
+ * surfaced to the user).
122
+ *
123
+ * If there is no other interaction with the shell from the calling process,
124
+ * then it acts "transparent" and allows user to interact with the subprocess
125
+ * in its stead.
126
+ */
127
+ spawnTransparently(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): NodeChildProcess & {
128
+ success: Promise<void>;
129
+ };
130
+ /** @deprecated: Use `.spawnTransparently(…)`. */
123
131
  spawnInherit(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): NodeChildProcess & {
124
132
  success: Promise<void>;
125
133
  };
134
+ /** A wrapper for `.spawn(…)` that:
135
+ *
136
+ * - sets `detached` to `true`,
137
+ * - sets stdio to `"inherit"`,
138
+ * - calls `.unref()`, and
139
+ * - does not wait for the process to exit.
140
+ *
141
+ * This is similar to starting a command int he background and disowning it (in a shell).
142
+ *
143
+ * The `stdio` field is left overridable. To capture `stdout` and `stderr`, connect them to output files like this:
144
+ *
145
+ * import { open } from "node:fs/promises";
146
+ * import { Path } from "path-class";
147
+ * import { PrintableShellCommand } from "printable-shell-command";
148
+ *
149
+ * const tempDir = await Path.makeTempDir();
150
+ * console.log(`Temp dir: ${tempDir}`);
151
+ * const stdout = await open(tempDir.join("stdout.log").path, "a");
152
+ * const stderr = await open(tempDir.join("stderr.log").path, "a");
153
+ *
154
+ * new PrintableShellCommand("echo", ["hi"]).spawnDetached({
155
+ * stdio: ["ignore", stdout.fd, stderr.fd],
156
+ * });
157
+ *
158
+ */
159
+ spawnDetached(options?: NodeWithCwd<Omit<NodeSpawnOptions, "detached">>): void;
126
160
  stdout(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Response;
127
161
  /** Equivalent to:
128
162
  *
129
163
  * ```
130
- * await this.print().spawnInherit(…).success;
164
+ * await this.print().spawnTransparently(…).success;
131
165
  * ```
132
166
  */
133
167
  shellOut(options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>): Promise<void>;
@@ -1,7 +1,7 @@
1
1
  // src/index.ts
2
2
  import { Readable } from "node:stream";
3
3
  import { styleText } from "node:util";
4
- import { Path } from "path-class";
4
+ import { Path, stringifyIfPath } from "path-class";
5
5
  var DEFAULT_MAIN_INDENTATION = "";
6
6
  var DEFAULT_ARG_INDENTATION = " ";
7
7
  var DEFAULT_ARGUMENT_LINE_WRAPPING = "by-entry";
@@ -85,7 +85,7 @@ var PrintableShellCommand = class {
85
85
  * ```
86
86
  */
87
87
  toFlatCommand() {
88
- return [this.commandName, ...this.args.flat()];
88
+ return [this.commandName, ...this.args.flat().map(stringifyIfPath)];
89
89
  }
90
90
  /**
91
91
  * Convenient alias for `toFlatCommand()`.
@@ -119,7 +119,7 @@ var PrintableShellCommand = class {
119
119
  *
120
120
  */
121
121
  toCommandWithFlatArgs() {
122
- return [this.commandName, this.args.flat()];
122
+ return [this.commandName, this.args.flat().map(stringifyIfPath)];
123
123
  }
124
124
  /**
125
125
  * For use with `node:child_process`
@@ -203,12 +203,14 @@ var PrintableShellCommand = class {
203
203
  options ??= {};
204
204
  const serializedEntries = [];
205
205
  for (let i = 0; i < this.args.length; i++) {
206
- const argsEntry = this.args[i];
206
+ const argsEntry = stringifyIfPath(this.args[i]);
207
207
  if (isString(argsEntry)) {
208
208
  serializedEntries.push(this.#escapeArg(argsEntry, false, options));
209
209
  } else {
210
210
  serializedEntries.push(
211
- argsEntry.map((part) => this.#escapeArg(part, false, options)).join(this.#argPairSeparator(options))
211
+ argsEntry.map(
212
+ (part) => this.#escapeArg(stringifyIfPath(part), false, options)
213
+ ).join(this.#argPairSeparator(options))
212
214
  );
213
215
  }
214
216
  }
@@ -227,14 +229,7 @@ var PrintableShellCommand = class {
227
229
  */
228
230
  spawn(options) {
229
231
  const { spawn } = process.getBuiltinModule("node:child_process");
230
- const cwd = (() => {
231
- if (typeof options?.cwd !== "undefined") {
232
- if (options.cwd instanceof Path) {
233
- return options.cwd.toString();
234
- }
235
- }
236
- return options?.cwd;
237
- })();
232
+ const cwd = stringifyIfPath(options?.cwd);
238
233
  const subprocess = spawn(...this.forNode(), {
239
234
  ...options,
240
235
  cwd
@@ -258,15 +253,60 @@ var PrintableShellCommand = class {
258
253
  });
259
254
  return subprocess;
260
255
  }
261
- /** A wrapper for `.spawnNode(…)` that sets stdio to `"inherit"` (common for
256
+ /** A wrapper for `.spawn(…)` that sets stdio to `"inherit"` (common for
262
257
  * invoking commands from scripts whose output and interaction should be
263
- * surfaced to the user). */
264
- spawnInherit(options) {
258
+ * surfaced to the user).
259
+ *
260
+ * If there is no other interaction with the shell from the calling process,
261
+ * then it acts "transparent" and allows user to interact with the subprocess
262
+ * in its stead.
263
+ */
264
+ spawnTransparently(options) {
265
265
  if (options && "stdio" in options) {
266
266
  throw new Error("Unexpected `stdio` field.");
267
267
  }
268
268
  return this.spawn({ ...options, stdio: "inherit" });
269
269
  }
270
+ /** @deprecated: Use `.spawnTransparently(…)`. */
271
+ spawnInherit(options) {
272
+ return this.spawnTransparently(options);
273
+ }
274
+ /** A wrapper for `.spawn(…)` that:
275
+ *
276
+ * - sets `detached` to `true`,
277
+ * - sets stdio to `"inherit"`,
278
+ * - calls `.unref()`, and
279
+ * - does not wait for the process to exit.
280
+ *
281
+ * This is similar to starting a command int he background and disowning it (in a shell).
282
+ *
283
+ * The `stdio` field is left overridable. To capture `stdout` and `stderr`, connect them to output files like this:
284
+ *
285
+ * import { open } from "node:fs/promises";
286
+ * import { Path } from "path-class";
287
+ * import { PrintableShellCommand } from "printable-shell-command";
288
+ *
289
+ * const tempDir = await Path.makeTempDir();
290
+ * console.log(`Temp dir: ${tempDir}`);
291
+ * const stdout = await open(tempDir.join("stdout.log").path, "a");
292
+ * const stderr = await open(tempDir.join("stderr.log").path, "a");
293
+ *
294
+ * new PrintableShellCommand("echo", ["hi"]).spawnDetached({
295
+ * stdio: ["ignore", stdout.fd, stderr.fd],
296
+ * });
297
+ *
298
+ */
299
+ spawnDetached(options) {
300
+ if (options && "detached" in options) {
301
+ throw new Error("Unexpected `detached` field.");
302
+ }
303
+ const childProcess = this.spawn({
304
+ stdio: "ignore",
305
+ ...options,
306
+ detached: true
307
+ });
308
+ childProcess.unref();
309
+ }
270
310
  stdout(options) {
271
311
  if (options && "stdio" in options) {
272
312
  throw new Error("Unexpected `stdio` field.");
@@ -280,11 +320,11 @@ var PrintableShellCommand = class {
280
320
  /** Equivalent to:
281
321
  *
282
322
  * ```
283
- * await this.print().spawnInherit(…).success;
323
+ * await this.print().spawnTransparently(…).success;
284
324
  * ```
285
325
  */
286
326
  async shellOut(options) {
287
- await this.print().spawnInherit(options).success;
327
+ await this.print().spawnTransparently(options).success;
288
328
  }
289
329
  /**
290
330
  * The returned subprocess includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/index.ts"],
4
- "sourcesContent": ["import type {\n ChildProcess as NodeChildProcess,\n SpawnOptions as NodeSpawnOptions,\n SpawnOptionsWithoutStdio as NodeSpawnOptionsWithoutStdio,\n SpawnOptionsWithStdioTuple as NodeSpawnOptionsWithStdioTuple,\n StdioNull as NodeStdioNull,\n StdioPipe as NodeStdioPipe,\n ProcessEnvOptions,\n} from \"node:child_process\";\nimport { Readable } from \"node:stream\";\nimport { styleText } from \"node:util\";\nimport type {\n SpawnOptions as BunSpawnOptions,\n Subprocess as BunSubprocess,\n SpawnOptions,\n} from \"bun\";\nimport { Path } from \"path-class\";\nimport type { SetFieldType } from \"type-fest\";\n\nconst DEFAULT_MAIN_INDENTATION = \"\";\nconst DEFAULT_ARG_INDENTATION = \" \";\nconst DEFAULT_ARGUMENT_LINE_WRAPPING = \"by-entry\";\n\nconst INLINE_SEPARATOR = \" \";\nconst LINE_WRAP_LINE_END = \" \\\\\\n\";\n\n// biome-ignore lint/suspicious/noExplicitAny: This is the correct type nere.\nfunction isString(s: any): s is string {\n return typeof s === \"string\";\n}\n// biome-ignore lint/suspicious/noExplicitAny: This is the correct type nere.\nfunction isStringArray(entries: any[]): entries is string[] {\n for (const entry of entries) {\n if (!isString(entry)) {\n return false;\n }\n }\n return true;\n}\n\n// TODO: allow `.toString()`ables?\ntype SingleArgument = string;\ntype FlagArgumentGroup = string[];\ntype ArgsEntry = SingleArgument | FlagArgumentGroup;\ntype Args = ArgsEntry[];\n\nexport interface PrintOptions {\n /** Defaults to \"\" */\n mainIndentation?: string;\n /** Defaults to \" \" */\n argIndentation?: string;\n /**\n * - `\"auto\"`: Quote only arguments that need it for safety. This tries to be\n * portable and safe across shells, but true safety and portability is hard\n * to guarantee.\n * - `\"extra-safe\"`: Quote all arguments, even ones that don't need it. This is\n * more likely to be safe under all circumstances.\n */\n quoting?: \"auto\" | \"extra-safe\";\n /** Line wrapping to use between arguments. Defaults to `\"by-entry\"`. */\n argumentLineWrapping?:\n | \"by-entry\"\n | \"nested-by-entry\"\n | \"by-argument\"\n | \"inline\";\n /** Include the first arg (or first arg group) on the same line as the command, regardless of the `argumentLineWrapping` setting. */\n skipLineWrapBeforeFirstArg?: true | false;\n /**\n * Style text using `node`'s [`styleText(\u2026)`](https://nodejs.org/api/util.html#utilstyletextformat-text-options)\n *\n * Example usage:\n *\n * ```\n * new PrintableShellCommand(\"echo\", [\"hi\"]).print({\n * styleTextFormat: [\"gray\", \"bold\"],\n * });\n * */\n styleTextFormat?: Parameters<typeof styleText>[0];\n}\n\n// https://mywiki.wooledge.org/BashGuide/SpecialCharacters\nconst SPECIAL_SHELL_CHARACTERS = new Set([\n \" \",\n '\"',\n \"'\",\n \"`\",\n \"|\",\n \"$\",\n \"*\",\n \"?\",\n \">\",\n \"<\",\n \"(\",\n \")\",\n \"[\",\n \"]\",\n \"{\",\n \"}\",\n \"&\",\n \"\\\\\",\n \";\",\n \"#\",\n]);\n\n// https://mywiki.wooledge.org/BashGuide/SpecialCharacters\nconst SPECIAL_SHELL_CHARACTERS_FOR_MAIN_COMMAND =\n // biome-ignore lint/suspicious/noExplicitAny: Workaround to make this package easier to use in a project that otherwise only uses ES2022.)\n (SPECIAL_SHELL_CHARACTERS as unknown as any).union(new Set([\"=\"]));\n\ntype NodeCwd = ProcessEnvOptions[\"cwd\"] | Path;\ntype NodeWithCwd<T extends { cwd?: ProcessEnvOptions[\"cwd\"] }> = SetFieldType<\n T,\n \"cwd\",\n NodeCwd | undefined\n>;\n\n// biome-ignore lint/suspicious/noExplicitAny: Just matching\ntype BunCwd = SpawnOptions.OptionsObject<any, any, any>[\"cwd\"] | Path;\ntype BunWithCwd<\n // biome-ignore lint/suspicious/noExplicitAny: Just matching\n T extends { cwd?: SpawnOptions.OptionsObject<any, any, any>[\"cwd\"] },\n> = SetFieldType<T, \"cwd\", BunCwd | undefined>;\n\nexport class PrintableShellCommand {\n #commandName: string;\n constructor(\n commandName: string,\n private args: Args = [],\n ) {\n if (!isString(commandName)) {\n // biome-ignore lint/suspicious/noExplicitAny: We want to print this, no matter what it is.\n throw new Error(\"Command name is not a string:\", commandName as any);\n }\n this.#commandName = commandName;\n if (typeof args === \"undefined\") {\n return;\n }\n if (!Array.isArray(args)) {\n throw new Error(\"Command arguments are not an array\");\n }\n for (let i = 0; i < args.length; i++) {\n const argEntry = args[i];\n if (typeof argEntry === \"string\") {\n continue;\n }\n if (Array.isArray(argEntry) && isStringArray(argEntry)) {\n continue;\n }\n throw new Error(`Invalid arg entry at index: ${i}`);\n }\n }\n\n get commandName(): string {\n return this.#commandName;\n }\n\n /** For use with `bun`.\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"bun\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * await spawn(command.toFlatCommand()).exited;\n * ```\n */\n public toFlatCommand(): string[] {\n return [this.commandName, ...this.args.flat()];\n }\n\n /**\n * Convenient alias for `toFlatCommand()`.\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"bun\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * await spawn(command.forBun()).exited;\n * ```\n *\n * */\n public forBun(): string[] {\n return this.toFlatCommand();\n }\n\n /**\n * For use with `node:child_process`\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"node:child_process\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * const child_process = spawn(...command.toCommandWithFlatArgs()); // Note the `...`\n * ```\n *\n */\n public toCommandWithFlatArgs(): [string, string[]] {\n return [this.commandName, this.args.flat()];\n }\n\n /**\n * For use with `node:child_process`\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"node:child_process\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * const child_process = spawn(...command.forNode()); // Note the `...`\n * ```\n *\n * Convenient alias for `toCommandWithFlatArgs()`.\n */\n public forNode(): [string, string[]] {\n return this.toCommandWithFlatArgs();\n }\n\n #escapeArg(\n arg: string,\n isMainCommand: boolean,\n options: PrintOptions,\n ): string {\n const argCharacters = new Set(arg);\n const specialShellCharacters = isMainCommand\n ? SPECIAL_SHELL_CHARACTERS_FOR_MAIN_COMMAND\n : SPECIAL_SHELL_CHARACTERS;\n if (\n options?.quoting === \"extra-safe\" ||\n // biome-ignore lint/suspicious/noExplicitAny: Workaround to make this package easier to use in a project that otherwise only uses ES2022.)\n (argCharacters as unknown as any).intersection(specialShellCharacters)\n .size > 0\n ) {\n // Use single quote to reduce the need to escape (and therefore reduce the chance for bugs/security issues).\n const escaped = arg.replaceAll(\"\\\\\", \"\\\\\\\\\").replaceAll(\"'\", \"\\\\'\");\n return `'${escaped}'`;\n }\n return arg;\n }\n\n #mainIndentation(options: PrintOptions): string {\n return options?.mainIndentation ?? DEFAULT_MAIN_INDENTATION;\n }\n\n #argIndentation(options: PrintOptions): string {\n return (\n this.#mainIndentation(options) +\n (options?.argIndentation ?? DEFAULT_ARG_INDENTATION)\n );\n }\n\n #lineWrapSeparator(options: PrintOptions): string {\n return LINE_WRAP_LINE_END + this.#argIndentation(options);\n }\n\n #argPairSeparator(options: PrintOptions): string {\n switch (options?.argumentLineWrapping ?? DEFAULT_ARGUMENT_LINE_WRAPPING) {\n case \"by-entry\": {\n return INLINE_SEPARATOR;\n }\n case \"nested-by-entry\": {\n return this.#lineWrapSeparator(options) + this.#argIndentation(options);\n }\n case \"by-argument\": {\n return this.#lineWrapSeparator(options);\n }\n case \"inline\": {\n return INLINE_SEPARATOR;\n }\n default:\n throw new Error(\"Invalid argument line wrapping argument.\");\n }\n }\n\n #intraEntrySeparator(options: PrintOptions): string {\n switch (options?.argumentLineWrapping ?? DEFAULT_ARGUMENT_LINE_WRAPPING) {\n case \"by-entry\":\n case \"nested-by-entry\":\n case \"by-argument\": {\n return LINE_WRAP_LINE_END + this.#argIndentation(options);\n }\n case \"inline\": {\n return INLINE_SEPARATOR;\n }\n default:\n throw new Error(\"Invalid argument line wrapping argument.\");\n }\n }\n\n #separatorAfterCommand(\n options: PrintOptions,\n numFollowingEntries: number,\n ): string {\n if (numFollowingEntries === 0) {\n return \"\";\n }\n if (options.skipLineWrapBeforeFirstArg ?? false) {\n return INLINE_SEPARATOR;\n }\n return this.#intraEntrySeparator(options);\n }\n\n public getPrintableCommand(options?: PrintOptions): string {\n // TODO: Why in the world does TypeScript not give the `options` arg the type of `PrintOptions | undefined`???\n options ??= {};\n const serializedEntries: string[] = [];\n\n for (let i = 0; i < this.args.length; i++) {\n const argsEntry = this.args[i];\n\n if (isString(argsEntry)) {\n serializedEntries.push(this.#escapeArg(argsEntry, false, options));\n } else {\n serializedEntries.push(\n argsEntry\n .map((part) => this.#escapeArg(part, false, options))\n .join(this.#argPairSeparator(options)),\n );\n }\n }\n\n let text =\n this.#mainIndentation(options) +\n this.#escapeArg(this.commandName, true, options) +\n this.#separatorAfterCommand(options, serializedEntries.length) +\n serializedEntries.join(this.#intraEntrySeparator(options));\n if (options?.styleTextFormat) {\n text = styleText(options.styleTextFormat, text);\n }\n return text;\n }\n\n public print(options?: PrintOptions): PrintableShellCommand {\n console.log(this.getPrintableCommand(options));\n return this;\n }\n\n /**\n * The returned child process includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313\n */\n public spawn<\n Stdin extends NodeStdioNull | NodeStdioPipe,\n Stdout extends NodeStdioNull | NodeStdioPipe,\n Stderr extends NodeStdioNull | NodeStdioPipe,\n >(\n options?:\n | NodeWithCwd<NodeSpawnOptions>\n | NodeWithCwd<NodeSpawnOptionsWithoutStdio>\n | NodeWithCwd<NodeSpawnOptionsWithStdioTuple<Stdin, Stdout, Stderr>>,\n ): // TODO: figure out how to return `ChildProcessByStdio<\u2026>` without duplicating fragile boilerplate.\n NodeChildProcess & { success: Promise<void> } {\n const { spawn } = process.getBuiltinModule(\"node:child_process\");\n const cwd = (() => {\n if (typeof options?.cwd !== \"undefined\") {\n if (options.cwd instanceof Path) {\n return options.cwd.toString();\n }\n }\n return options?.cwd;\n })();\n // biome-ignore lint/suspicious/noTsIgnore: We don't want linting to depend on *broken* type checking.\n // @ts-ignore: The TypeScript checker has trouble reconciling the optional (i.e. potentially `undefined`) `options` with the third argument.\n const subprocess = spawn(...this.forNode(), {\n ...options,\n cwd,\n }) as NodeChildProcess & {\n success: Promise<void>;\n };\n Object.defineProperty(subprocess, \"success\", {\n get() {\n return new Promise<void>((resolve, reject) =>\n this.addListener(\n \"exit\",\n (exitCode: number /* we only use the first arg */) => {\n if (exitCode === 0) {\n resolve();\n } else {\n reject(`Command failed with non-zero exit code: ${exitCode}`);\n }\n },\n ),\n );\n },\n enumerable: false,\n });\n return subprocess;\n }\n\n /** A wrapper for `.spawnNode(\u2026)` that sets stdio to `\"inherit\"` (common for\n * invoking commands from scripts whose output and interaction should be\n * surfaced to the user). */\n public spawnInherit(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): NodeChildProcess & { success: Promise<void> } {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n return this.spawn({ ...options, stdio: \"inherit\" });\n }\n\n public stdout(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Response {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n const subprocess = this.spawn({\n ...options,\n stdio: [\"ignore\", \"pipe\", \"inherit\"],\n });\n\n // biome-ignore lint/style/noNonNullAssertion: dude\n return new Response(Readable.toWeb(subprocess.stdout!));\n }\n\n /** Equivalent to:\n *\n * ```\n * await this.print().spawnInherit(\u2026).success;\n * ```\n */\n public async shellOut(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Promise<void> {\n await this.print().spawnInherit(options).success;\n }\n\n /**\n * The returned subprocess includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313\n */\n #spawnBun<\n const In extends BunSpawnOptions.Writable = \"ignore\",\n const Out extends BunSpawnOptions.Readable = \"pipe\",\n const Err extends BunSpawnOptions.Readable = \"inherit\",\n >(\n options?: BunWithCwd<\n Omit<BunSpawnOptions.OptionsObject<In, Out, Err>, \"cmd\">\n >,\n ): BunSubprocess<In, Out, Err> & { success: Promise<void> } {\n if (options && \"cmd\" in options) {\n throw new Error(\"Unexpected `cmd` field.\");\n }\n const { spawn } = process.getBuiltinModule(\"bun\") as typeof import(\"bun\");\n const cwd = (() => {\n if (typeof options?.cwd !== \"undefined\") {\n if (options.cwd instanceof Path) {\n return options.cwd.toString();\n }\n }\n return options?.cwd;\n })();\n const subprocess = spawn({\n ...options,\n cwd,\n cmd: this.forBun(),\n }) as BunSubprocess<In, Out, Err> & { success: Promise<void> };\n Object.defineProperty(subprocess, \"success\", {\n get() {\n return new Promise<void>((resolve, reject) =>\n this.exited\n .then((exitCode: number) => {\n if (exitCode === 0) {\n resolve();\n } else {\n reject(\n new Error(\n `Command failed with non-zero exit code: ${exitCode}`,\n ),\n );\n }\n })\n .catch(reject),\n );\n },\n enumerable: false,\n });\n return subprocess;\n }\n\n #spawnBunInherit(\n options?: BunWithCwd<\n Omit<\n BunSpawnOptions.OptionsObject<\"inherit\", \"inherit\", \"inherit\">,\n \"cmd\" | \"stdio\"\n >\n >,\n ): BunSubprocess<\"inherit\", \"inherit\", \"inherit\"> & {\n success: Promise<void>;\n } {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n return this.bun.spawnBun({\n ...options,\n stdio: [\"inherit\", \"inherit\", \"inherit\"],\n });\n }\n\n #spawnBunStdout(\n options?: BunWithCwd<\n Omit<\n BunSpawnOptions.OptionsObject<\"inherit\", \"inherit\", \"inherit\">,\n \"cmd\" | \"stdio\"\n >\n >,\n ): Response {\n // biome-ignore lint/suspicious/noExplicitAny: Avoid breaking the lib check when used without `@types/bun`.\n return new Response((this.bun.spawnBun(options) as any).stdout);\n }\n\n async #shellOutBun(\n options?: BunWithCwd<\n Omit<\n BunSpawnOptions.OptionsObject<\"inherit\", \"inherit\", \"inherit\">,\n \"cmd\" | \"stdio\"\n >\n >,\n ): Promise<void> {\n await this.print().bun.spawnBunInherit(options).success;\n }\n\n bun = {\n /** Equivalent to:\n *\n * ```\n * await this.print().bun.spawnBunInherit(\u2026).success;\n * ```\n */\n spawnBun: this.#spawnBun.bind(this),\n /**\n * A wrapper for `.spawnBunInherit(\u2026)` that sets stdio to `\"inherit\"` (common\n * for invoking commands from scripts whose output and interaction should be\n * surfaced to the user).\n */\n spawnBunInherit: this.#spawnBunInherit.bind(this),\n /** Equivalent to:\n *\n * ```\n * new Response(this.bun.spawnBun(\u2026).stdout);\n * ```\n */\n spawnBunStdout: this.#spawnBunStdout.bind(this),\n shellOutBun: this.#shellOutBun.bind(this),\n };\n}\n"],
5
- "mappings": ";AASA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAM1B,SAAS,YAAY;AAGrB,IAAM,2BAA2B;AACjC,IAAM,0BAA0B;AAChC,IAAM,iCAAiC;AAEvC,IAAM,mBAAmB;AACzB,IAAM,qBAAqB;AAG3B,SAAS,SAAS,GAAqB;AACrC,SAAO,OAAO,MAAM;AACtB;AAEA,SAAS,cAAc,SAAqC;AAC1D,aAAW,SAAS,SAAS;AAC3B,QAAI,CAAC,SAAS,KAAK,GAAG;AACpB,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AA2CA,IAAM,2BAA2B,oBAAI,IAAI;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA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AAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOJ,UAAU,KAAK,UAAU,KAAK,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMlC,iBAAiB,KAAK,iBAAiB,KAAK,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOhD,gBAAgB,KAAK,gBAAgB,KAAK,IAAI;AAAA,IAC9C,aAAa,KAAK,aAAa,KAAK,IAAI;AAAA,EAC1C;AACF;",
4
+ "sourcesContent": ["import type {\n ChildProcess as NodeChildProcess,\n SpawnOptions as NodeSpawnOptions,\n SpawnOptionsWithoutStdio as NodeSpawnOptionsWithoutStdio,\n SpawnOptionsWithStdioTuple as NodeSpawnOptionsWithStdioTuple,\n StdioNull as NodeStdioNull,\n StdioPipe as NodeStdioPipe,\n ProcessEnvOptions,\n} from \"node:child_process\";\nimport { Readable } from \"node:stream\";\nimport { styleText } from \"node:util\";\nimport type {\n SpawnOptions as BunSpawnOptions,\n Subprocess as BunSubprocess,\n SpawnOptions,\n} from \"bun\";\nimport { Path, stringifyIfPath } from \"path-class\";\nimport type { SetFieldType } from \"type-fest\";\n\nconst DEFAULT_MAIN_INDENTATION = \"\";\nconst DEFAULT_ARG_INDENTATION = \" \";\nconst DEFAULT_ARGUMENT_LINE_WRAPPING = \"by-entry\";\n\nconst INLINE_SEPARATOR = \" \";\nconst LINE_WRAP_LINE_END = \" \\\\\\n\";\n\n// biome-ignore lint/suspicious/noExplicitAny: This is the correct type nere.\nfunction isString(s: any): s is string {\n return typeof s === \"string\";\n}\n// biome-ignore lint/suspicious/noExplicitAny: This is the correct type nere.\nfunction isStringArray(entries: any[]): entries is string[] {\n for (const entry of entries) {\n if (!isString(entry)) {\n return false;\n }\n }\n return true;\n}\n\n// TODO: allow `.toString()`ables?\ntype SingleArgument = string | Path;\ntype ArgsEntry = SingleArgument | SingleArgument[];\ntype Args = ArgsEntry[];\n\nexport interface PrintOptions {\n /** Defaults to \"\" */\n mainIndentation?: string;\n /** Defaults to \" \" */\n argIndentation?: string;\n /**\n * - `\"auto\"`: Quote only arguments that need it for safety. This tries to be\n * portable and safe across shells, but true safety and portability is hard\n * to guarantee.\n * - `\"extra-safe\"`: Quote all arguments, even ones that don't need it. This is\n * more likely to be safe under all circumstances.\n */\n quoting?: \"auto\" | \"extra-safe\";\n /** Line wrapping to use between arguments. Defaults to `\"by-entry\"`. */\n argumentLineWrapping?:\n | \"by-entry\"\n | \"nested-by-entry\"\n | \"by-argument\"\n | \"inline\";\n /** Include the first arg (or first arg group) on the same line as the command, regardless of the `argumentLineWrapping` setting. */\n skipLineWrapBeforeFirstArg?: true | false;\n /**\n * Style text using `node`'s [`styleText(\u2026)`](https://nodejs.org/api/util.html#utilstyletextformat-text-options)\n *\n * Example usage:\n *\n * ```\n * new PrintableShellCommand(\"echo\", [\"hi\"]).print({\n * styleTextFormat: [\"gray\", \"bold\"],\n * });\n * */\n styleTextFormat?: Parameters<typeof styleText>[0];\n}\n\n// https://mywiki.wooledge.org/BashGuide/SpecialCharacters\nconst SPECIAL_SHELL_CHARACTERS = new Set([\n \" \",\n '\"',\n \"'\",\n \"`\",\n \"|\",\n \"$\",\n \"*\",\n \"?\",\n \">\",\n \"<\",\n \"(\",\n \")\",\n \"[\",\n \"]\",\n \"{\",\n \"}\",\n \"&\",\n \"\\\\\",\n \";\",\n \"#\",\n]);\n\n// https://mywiki.wooledge.org/BashGuide/SpecialCharacters\nconst SPECIAL_SHELL_CHARACTERS_FOR_MAIN_COMMAND =\n // biome-ignore lint/suspicious/noExplicitAny: Workaround to make this package easier to use in a project that otherwise only uses ES2022.)\n (SPECIAL_SHELL_CHARACTERS as unknown as any).union(new Set([\"=\"]));\n\ntype NodeCwd = ProcessEnvOptions[\"cwd\"] | Path;\ntype NodeWithCwd<T extends { cwd?: ProcessEnvOptions[\"cwd\"] }> = SetFieldType<\n T,\n \"cwd\",\n NodeCwd | undefined\n>;\n\n// biome-ignore lint/suspicious/noExplicitAny: Just matching\ntype BunCwd = SpawnOptions.OptionsObject<any, any, any>[\"cwd\"] | Path;\ntype BunWithCwd<\n // biome-ignore lint/suspicious/noExplicitAny: Just matching\n T extends { cwd?: SpawnOptions.OptionsObject<any, any, any>[\"cwd\"] },\n> = SetFieldType<T, \"cwd\", BunCwd | undefined>;\n\nexport class PrintableShellCommand {\n #commandName: string;\n constructor(\n commandName: string,\n private args: Args = [],\n ) {\n if (!isString(commandName)) {\n // biome-ignore lint/suspicious/noExplicitAny: We want to print this, no matter what it is.\n throw new Error(\"Command name is not a string:\", commandName as any);\n }\n this.#commandName = commandName;\n if (typeof args === \"undefined\") {\n return;\n }\n if (!Array.isArray(args)) {\n throw new Error(\"Command arguments are not an array\");\n }\n for (let i = 0; i < args.length; i++) {\n const argEntry = args[i];\n if (typeof argEntry === \"string\") {\n continue;\n }\n if (Array.isArray(argEntry) && isStringArray(argEntry)) {\n continue;\n }\n throw new Error(`Invalid arg entry at index: ${i}`);\n }\n }\n\n get commandName(): string {\n return this.#commandName;\n }\n\n /** For use with `bun`.\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"bun\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * await spawn(command.toFlatCommand()).exited;\n * ```\n */\n public toFlatCommand(): string[] {\n return [this.commandName, ...this.args.flat().map(stringifyIfPath)];\n }\n\n /**\n * Convenient alias for `toFlatCommand()`.\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"bun\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * await spawn(command.forBun()).exited;\n * ```\n *\n * */\n public forBun(): string[] {\n return this.toFlatCommand();\n }\n\n /**\n * For use with `node:child_process`\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"node:child_process\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * const child_process = spawn(...command.toCommandWithFlatArgs()); // Note the `...`\n * ```\n *\n */\n public toCommandWithFlatArgs(): [string, string[]] {\n return [this.commandName, this.args.flat().map(stringifyIfPath)];\n }\n\n /**\n * For use with `node:child_process`\n *\n * Usage example:\n *\n * ```\n * import { PrintableShellCommand } from \"printable-shell-command\";\n * import { spawn } from \"node:child_process\";\n *\n * const command = new PrintableShellCommand( \u2026 );\n * const child_process = spawn(...command.forNode()); // Note the `...`\n * ```\n *\n * Convenient alias for `toCommandWithFlatArgs()`.\n */\n public forNode(): [string, string[]] {\n return this.toCommandWithFlatArgs();\n }\n\n #escapeArg(\n arg: string,\n isMainCommand: boolean,\n options: PrintOptions,\n ): string {\n const argCharacters = new Set(arg);\n const specialShellCharacters = isMainCommand\n ? SPECIAL_SHELL_CHARACTERS_FOR_MAIN_COMMAND\n : SPECIAL_SHELL_CHARACTERS;\n if (\n options?.quoting === \"extra-safe\" ||\n // biome-ignore lint/suspicious/noExplicitAny: Workaround to make this package easier to use in a project that otherwise only uses ES2022.)\n (argCharacters as unknown as any).intersection(specialShellCharacters)\n .size > 0\n ) {\n // Use single quote to reduce the need to escape (and therefore reduce the chance for bugs/security issues).\n const escaped = arg.replaceAll(\"\\\\\", \"\\\\\\\\\").replaceAll(\"'\", \"\\\\'\");\n return `'${escaped}'`;\n }\n return arg;\n }\n\n #mainIndentation(options: PrintOptions): string {\n return options?.mainIndentation ?? DEFAULT_MAIN_INDENTATION;\n }\n\n #argIndentation(options: PrintOptions): string {\n return (\n this.#mainIndentation(options) +\n (options?.argIndentation ?? DEFAULT_ARG_INDENTATION)\n );\n }\n\n #lineWrapSeparator(options: PrintOptions): string {\n return LINE_WRAP_LINE_END + this.#argIndentation(options);\n }\n\n #argPairSeparator(options: PrintOptions): string {\n switch (options?.argumentLineWrapping ?? DEFAULT_ARGUMENT_LINE_WRAPPING) {\n case \"by-entry\": {\n return INLINE_SEPARATOR;\n }\n case \"nested-by-entry\": {\n return this.#lineWrapSeparator(options) + this.#argIndentation(options);\n }\n case \"by-argument\": {\n return this.#lineWrapSeparator(options);\n }\n case \"inline\": {\n return INLINE_SEPARATOR;\n }\n default:\n throw new Error(\"Invalid argument line wrapping argument.\");\n }\n }\n\n #intraEntrySeparator(options: PrintOptions): string {\n switch (options?.argumentLineWrapping ?? DEFAULT_ARGUMENT_LINE_WRAPPING) {\n case \"by-entry\":\n case \"nested-by-entry\":\n case \"by-argument\": {\n return LINE_WRAP_LINE_END + this.#argIndentation(options);\n }\n case \"inline\": {\n return INLINE_SEPARATOR;\n }\n default:\n throw new Error(\"Invalid argument line wrapping argument.\");\n }\n }\n\n #separatorAfterCommand(\n options: PrintOptions,\n numFollowingEntries: number,\n ): string {\n if (numFollowingEntries === 0) {\n return \"\";\n }\n if (options.skipLineWrapBeforeFirstArg ?? false) {\n return INLINE_SEPARATOR;\n }\n return this.#intraEntrySeparator(options);\n }\n\n public getPrintableCommand(options?: PrintOptions): string {\n // TODO: Why in the world does TypeScript not give the `options` arg the type of `PrintOptions | undefined`???\n options ??= {};\n const serializedEntries: string[] = [];\n\n for (let i = 0; i < this.args.length; i++) {\n const argsEntry = stringifyIfPath(this.args[i]);\n\n if (isString(argsEntry)) {\n serializedEntries.push(this.#escapeArg(argsEntry, false, options));\n } else {\n serializedEntries.push(\n argsEntry\n .map((part) =>\n this.#escapeArg(stringifyIfPath(part), false, options),\n )\n .join(this.#argPairSeparator(options)),\n );\n }\n }\n\n let text =\n this.#mainIndentation(options) +\n this.#escapeArg(this.commandName, true, options) +\n this.#separatorAfterCommand(options, serializedEntries.length) +\n serializedEntries.join(this.#intraEntrySeparator(options));\n if (options?.styleTextFormat) {\n text = styleText(options.styleTextFormat, text);\n }\n return text;\n }\n\n public print(options?: PrintOptions): PrintableShellCommand {\n console.log(this.getPrintableCommand(options));\n return this;\n }\n\n /**\n * The returned child process includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313\n */\n public spawn<\n Stdin extends NodeStdioNull | NodeStdioPipe,\n Stdout extends NodeStdioNull | NodeStdioPipe,\n Stderr extends NodeStdioNull | NodeStdioPipe,\n >(\n options?:\n | NodeWithCwd<NodeSpawnOptions>\n | NodeWithCwd<NodeSpawnOptionsWithoutStdio>\n | NodeWithCwd<NodeSpawnOptionsWithStdioTuple<Stdin, Stdout, Stderr>>,\n ): // TODO: figure out how to return `ChildProcessByStdio<\u2026>` without duplicating fragile boilerplate.\n NodeChildProcess & { success: Promise<void> } {\n const { spawn } = process.getBuiltinModule(\"node:child_process\");\n const cwd = stringifyIfPath(options?.cwd);\n // biome-ignore lint/suspicious/noTsIgnore: We don't want linting to depend on *broken* type checking.\n // @ts-ignore: The TypeScript checker has trouble reconciling the optional (i.e. potentially `undefined`) `options` with the third argument.\n const subprocess = spawn(...this.forNode(), {\n ...options,\n cwd,\n }) as NodeChildProcess & {\n success: Promise<void>;\n };\n Object.defineProperty(subprocess, \"success\", {\n get() {\n return new Promise<void>((resolve, reject) =>\n this.addListener(\n \"exit\",\n (exitCode: number /* we only use the first arg */) => {\n if (exitCode === 0) {\n resolve();\n } else {\n reject(`Command failed with non-zero exit code: ${exitCode}`);\n }\n },\n ),\n );\n },\n enumerable: false,\n });\n return subprocess;\n }\n\n /** A wrapper for `.spawn(\u2026)` that sets stdio to `\"inherit\"` (common for\n * invoking commands from scripts whose output and interaction should be\n * surfaced to the user).\n *\n * If there is no other interaction with the shell from the calling process,\n * then it acts \"transparent\" and allows user to interact with the subprocess\n * in its stead.\n */\n public spawnTransparently(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): NodeChildProcess & { success: Promise<void> } {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n return this.spawn({ ...options, stdio: \"inherit\" });\n }\n\n /** @deprecated: Use `.spawnTransparently(\u2026)`. */\n public spawnInherit(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): NodeChildProcess & { success: Promise<void> } {\n return this.spawnTransparently(options);\n }\n\n /** A wrapper for `.spawn(\u2026)` that:\n *\n * - sets `detached` to `true`,\n * - sets stdio to `\"inherit\"`,\n * - calls `.unref()`, and\n * - does not wait for the process to exit.\n *\n * This is similar to starting a command int he background and disowning it (in a shell).\n *\n * The `stdio` field is left overridable. To capture `stdout` and `stderr`, connect them to output files like this:\n *\n * import { open } from \"node:fs/promises\";\n * import { Path } from \"path-class\";\n * import { PrintableShellCommand } from \"printable-shell-command\";\n *\n * const tempDir = await Path.makeTempDir();\n * console.log(`Temp dir: ${tempDir}`);\n * const stdout = await open(tempDir.join(\"stdout.log\").path, \"a\");\n * const stderr = await open(tempDir.join(\"stderr.log\").path, \"a\");\n *\n * new PrintableShellCommand(\"echo\", [\"hi\"]).spawnDetached({\n * stdio: [\"ignore\", stdout.fd, stderr.fd],\n * });\n *\n */\n public spawnDetached(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"detached\">>,\n ): void {\n if (options && \"detached\" in options) {\n throw new Error(\"Unexpected `detached` field.\");\n }\n const childProcess = this.spawn({\n stdio: \"ignore\",\n ...options,\n detached: true,\n });\n childProcess.unref();\n }\n\n public stdout(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Response {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n const subprocess = this.spawn({\n ...options,\n stdio: [\"ignore\", \"pipe\", \"inherit\"],\n });\n\n // biome-ignore lint/style/noNonNullAssertion: dude\n return new Response(Readable.toWeb(subprocess.stdout!));\n }\n\n /** Equivalent to:\n *\n * ```\n * await this.print().spawnTransparently(\u2026).success;\n * ```\n */\n public async shellOut(\n options?: NodeWithCwd<Omit<NodeSpawnOptions, \"stdio\">>,\n ): Promise<void> {\n await this.print().spawnTransparently(options).success;\n }\n\n /**\n * The returned subprocess includes a `.success` `Promise` field, per https://github.com/oven-sh/bun/issues/8313\n */\n #spawnBun<\n const In extends BunSpawnOptions.Writable = \"ignore\",\n const Out extends BunSpawnOptions.Readable = \"pipe\",\n const Err extends BunSpawnOptions.Readable = \"inherit\",\n >(\n options?: BunWithCwd<\n Omit<BunSpawnOptions.OptionsObject<In, Out, Err>, \"cmd\">\n >,\n ): BunSubprocess<In, Out, Err> & { success: Promise<void> } {\n if (options && \"cmd\" in options) {\n throw new Error(\"Unexpected `cmd` field.\");\n }\n const { spawn } = process.getBuiltinModule(\"bun\") as typeof import(\"bun\");\n const cwd = (() => {\n if (typeof options?.cwd !== \"undefined\") {\n if (options.cwd instanceof Path) {\n return options.cwd.toString();\n }\n }\n return options?.cwd;\n })();\n const subprocess = spawn({\n ...options,\n cwd,\n cmd: this.forBun(),\n }) as BunSubprocess<In, Out, Err> & { success: Promise<void> };\n Object.defineProperty(subprocess, \"success\", {\n get() {\n return new Promise<void>((resolve, reject) =>\n this.exited\n .then((exitCode: number) => {\n if (exitCode === 0) {\n resolve();\n } else {\n reject(\n new Error(\n `Command failed with non-zero exit code: ${exitCode}`,\n ),\n );\n }\n })\n .catch(reject),\n );\n },\n enumerable: false,\n });\n return subprocess;\n }\n\n #spawnBunInherit(\n options?: BunWithCwd<\n Omit<\n BunSpawnOptions.OptionsObject<\"inherit\", \"inherit\", \"inherit\">,\n \"cmd\" | \"stdio\"\n >\n >,\n ): BunSubprocess<\"inherit\", \"inherit\", \"inherit\"> & {\n success: Promise<void>;\n } {\n if (options && \"stdio\" in options) {\n throw new Error(\"Unexpected `stdio` field.\");\n }\n return this.bun.spawnBun({\n ...options,\n stdio: [\"inherit\", \"inherit\", \"inherit\"],\n });\n }\n\n #spawnBunStdout(\n options?: BunWithCwd<\n Omit<\n BunSpawnOptions.OptionsObject<\"inherit\", \"inherit\", \"inherit\">,\n \"cmd\" | \"stdio\"\n >\n >,\n ): Response {\n // biome-ignore lint/suspicious/noExplicitAny: Avoid breaking the lib check when used without `@types/bun`.\n return new Response((this.bun.spawnBun(options) as any).stdout);\n }\n\n async #shellOutBun(\n options?: BunWithCwd<\n Omit<\n BunSpawnOptions.OptionsObject<\"inherit\", \"inherit\", \"inherit\">,\n \"cmd\" | \"stdio\"\n >\n >,\n ): Promise<void> {\n await this.print().bun.spawnBunInherit(options).success;\n }\n\n bun = {\n /** Equivalent to:\n *\n * ```\n * await this.print().bun.spawnBunInherit(\u2026).success;\n * ```\n */\n spawnBun: this.#spawnBun.bind(this),\n /**\n * A wrapper for `.spawnBunInherit(\u2026)` that sets stdio to `\"inherit\"` (common\n * for invoking commands from scripts whose output and interaction should be\n * surfaced to the user).\n */\n spawnBunInherit: this.#spawnBunInherit.bind(this),\n /** Equivalent to:\n *\n * ```\n * new Response(this.bun.spawnBun(\u2026).stdout);\n * ```\n */\n spawnBunStdout: this.#spawnBunStdout.bind(this),\n shellOutBun: this.#shellOutBun.bind(this),\n };\n}\n"],
5
+ "mappings": ";AASA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAM1B,SAAS,MAAM,uBAAuB;AAGtC,IAAM,2BAA2B;AACjC,IAAM,0BAA0B;AAChC,IAAM,iCAAiC;AAEvC,IAAM,mBAAmB;AACzB,IAAM,qBAAqB;AAG3B,SAAS,SAAS,GAAqB;AACrC,SAAO,OAAO,MAAM;AACtB;AAEA,SAAS,cAAc,SAAqC;AAC1D,aAAW,SAAS,SAAS;AAC3B,QAAI,CAAC,SAAS,KAAK,GAAG;AACpB,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AA0CA,IAAM,2BAA2B,oBAAI,IAAI;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAGD,IAAM;AAAA;AAAA,EAEH,yBAA4C,MAAM,oBAAI,IAAI,CAAC,GAAG,CAAC,CAAC;AAAA;AAgB5D,IAAM,wBAAN,MAA4B;AAAA,EAEjC,YACE,aACQ,OAAa,CAAC,GACtB;AADQ;AAER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AA,EACH;AAAA,EAEA,gBACE,SAMU;AAEV,WAAO,IAAI,SAAU,KAAK,IAAI,SAAS,OAAO,EAAU,MAAM;AAAA,EAChE;AAAA,EAEA,MAAM,aACJ,SAMe;AACf,UAAM,KAAK,MAAM,EAAE,IAAI,gBAAgB,OAAO,EAAE;AAAA,EAClD;AAAA,EAEA,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOJ,UAAU,KAAK,UAAU,KAAK,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMlC,iBAAiB,KAAK,iBAAiB,KAAK,IAAI;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOhD,gBAAgB,KAAK,gBAAgB,KAAK,IAAI;AAAA,IAC9C,aAAa,KAAK,aAAa,KAAK,IAAI;AAAA,EAC1C;AACF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "printable-shell-command",
3
- "version": "2.1.2",
3
+ "version": "2.3.0",
4
4
  "main": "./src/index.ts",
5
5
  "devDependencies": {
6
6
  "@biomejs/biome": "^2.1.4",
@@ -29,7 +29,7 @@
29
29
  "prepublishOnly": "make prepublishOnly"
30
30
  },
31
31
  "dependencies": {
32
- "path-class": "^0.3.3",
32
+ "path-class": "^0.6.1",
33
33
  "type-fest": "^5.1.0"
34
34
  }
35
35
  }
package/src/index.ts CHANGED
@@ -14,7 +14,7 @@ import type {
14
14
  Subprocess as BunSubprocess,
15
15
  SpawnOptions,
16
16
  } from "bun";
17
- import { Path } from "path-class";
17
+ import { Path, stringifyIfPath } from "path-class";
18
18
  import type { SetFieldType } from "type-fest";
19
19
 
20
20
  const DEFAULT_MAIN_INDENTATION = "";
@@ -39,9 +39,8 @@ function isStringArray(entries: any[]): entries is string[] {
39
39
  }
40
40
 
41
41
  // TODO: allow `.toString()`ables?
42
- type SingleArgument = string;
43
- type FlagArgumentGroup = string[];
44
- type ArgsEntry = SingleArgument | FlagArgumentGroup;
42
+ type SingleArgument = string | Path;
43
+ type ArgsEntry = SingleArgument | SingleArgument[];
45
44
  type Args = ArgsEntry[];
46
45
 
47
46
  export interface PrintOptions {
@@ -167,7 +166,7 @@ export class PrintableShellCommand {
167
166
  * ```
168
167
  */
169
168
  public toFlatCommand(): string[] {
170
- return [this.commandName, ...this.args.flat()];
169
+ return [this.commandName, ...this.args.flat().map(stringifyIfPath)];
171
170
  }
172
171
 
173
172
  /**
@@ -203,7 +202,7 @@ export class PrintableShellCommand {
203
202
  *
204
203
  */
205
204
  public toCommandWithFlatArgs(): [string, string[]] {
206
- return [this.commandName, this.args.flat()];
205
+ return [this.commandName, this.args.flat().map(stringifyIfPath)];
207
206
  }
208
207
 
209
208
  /**
@@ -315,14 +314,16 @@ export class PrintableShellCommand {
315
314
  const serializedEntries: string[] = [];
316
315
 
317
316
  for (let i = 0; i < this.args.length; i++) {
318
- const argsEntry = this.args[i];
317
+ const argsEntry = stringifyIfPath(this.args[i]);
319
318
 
320
319
  if (isString(argsEntry)) {
321
320
  serializedEntries.push(this.#escapeArg(argsEntry, false, options));
322
321
  } else {
323
322
  serializedEntries.push(
324
323
  argsEntry
325
- .map((part) => this.#escapeArg(part, false, options))
324
+ .map((part) =>
325
+ this.#escapeArg(stringifyIfPath(part), false, options),
326
+ )
326
327
  .join(this.#argPairSeparator(options)),
327
328
  );
328
329
  }
@@ -359,14 +360,7 @@ export class PrintableShellCommand {
359
360
  ): // TODO: figure out how to return `ChildProcessByStdio<…>` without duplicating fragile boilerplate.
360
361
  NodeChildProcess & { success: Promise<void> } {
361
362
  const { spawn } = process.getBuiltinModule("node:child_process");
362
- const cwd = (() => {
363
- if (typeof options?.cwd !== "undefined") {
364
- if (options.cwd instanceof Path) {
365
- return options.cwd.toString();
366
- }
367
- }
368
- return options?.cwd;
369
- })();
363
+ const cwd = stringifyIfPath(options?.cwd);
370
364
  // biome-ignore lint/suspicious/noTsIgnore: We don't want linting to depend on *broken* type checking.
371
365
  // @ts-ignore: The TypeScript checker has trouble reconciling the optional (i.e. potentially `undefined`) `options` with the third argument.
372
366
  const subprocess = spawn(...this.forNode(), {
@@ -395,10 +389,15 @@ export class PrintableShellCommand {
395
389
  return subprocess;
396
390
  }
397
391
 
398
- /** A wrapper for `.spawnNode(…)` that sets stdio to `"inherit"` (common for
392
+ /** A wrapper for `.spawn(…)` that sets stdio to `"inherit"` (common for
399
393
  * invoking commands from scripts whose output and interaction should be
400
- * surfaced to the user). */
401
- public spawnInherit(
394
+ * surfaced to the user).
395
+ *
396
+ * If there is no other interaction with the shell from the calling process,
397
+ * then it acts "transparent" and allows user to interact with the subprocess
398
+ * in its stead.
399
+ */
400
+ public spawnTransparently(
402
401
  options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>,
403
402
  ): NodeChildProcess & { success: Promise<void> } {
404
403
  if (options && "stdio" in options) {
@@ -407,6 +406,52 @@ export class PrintableShellCommand {
407
406
  return this.spawn({ ...options, stdio: "inherit" });
408
407
  }
409
408
 
409
+ /** @deprecated: Use `.spawnTransparently(…)`. */
410
+ public spawnInherit(
411
+ options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>,
412
+ ): NodeChildProcess & { success: Promise<void> } {
413
+ return this.spawnTransparently(options);
414
+ }
415
+
416
+ /** A wrapper for `.spawn(…)` that:
417
+ *
418
+ * - sets `detached` to `true`,
419
+ * - sets stdio to `"inherit"`,
420
+ * - calls `.unref()`, and
421
+ * - does not wait for the process to exit.
422
+ *
423
+ * This is similar to starting a command int he background and disowning it (in a shell).
424
+ *
425
+ * The `stdio` field is left overridable. To capture `stdout` and `stderr`, connect them to output files like this:
426
+ *
427
+ * import { open } from "node:fs/promises";
428
+ * import { Path } from "path-class";
429
+ * import { PrintableShellCommand } from "printable-shell-command";
430
+ *
431
+ * const tempDir = await Path.makeTempDir();
432
+ * console.log(`Temp dir: ${tempDir}`);
433
+ * const stdout = await open(tempDir.join("stdout.log").path, "a");
434
+ * const stderr = await open(tempDir.join("stderr.log").path, "a");
435
+ *
436
+ * new PrintableShellCommand("echo", ["hi"]).spawnDetached({
437
+ * stdio: ["ignore", stdout.fd, stderr.fd],
438
+ * });
439
+ *
440
+ */
441
+ public spawnDetached(
442
+ options?: NodeWithCwd<Omit<NodeSpawnOptions, "detached">>,
443
+ ): void {
444
+ if (options && "detached" in options) {
445
+ throw new Error("Unexpected `detached` field.");
446
+ }
447
+ const childProcess = this.spawn({
448
+ stdio: "ignore",
449
+ ...options,
450
+ detached: true,
451
+ });
452
+ childProcess.unref();
453
+ }
454
+
410
455
  public stdout(
411
456
  options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>,
412
457
  ): Response {
@@ -425,13 +470,13 @@ export class PrintableShellCommand {
425
470
  /** Equivalent to:
426
471
  *
427
472
  * ```
428
- * await this.print().spawnInherit(…).success;
473
+ * await this.print().spawnTransparently(…).success;
429
474
  * ```
430
475
  */
431
476
  public async shellOut(
432
477
  options?: NodeWithCwd<Omit<NodeSpawnOptions, "stdio">>,
433
478
  ): Promise<void> {
434
- await this.print().spawnInherit(options).success;
479
+ await this.print().spawnTransparently(options).success;
435
480
  }
436
481
 
437
482
  /**