codify-plugin-lib 1.0.29 → 1.0.30

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.
@@ -12,6 +12,8 @@ type CodifySpawnOptions = {
12
12
  cwd?: string;
13
13
  stdioString?: boolean;
14
14
  } & SpawnOptions;
15
- export declare function codifySpawn(cmd: string, args?: string[], opts?: Omit<CodifySpawnOptions, 'stdio' | 'stdioString' | 'shell'>, extras?: Record<any, any>): Promise<SpawnResult>;
15
+ export declare function codifySpawn(cmd: string, args?: string[], opts?: Omit<CodifySpawnOptions, 'stdio' | 'stdioString'> & {
16
+ throws?: boolean;
17
+ }, extras?: Record<any, any>): Promise<SpawnResult>;
16
18
  export declare function isDebug(): boolean;
17
19
  export {};
@@ -6,7 +6,7 @@ export var SpawnStatus;
6
6
  })(SpawnStatus || (SpawnStatus = {}));
7
7
  export async function codifySpawn(cmd, args, opts, extras) {
8
8
  try {
9
- const result = await promiseSpawn(cmd, args ?? [], { ...opts, stdio: 'pipe', stdioString: true, shell: true }, extras);
9
+ const result = await promiseSpawn(cmd, args ?? [], { ...opts, stdio: 'pipe', stdioString: true, shell: opts?.shell ?? process.env.SHELL }, extras);
10
10
  if (isDebug()) {
11
11
  console.log(`codifySpawn result for: ${cmd}`);
12
12
  console.log(JSON.stringify(result, null, 2));
@@ -19,10 +19,17 @@ export async function codifySpawn(cmd, args, opts, extras) {
19
19
  data: status === SpawnStatus.SUCCESS ? result.stdout : result.stderr
20
20
  };
21
21
  }
22
- catch (e) {
22
+ catch (error) {
23
+ const shouldThrow = opts?.throws ?? true;
24
+ if (isDebug() || shouldThrow) {
25
+ console.error(`CodifySpawn Error for command ${cmd} ${args}`, error);
26
+ }
27
+ if (shouldThrow) {
28
+ throw error;
29
+ }
23
30
  return {
24
31
  status: SpawnStatus.ERROR,
25
- data: e,
32
+ data: error,
26
33
  };
27
34
  }
28
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.29",
3
+ "version": "1.0.30",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -16,17 +16,32 @@ type CodifySpawnOptions = {
16
16
  stdioString?: boolean;
17
17
  } & SpawnOptions
18
18
 
19
+ /**
20
+ *
21
+ * @param cmd Command to run. Ex: `rm -rf`
22
+ * @param args Optional additional arguments to append
23
+ * @param opts Standard options for node spawn. Additional argument:
24
+ * throws determines if a shell will throw a JS error. Defaults to true
25
+ * @param extras From PromiseSpawn
26
+ *
27
+ * @see promiseSpawn
28
+ * @see spawn
29
+ *
30
+ * @returns SpawnResult { status: SUCCESS | ERROR; data: string }
31
+ */
19
32
  export async function codifySpawn(
20
33
  cmd: string,
21
34
  args?: string[],
22
- opts?: Omit<CodifySpawnOptions, 'stdio' | 'stdioString' | 'shell'>,
35
+ opts?: Omit<CodifySpawnOptions, 'stdio' | 'stdioString'> & { throws?: boolean },
23
36
  extras?: Record<any, any>,
24
37
  ): Promise<SpawnResult> {
25
38
  try {
39
+ // TODO: Need to benchmark the effects of using sh vs zsh for shell.
40
+ // Seems like zsh shells run slower
26
41
  const result = await promiseSpawn(
27
42
  cmd,
28
43
  args ?? [],
29
- { ...opts, stdio: 'pipe', stdioString: true, shell: true },
44
+ { ...opts, stdio: 'pipe', stdioString: true, shell: opts?.shell ?? process.env.SHELL },
30
45
  extras,
31
46
  );
32
47
 
@@ -43,10 +58,19 @@ export async function codifySpawn(
43
58
  status,
44
59
  data: status === SpawnStatus.SUCCESS ? result.stdout : result.stderr
45
60
  }
46
- } catch (e) {
61
+ } catch (error) {
62
+ const shouldThrow = opts?.throws ?? true;
63
+ if (isDebug() || shouldThrow) {
64
+ console.error(`CodifySpawn Error for command ${cmd} ${args}`, error);
65
+ }
66
+
67
+ if (shouldThrow) {
68
+ throw error;
69
+ }
70
+
47
71
  return {
48
72
  status: SpawnStatus.ERROR,
49
- data: e as string,
73
+ data: error as string,
50
74
  }
51
75
  }
52
76
  }