From 51fd238de7e91555ad2bd533c0d92a6e085c5f97 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sun, 8 Dec 2024 23:50:24 +0300 Subject: [PATCH] feat: let `shell` be false --- src/main/ts/spawn.ts | 2 +- src/main/ts/x.ts | 3 ++- src/test/ts/x.test.ts | 11 +++++++++++ target/cjs/index.cjs | 3 ++- target/dts/spawn.d.ts | 4 ++-- target/esm/index.mjs | 3 ++- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/ts/spawn.ts b/src/main/ts/spawn.ts index e4582b5..2d924e3 100644 --- a/src/main/ts/spawn.ts +++ b/src/main/ts/spawn.ts @@ -64,7 +64,7 @@ export interface TSpawnCtxNormalized { on: Partial ac: AbortController signal: AbortController['signal'] - shell: string | true | undefined + shell: string | boolean | undefined spawn: typeof cp.spawn spawnSync: typeof cp.spawnSync spawnOpts: Record diff --git a/src/main/ts/x.ts b/src/main/ts/x.ts index 7253125..a1d51cc 100644 --- a/src/main/ts/x.ts +++ b/src/main/ts/x.ts @@ -101,7 +101,8 @@ export const $: TShell = function(this: any, pieces?: any, ...args: any): any { } const ignite = (preset: any, pieces: TemplateStringsArray, ...args: any[]) => { - const cmd = buildCmd(preset.quote || quote, pieces as TemplateStringsArray, args) + const _quote = preset.quote || (preset.shell === false ? (arg: string) => arg : quote) + const cmd = buildCmd(_quote, pieces as TemplateStringsArray, args) const input = parseInput(preset.input) const run = cmd instanceof Promise ? (cb: TVoidCallback, ctx: TShellCtx) => cmd.then((cmd) => { ctx.cmd = cmd; cb() }) diff --git a/src/test/ts/x.test.ts b/src/test/ts/x.test.ts index 71a149f..a34c7bd 100644 --- a/src/test/ts/x.test.ts +++ b/src/test/ts/x.test.ts @@ -28,6 +28,9 @@ describe('$()', () => { console.error(error) assert.ok((error as Error).message.includes('exit code: 2 (Misuse of shell builtins)')) } + + const err = await $`exit 2`.catch((error) => error) + assert.ok(err.message.includes('exit code: 2 (Misuse of shell builtins)')) }) it('supports sync flow', () => { @@ -74,6 +77,14 @@ describe('$()', () => { $({stdio: 'ignore', sync: true})`ls` }) + it('works without shell', async () => { + const o1 = await $({shell: true})`exit 2 | exit 0` + const o2 = await $({shell: false, nothrow: true})`exit 1 | exit 0` + + assert.equal(o1.status, 0) + assert.equal(o2.status, -2) + }) + it('supports presets', () => { const $$ = $({sync: true, cmd: 'echo foo'}) const $$$ = $$({cmd: 'echo bar'}) diff --git a/target/cjs/index.cjs b/target/cjs/index.cjs index a020ba2..bb55063 100644 --- a/target/cjs/index.cjs +++ b/target/cjs/index.cjs @@ -141,7 +141,8 @@ var $ = function(pieces, ...args) { return (...args2) => $.apply(self ? (0, import_util4.assign)(self, pieces) : pieces, args2); }; var ignite = (preset, pieces, ...args) => { - const cmd = (0, import_util4.buildCmd)(preset.quote || import_util4.quote, pieces, args); + const _quote = preset.quote || (preset.shell === false ? (arg) => arg : import_util4.quote); + const cmd = (0, import_util4.buildCmd)(_quote, pieces, args); const input = (0, import_util4.parseInput)(preset.input); const run = cmd instanceof Promise ? (cb, ctx) => cmd.then((cmd2) => { ctx.cmd = cmd2; diff --git a/target/dts/spawn.d.ts b/target/dts/spawn.d.ts index 1b25c0d..c917689 100644 --- a/target/dts/spawn.d.ts +++ b/target/dts/spawn.d.ts @@ -56,7 +56,7 @@ export interface TSpawnCtxNormalized { on: Partial; ac: AbortController; signal: AbortController['signal']; - shell: string | true | undefined; + shell: string | boolean | undefined; spawn: typeof cp.spawn; spawnSync: typeof cp.spawnSync; spawnOpts: Record; @@ -81,7 +81,7 @@ export declare const buildSpawnOpts: ({ spawnOpts, stdio, cwd, shell, input, env env: Record; cwd: string; stdio: cp.StdioOptions; - shell: string | true | undefined; + shell: string | boolean | undefined; input: string | Buffer; windowsHide: boolean; detached: boolean; diff --git a/target/esm/index.mjs b/target/esm/index.mjs index 1ec42f9..92689f1 100644 --- a/target/esm/index.mjs +++ b/target/esm/index.mjs @@ -136,7 +136,8 @@ var $ = function(pieces, ...args) { return (...args2) => $.apply(self ? assign4(self, pieces) : pieces, args2); }; var ignite = (preset, pieces, ...args) => { - const cmd = buildCmd(preset.quote || quote, pieces, args); + const _quote = preset.quote || (preset.shell === false ? (arg) => arg : quote); + const cmd = buildCmd(_quote, pieces, args); const input = parseInput(preset.input); const run = cmd instanceof Promise ? (cb, ctx) => cmd.then((cmd2) => { ctx.cmd = cmd2;