@ls-stack/utils 1.2.1 → 1.4.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.
@@ -1,5 +1,6 @@
1
1
  declare function exhaustiveMatch<T extends string>(value: T): {
2
2
  with: <R>(pattern: { [K in T]: "_nxt" | "_never" | (() => R); }) => R;
3
+ withObject: <R_1>(pattern: Record<T, R_1>) => R_1;
3
4
  };
4
5
 
5
6
  export { exhaustiveMatch };
@@ -17,8 +17,12 @@ function exhaustiveMatch(value) {
17
17
  }
18
18
  throw new Error(`Exhaustive match failed: no match for ${value}`);
19
19
  }
20
+ function withObject(pattern) {
21
+ return pattern[value];
22
+ }
20
23
  return {
21
- with: matchWith
24
+ with: matchWith,
25
+ withObject
22
26
  };
23
27
  }
24
28
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/exhaustiveMatch.ts"],"sourcesContent":["export function exhaustiveMatch<T extends string>(value: T) {\n type Pattern<R> = {\n [K in T]: (() => R) | '_nxt' | '_never';\n };\n\n /**\n * The pattern can be:\n * - a function that returns the result\n * - '_nxt' to try the next pattern\n * - '_never' to indicate that this pattern should never be matched\n */\n function matchWith<R>(pattern: Pattern<R>): R {\n const result = pattern[value];\n\n if (typeof result === 'function') {\n return result();\n }\n\n if (result === '_nxt') {\n const keys = Object.keys(pattern);\n\n const nextIndex = keys.indexOf(value) + 1;\n\n for (let i = nextIndex; i < keys.length; i++) {\n const nextMatch = pattern[keys[i] as T];\n\n if (typeof nextMatch === 'function') {\n return nextMatch();\n }\n }\n }\n\n throw new Error(`Exhaustive match failed: no match for ${value}`);\n }\n\n return {\n with: matchWith,\n };\n}\n"],"mappings":";AAAO,SAAS,gBAAkC,OAAU;AAW1D,WAAS,UAAa,SAAwB;AAC5C,UAAM,SAAS,QAAQ,KAAK;AAE5B,QAAI,OAAO,WAAW,YAAY;AAChC,aAAO,OAAO;AAAA,IAChB;AAEA,QAAI,WAAW,QAAQ;AACrB,YAAM,OAAO,OAAO,KAAK,OAAO;AAEhC,YAAM,YAAY,KAAK,QAAQ,KAAK,IAAI;AAExC,eAAS,IAAI,WAAW,IAAI,KAAK,QAAQ,KAAK;AAC5C,cAAM,YAAY,QAAQ,KAAK,CAAC,CAAM;AAEtC,YAAI,OAAO,cAAc,YAAY;AACnC,iBAAO,UAAU;AAAA,QACnB;AAAA,MACF;AAAA,IACF;AAEA,UAAM,IAAI,MAAM,yCAAyC,KAAK,EAAE;AAAA,EAClE;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,EACR;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/exhaustiveMatch.ts"],"sourcesContent":["export function exhaustiveMatch<T extends string>(value: T) {\n type Pattern<R> = {\n [K in T]: (() => R) | '_nxt' | '_never';\n };\n\n /**\n * The pattern can be:\n * - a function that returns the result\n * - '_nxt' to try the next pattern\n * - '_never' to indicate that this pattern should never be matched\n */\n function matchWith<R>(pattern: Pattern<R>): R {\n const result = pattern[value];\n\n if (typeof result === 'function') {\n return result();\n }\n\n if (result === '_nxt') {\n const keys = Object.keys(pattern);\n\n const nextIndex = keys.indexOf(value) + 1;\n\n for (let i = nextIndex; i < keys.length; i++) {\n const nextMatch = pattern[keys[i] as T];\n\n if (typeof nextMatch === 'function') {\n return nextMatch();\n }\n }\n }\n\n throw new Error(`Exhaustive match failed: no match for ${value}`);\n }\n\n /** match with early evaluation of the values */\n function withObject<R>(pattern: Record<T, R>): R {\n return pattern[value];\n }\n\n return {\n with: matchWith,\n withObject,\n };\n}\n"],"mappings":";AAAO,SAAS,gBAAkC,OAAU;AAW1D,WAAS,UAAa,SAAwB;AAC5C,UAAM,SAAS,QAAQ,KAAK;AAE5B,QAAI,OAAO,WAAW,YAAY;AAChC,aAAO,OAAO;AAAA,IAChB;AAEA,QAAI,WAAW,QAAQ;AACrB,YAAM,OAAO,OAAO,KAAK,OAAO;AAEhC,YAAM,YAAY,KAAK,QAAQ,KAAK,IAAI;AAExC,eAAS,IAAI,WAAW,IAAI,KAAK,QAAQ,KAAK;AAC5C,cAAM,YAAY,QAAQ,KAAK,CAAC,CAAM;AAEtC,YAAI,OAAO,cAAc,YAAY;AACnC,iBAAO,UAAU;AAAA,QACnB;AAAA,MACF;AAAA,IACF;AAEA,UAAM,IAAI,MAAM,yCAAyC,KAAK,EAAE;AAAA,EAClE;AAGA,WAAS,WAAc,SAA0B;AAC/C,WAAO,QAAQ,KAAK;AAAA,EACtB;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,EACF;AACF;","names":[]}
@@ -5,11 +5,18 @@ type CmdResult = {
5
5
  stdout: string;
6
6
  stderr: string;
7
7
  };
