@magic/cli 0.0.52 → 0.0.53

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
@@ -580,6 +580,10 @@ update dependencies
580
580
 
581
581
  - actually publish the types from 0.0.51.
582
582
 
583
- ##### 0.0.53 - unreleased
583
+ ##### 0.0.53
584
+
585
+ - export exec, execFile and spawn option types
586
+
587
+ ##### 0.0.54 - unreleased
584
588
 
585
589
  - ...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic/cli",
3
- "version": "0.0.52",
3
+ "version": "0.0.53",
4
4
  "homepage": "https://magic.github.io/cli",
5
5
  "description": "declarative command line interfaces with aliasing, commands and environment sanitization",
6
6
  "scripts": {
package/src/exec.js CHANGED
@@ -4,10 +4,12 @@ import error from '@magic/error'
4
4
 
5
5
  const libName = '@magic/cli.exec'
6
6
 
7
+ /** @typedef {child_process.ExecOptionsWithStringEncoding & { stderrToStdout?: boolean }} CLIExecOptions */
8
+
7
9
  /**
8
10
  * Executes a shell command using child_process.exec
9
11
  * @param {string} cmd - The shell command to execute.
10
- * @param {child_process.ExecOptionsWithStringEncoding & { stderrToStdout: boolean }} [options = {}] - Execution options.
12
+ * @param {CLIExecOptions} [options = {}] - Execution options.
11
13
  * @returns {Promise<string>} Resolves with stdout or stderr (if stderrToStdout = true), rejects with Error.
12
14
  */
13
15
  export const exec = (cmd, options) =>
package/src/execFile.js CHANGED
@@ -4,15 +4,18 @@ import error from '@magic/error'
4
4
 
5
5
  const libName = '@magic/cli.execFile'
6
6
 
7
+ /** @typedef {child_process.ExecFileOptions & { stderrToStdout?: boolean }} CLIExecFileOptions */
8
+
7
9
  /**
8
10
  * Executes a file using child_process.execFile
9
11
  * @param {string} p - Path to the executable file.
10
12
  * @param {string[]} [args=[]] - Arguments to pass to the executable.
11
- * @param {child_process.ExecFileOptions} [opts={}] - Execution options.
13
+ * @param {CLIExecFileOptions} [options={}] - Execution options.
12
14
  * @returns {Promise<string | Buffer>} Resolves with stdout, rejects with Error.
13
15
  */
14
- export const execFile = (p, args = [], opts = {}) =>
16
+ export const execFile = (p, args = [], options = {}) =>
15
17
  new Promise((resolve, reject) => {
18
+ const { stderrToStdout = false, ...opts } = options
16
19
  child_process.execFile(
17
20
  p,
18
21
  args.filter(a => a),
@@ -23,10 +26,16 @@ export const execFile = (p, args = [], opts = {}) =>
23
26
  reject(err)
24
27
  return
25
28
  }
29
+
26
30
  if (stderr) {
27
- const e = error(new Error(`${libName}: error: ${stderr}`), 'E_EXECFILE_STDERR')
28
- reject(e)
29
- return
31
+ if (stderrToStdout) {
32
+ resolve(stderr)
33
+ return
34
+ } else {
35
+ const e = error(new Error(`${libName}: error: ${stderr}`), 'E_EXECFILE_STDERR')
36
+ reject(e)
37
+ return
38
+ }
30
39
  }
31
40
 
32
41
  resolve(stdout)
package/src/index.js CHANGED
@@ -73,4 +73,8 @@ cli.prompt = promptUser
73
73
  export const execFile = executeFile
74
74
  cli.execFile = executeFile
75
75
 
76
+ /** @typedef {import('./execFile.js').CLIExecFileOptions} CLIExecFileOptions */
77
+ /** @typedef {import('./exec.js').CLIExecOptions} CLIExecOptions */
78
+ /** @typedef {import('./spawn.js')} CLISpawnOptions*/
79
+
76
80
  export default cli
package/src/spawn.js CHANGED
@@ -1,19 +1,21 @@
1
1
  import child_process from 'child_process'
2
2
 
3
+ /** @typedef {import('child_process').SpawnOptions} CLISpawnOptions*/
4
+
3
5
  /**
4
6
  * Spawns a new process.
5
7
  * @param {string} cmd - Command to run.
6
8
  * @param {string[]} [args=[]] - Arguments for the command.
7
- * @param {import('child_process').SpawnOptions} [opt={}] - Spawn options.
9
+ * @param {CLISpawnOptions} [options={}] - Spawn options.
8
10
  * @returns {import('child_process').ChildProcess} The spawned process.
9
11
  */
10
- export const spawn = (cmd, args = [], opt = {}) => {
11
- /** @type {import('child_process').SpawnOptions} */
12
+ export const spawn = (cmd, args = [], options = {}) => {
13
+ /** @type {CLISpawnOptions} */
12
14
  const opts = {
13
15
  cwd: process.cwd(),
14
16
  env: process.env,
15
17
  stdio: 'inherit',
16
- ...opt,
18
+ ...options,
17
19
  }
18
20
 
19
21
  return child_process.spawn(cmd, args, opts)
package/types/exec.d.ts CHANGED
@@ -1,7 +1,5 @@
1
- export function exec(
2
- cmd: string,
3
- options?: child_process.ExecOptionsWithStringEncoding & {
4
- stderrToStdout: boolean
5
- },
6
- ): Promise<string>
1
+ export function exec(cmd: string, options?: CLIExecOptions): Promise<string>
2
+ export type CLIExecOptions = child_process.ExecOptionsWithStringEncoding & {
3
+ stderrToStdout?: boolean
4
+ }
7
5
  import child_process from 'child_process'
@@ -1,6 +1,9 @@
1
1
  export function execFile(
2
2
  p: string,
3
3
  args?: string[],
4
- opts?: child_process.ExecFileOptions,
4
+ options?: CLIExecFileOptions,
5
5
  ): Promise<string | Buffer>
6
+ export type CLIExecFileOptions = child_process.ExecFileOptions & {
7
+ stderrToStdout?: boolean
8
+ }
6
9
  import child_process from 'child_process'
package/types/index.d.ts CHANGED
@@ -8,14 +8,9 @@ export namespace cli {
8
8
  export const spawn: (
9
9
  cmd: string,
10
10
  args?: string[],
11
- opt?: import('child_process').SpawnOptions,
11
+ options?: import('./spawn.js').CLISpawnOptions,
12
12
  ) => import('child_process').ChildProcess
13
- export const exec: (
14
- cmd: string,
15
- options?: import('child_process').ExecOptionsWithStringEncoding & {
16
- stderrToStdout: boolean
17
- },
18
- ) => Promise<string>
13
+ export const exec: (cmd: string, options?: import('./exec.js').CLIExecOptions) => Promise<string>
19
14
  export const prompt: (
20
15
  msg?: string | string[],
21
16
  options?: {
@@ -27,7 +22,7 @@ export const prompt: (
27
22
  export const execFile: (
28
23
  p: string,
29
24
  args?: string[],
30
- opts?: import('child_process').ExecFileOptions,
25
+ options?: import('./execFile.js').CLIExecFileOptions,
31
26
  ) => Promise<string | Buffer>
32
27
  export default cli
33
28
  export type ParseProps = {
@@ -52,6 +47,9 @@ export type ParsedCLI = {
52
47
  commands: Record<string, boolean>
53
48
  errors: Array<string | string[]>
54
49
  }
50
+ export type CLIExecFileOptions = import('./execFile.js').CLIExecFileOptions
51
+ export type CLIExecOptions = import('./exec.js').CLIExecOptions
52
+ export type CLISpawnOptions = typeof import('./spawn.js')
55
53
  import { spawn as spawner } from './spawn.js'
56
54
  import { exec as execute } from './exec.js'
57
55
  import { prompt as promptUser } from './prompt.js'
package/types/spawn.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export function spawn(
2
2
  cmd: string,
3
3
  args?: string[],
4
- opt?: import('child_process').SpawnOptions,
4
+ options?: CLISpawnOptions,
5
5
  ): import('child_process').ChildProcess
6
+ export type CLISpawnOptions = import('child_process').SpawnOptions