8
- declare function runCmd(label: string | null, command: string | string[], { mock, silent, cwd, }?: {
8
+ type RunCmdOptions = {
9
9
  mock?: CmdResult;
10
- silent?: boolean;
10
+ silent?: boolean | 'timeOnly';
11
11
  cwd?: string;
12
- }): Promise<CmdResult>;
12
+ throwOnError?: boolean;
13
+ };
14
+ declare function runCmd(label: string | null, command: string | string[], { mock, silent, throwOnError, cwd }?: RunCmdOptions): Promise<CmdResult>;
13
15
  declare function concurrentCmd(label: string, cmd: string, onResult: (result: CmdResult) => void): Promise<() => void>;
16
+ declare function runCmdUnwrap(label: string | null, command: string | string[], { silent, }?: {
17
+ silent?: boolean | 'timeOnly';
18
+ }): Promise<string>;
19
+ declare function runCmdSilent(command: string | string[]): Promise<CmdResult>;
20
+ declare function runCmdSilentUnwrap(command: string | string[]): Promise<string>;
14
21
 
15
- export { concurrentCmd, runCmd };
22
+ export { concurrentCmd, runCmd, runCmdSilent, runCmdSilentUnwrap, runCmdUnwrap };
@@ -1,19 +1,17 @@
1
1
  // src/runShellCmd.ts
2
2
  import { spawn } from "child_process";
3
3
  process.env.FORCE_COLOR = true;
4
- function runCmd(label, command, {
5
- mock,
6
- silent,
7
- cwd = process.cwd()
8
- } = {}) {
4
+ function runCmd(label, command, { mock, silent, throwOnError, cwd = process.cwd() } = {}) {
9
5
  if (mock)
10
6
  return Promise.resolve(mock);
11
- if (label && !silent) {
7
+ if (label && (!silent || silent === "timeOnly")) {
12
8
  console.log(`----${label}----`);
13
9
  console.time("\u2705");
14
10
  }
15
11
  return new Promise((resolve) => {
16
- const [cmd = "", ...args] = Array.isArray(command) ? command : command.split(" ");
12
+ const [cmd = "", ...args] = Array.isArray(command) ? command.flatMap(
13
+ (item) => item.startsWith("$") ? item.replace("$", "").split(" ") : item
14
+ ) : command.split(" ");
17
15
  const child = spawn(cmd, args, {
18
16
  cwd
19
17
  });
@@ -36,12 +34,23 @@ function runCmd(label, command, {
36
34
  });
37
35
  child.on("close", (code) => {
38
36
  const hasError = code !== 0;
39
- if (!silent && label) {
37
+ if (!silent || silent === "timeOnly") {
40
38
  if (!hasError) {
41
39
  console.timeEnd("\u2705");
42
40
  }
43
41
  console.log("\n");
44
42
  }
43
+ if (throwOnError && hasError) {
44
+ if (silent) {
45
+ if (label) {
46
+ console.log(`----${label}----`);
47
+ } else {
48
+ console.trace();
49
+ }
50
+ console.error(stderr);
51
+ }
52
+ process.exit(1);
53
+ }
45
54
  resolve({ label, out, stderr, stdout, error: hasError });
46
55
  });
47
56
  });
@@ -50,9 +59,9 @@ async function concurrentCmd(label, cmd, onResult) {
50
59
  const start = Date.now();
51
60
  const result = await runCmd(label, cmd, { silent: true });
52
61
  onResult(result);
53
- const ellapsedSeconds = (Date.now() - start) / 1e3;
62
+ const elapsedSeconds = (Date.now() - start) / 1e3;
54
63
  console.log(
55
- `${result.error ? "\u{1F534}" : "\u2705"} ${result.label} (${ellapsedSeconds.toFixed(
64
+ `${result.error ? "\u{1F534}" : "\u2705"} ${result.label} (${elapsedSeconds.toFixed(
56
65
  1
57
66
  )}s)`
58
67
  );
@@ -64,8 +73,22 @@ async function concurrentCmd(label, cmd, onResult) {
64
73
  }
65
74
  };
66
75
  }
76
+ async function runCmdUnwrap(label, command, {
77
+ silent
78
+ } = {}) {
79
+ return (await runCmd(label, command, { silent, throwOnError: true })).stdout;
80
+ }
81
+ function runCmdSilent(command) {
82
+ return runCmd(null, command, { silent: true });
83
+ }
84
+ function runCmdSilentUnwrap(command) {
85
+ return runCmdUnwrap(null, command, { silent: true });
86
+ }
67
87
  export {
68
88
  concurrentCmd,
69
- runCmd
89
+ runCmd,
90
+ runCmdSilent,
91
+ runCmdSilentUnwrap,
92
+ runCmdUnwrap
70
93
  };
71
94
  //# sourceMappingURL=runShellCmd.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/runShellCmd.ts"],"sourcesContent":["/* eslint-disable no-console -- we need console here to print errors */\n\ntype CmdResult = {\n label: string | null;\n out: string;\n error: boolean;\n stdout: string;\n stderr: string;\n};\n\nimport { spawn } from 'child_process';\n\n(process.env as any).FORCE_COLOR = true;\n\nexport function runCmd(\n label: string | null,\n command: string | string[],\n {\n mock,\n silent,\n cwd = process.cwd(),\n }: { mock?: CmdResult; silent?: boolean; cwd?: string } = {},\n): Promise<CmdResult> {\n if (mock) return Promise.resolve(mock);\n\n if (label && !silent) {\n console.log(`----${label}----`);\n console.time('✅');\n }\n\n return new Promise((resolve) => {\n const [cmd = '', ...args] =\n Array.isArray(command) ? command : command.split(' ');\n const child = spawn(cmd, args, {\n cwd,\n });\n\n let stdout = '';\n let stderr = '';\n let out = '';\n\n child.stdout.on('data', (data) => {\n stdout += String(data);\n out += String(data);\n\n if (!silent) {\n console.log(String(data));\n }\n });\n\n child.stderr.on('data', (data) => {\n stderr += String(data);\n out += String(data);\n\n if (!silent) {\n console.log(String(data));\n }\n });\n\n child.on('close', (code) => {\n const hasError = code !== 0;\n\n if (!silent && label) {\n if (!hasError) {\n console.timeEnd('✅');\n }\n\n console.log('\\n');\n }\n\n resolve({ label, out, stderr, stdout, error: hasError });\n });\n });\n}\n\nexport async function concurrentCmd(\n label: string,\n cmd: string,\n onResult: (result: CmdResult) => void,\n) {\n const start = Date.now();\n\n const result = await runCmd(label, cmd, { silent: true });\n\n onResult(result);\n\n const ellapsedSeconds = (Date.now() - start) / 1000;\n\n console.log(\n `${result.error ? '🔴' : '✅'} ${result.label} (${ellapsedSeconds.toFixed(\n 1,\n )}s)`,\n );\n\n return () => {\n if (result.error) {\n console.log(`❌ ${result.label} errors:`);\n console.log(result.out);\n console.log('\\n');\n }\n };\n}\n"],"mappings":";AAUA,SAAS,aAAa;AAErB,QAAQ,IAAY,cAAc;AAE5B,SAAS,OACd,OACA,SACA;AAAA,EACE;AAAA,EACA;AAAA,EACA,MAAM,QAAQ,IAAI;AACpB,IAA0D,CAAC,GACvC;AACpB,MAAI;AAAM,WAAO,QAAQ,QAAQ,IAAI;AAErC,MAAI,SAAS,CAAC,QAAQ;AACpB,YAAQ,IAAI,OAAO,KAAK,MAAM;AAC9B,YAAQ,KAAK,QAAG;AAAA,EAClB;AAEA,SAAO,IAAI,QAAQ,CAAC,YAAY;AAC9B,UAAM,CAAC,MAAM,IAAI,GAAG,IAAI,IACtB,MAAM,QAAQ,OAAO,IAAI,UAAU,QAAQ,MAAM,GAAG;AACtD,UAAM,QAAQ,MAAM,KAAK,MAAM;AAAA,MAC7B;AAAA,IACF,CAAC;AAED,QAAI,SAAS;AACb,QAAI,SAAS;AACb,QAAI,MAAM;AAEV,UAAM,OAAO,GAAG,QAAQ,CAAC,SAAS;AAChC,gBAAU,OAAO,IAAI;AACrB,aAAO,OAAO,IAAI;AAElB,UAAI,CAAC,QAAQ;AACX,gBAAQ,IAAI,OAAO,IAAI,CAAC;AAAA,MAC1B;AAAA,IACF,CAAC;AAED,UAAM,OAAO,GAAG,QAAQ,CAAC,SAAS;AAChC,gBAAU,OAAO,IAAI;AACrB,aAAO,OAAO,IAAI;AAElB,UAAI,CAAC,QAAQ;AACX,gBAAQ,IAAI,OAAO,IAAI,CAAC;AAAA,MAC1B;AAAA,IACF,CAAC;AAED,UAAM,GAAG,SAAS,CAAC,SAAS;AAC1B,YAAM,WAAW,SAAS;AAE1B,UAAI,CAAC,UAAU,OAAO;AACpB,YAAI,CAAC,UAAU;AACb,kBAAQ,QAAQ,QAAG;AAAA,QACrB;AAEA,gBAAQ,IAAI,IAAI;AAAA,MAClB;AAEA,cAAQ,EAAE,OAAO,KAAK,QAAQ,QAAQ,OAAO,SAAS,CAAC;AAAA,IACzD,CAAC;AAAA,EACH,CAAC;AACH;AAEA,eAAsB,cACpB,OACA,KACA,UACA;AACA,QAAM,QAAQ,KAAK,IAAI;AAEvB,QAAM,SAAS,MAAM,OAAO,OAAO,KAAK,EAAE,QAAQ,KAAK,CAAC;AAExD,WAAS,MAAM;AAEf,QAAM,mBAAmB,KAAK,IAAI,IAAI,SAAS;AAE/C,UAAQ;AAAA,IACN,GAAG,OAAO,QAAQ,cAAO,QAAG,IAAI,OAAO,KAAK,KAAK,gBAAgB;AAAA,MAC/D;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,MAAM;AACX,QAAI,OAAO,OAAO;AAChB,cAAQ,IAAI,UAAK,OAAO,KAAK,UAAU;AACvC,cAAQ,IAAI,OAAO,GAAG;AACtB,cAAQ,IAAI,IAAI;AAAA,IAClB;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/runShellCmd.ts"],"sourcesContent":["/* eslint-disable no-console -- we need console here to print errors */\n\ntype CmdResult = {\n label: string | null;\n out: string;\n error: boolean;\n stdout: string;\n stderr: string;\n};\n\nimport { spawn } from 'child_process';\n\n(process.env as any).FORCE_COLOR = true;\n\ntype RunCmdOptions = {\n mock?: CmdResult;\n silent?: boolean | 'timeOnly';\n cwd?: string;\n throwOnError?: boolean;\n};\n\nexport function runCmd(\n label: string | null,\n command: string | string[],\n { mock, silent, throwOnError, cwd = process.cwd() }: RunCmdOptions = {},\n): Promise<CmdResult> {\n if (mock) return Promise.resolve(mock);\n\n if (label && (!silent || silent === 'timeOnly')) {\n console.log(`----${label}----`);\n console.time('✅');\n }\n\n return new Promise((resolve) => {\n const [cmd = '', ...args] =\n Array.isArray(command) ?\n command.flatMap((item) =>\n item.startsWith('$') ? item.replace('$', '').split(' ') : item,\n )\n : command.split(' ');\n const child = spawn(cmd, args, {\n cwd,\n });\n\n let stdout = '';\n let stderr = '';\n let out = '';\n\n child.stdout.on('data', (data) => {\n stdout += String(data);\n out += String(data);\n\n if (!silent) {\n console.log(String(data));\n }\n });\n\n child.stderr.on('data', (data) => {\n stderr += String(data);\n out += String(data);\n\n if (!silent) {\n console.log(String(data));\n }\n });\n\n child.on('close', (code) => {\n const hasError = code !== 0;\n\n if (!silent || silent === 'timeOnly') {\n if (!hasError) {\n console.timeEnd('✅');\n }\n\n console.log('\\n');\n }\n\n if (throwOnError && hasError) {\n if (silent) {\n if (label) {\n console.log(`----${label}----`);\n } else {\n console.trace();\n }\n console.error(stderr);\n }\n\n process.exit(1);\n }\n\n resolve({ label, out, stderr, stdout, error: hasError });\n });\n });\n}\n\nexport async function concurrentCmd(\n label: string,\n cmd: string,\n onResult: (result: CmdResult) => void,\n) {\n const start = Date.now();\n\n const result = await runCmd(label, cmd, { silent: true });\n\n onResult(result);\n\n const elapsedSeconds = (Date.now() - start) / 1000;\n\n console.log(\n `${result.error ? '🔴' : '✅'} ${result.label} (${elapsedSeconds.toFixed(\n 1,\n )}s)`,\n );\n\n return () => {\n if (result.error) {\n console.log(`❌ ${result.label} errors:`);\n console.log(result.out);\n console.log('\\n');\n }\n };\n}\n\nexport async function runCmdUnwrap(\n label: string | null,\n command: string | string[],\n {\n silent,\n }: {\n silent?: boolean | 'timeOnly';\n } = {},\n) {\n return (await runCmd(label, command, { silent, throwOnError: true })).stdout;\n}\n\nexport function runCmdSilent(command: string | string[]) {\n return runCmd(null, command, { silent: true });\n}\n\nexport function runCmdSilentUnwrap(command: string | string[]) {\n return runCmdUnwrap(null, command, { silent: true });\n}\n"],"mappings":";AAUA,SAAS,aAAa;AAErB,QAAQ,IAAY,cAAc;AAS5B,SAAS,OACd,OACA,SACA,EAAE,MAAM,QAAQ,cAAc,MAAM,QAAQ,IAAI,EAAE,IAAmB,CAAC,GAClD;AACpB,MAAI;AAAM,WAAO,QAAQ,QAAQ,IAAI;AAErC,MAA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ACtB;AAEA,gBAAQ,KAAK,CAAC;AAAA,MAChB;AAEA,cAAQ,EAAE,OAAO,KAAK,QAAQ,QAAQ,OAAO,SAAS,CAAC;AAAA,IACzD,CAAC;AAAA,EACH,CAAC;AACH;AAEA,eAAsB,cACpB,OACA,KACA,UACA;AACA,QAAM,QAAQ,KAAK,IAAI;AAEvB,QAAM,SAAS,MAAM,OAAO,OAAO,KAAK,EAAE,QAAQ,KAAK,CAAC;AAExD,WAAS,MAAM;AAEf,QAAM,kBAAkB,KAAK,IAAI,IAAI,SAAS;AAE9C,UAAQ;AAAA,IACN,GAAG,OAAO,QAAQ,cAAO,QAAG,IAAI,OAAO,KAAK,KAAK,eAAe;AAAA,MAC9D;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,MAAM;AACX,QAAI,OAAO,OAAO;AAChB,cAAQ,IAAI,UAAK,OAAO,KAAK,UAAU;AACvC,cAAQ,IAAI,OAAO,GAAG;AACtB,cAAQ,IAAI,IAAI;AAAA,IAClB;AAAA,EACF;AACF;AAEA,eAAsB,aACpB,OACA,SACA;AAAA,EACE;AACF,IAEI,CAAC,GACL;AACA,UAAQ,MAAM,OAAO,OAAO,SAAS,EAAE,QAAQ,cAAc,KAAK,CAAC,GAAG;AACxE;AAEO,SAAS,aAAa,SAA4B;AACvD,SAAO,OAAO,MAAM,SAAS,EAAE,QAAQ,KAAK,CAAC;AAC/C;AAEO,SAAS,mBAAmB,SAA4B;AAC7D,SAAO,aAAa,MAAM,SAAS,EAAE,QAAQ,KAAK,CAAC;AACrD;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "1.2.1",
4
+ "version": "1.4.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"