From fb9554f322d5b1fa013ee27fe21ab92558a7ed4b Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sat, 30 Mar 2024 13:29:32 +0300 Subject: [PATCH] feat: add `$.postfix option (#756) finalizes #536 Co-authored-by: Noah Koontz --- src/cli.ts | 4 +++- src/core.ts | 5 ++++- test/cli.test.js | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 17d3252e1f..7bd06ffedb 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -42,6 +42,7 @@ function printUsage() { --quiet don't echo commands --shell= custom shell binary --prefix= prefix all commands + --postfix= postfix all commands --eval=, -e evaluate script --install, -i install dependencies --version, -v print current zx version @@ -51,7 +52,7 @@ function printUsage() { } const argv = minimist(process.argv.slice(2), { - string: ['shell', 'prefix', 'eval'], + string: ['shell', 'prefix', 'postfix', 'eval'], boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl'], alias: { e: 'eval', i: 'install', v: 'version', h: 'help' }, stopEarly: true, @@ -64,6 +65,7 @@ await (async function main() { if (argv.quiet) $.verbose = false if (argv.shell) $.shell = argv.shell if (argv.prefix) $.prefix = argv.prefix + if (argv.postfix) $.postfix = argv.postfix if (argv.version) { console.log(getVersion()) return diff --git a/src/core.ts b/src/core.ts index 2ee67b50d8..f005e8510f 100644 --- a/src/core.ts +++ b/src/core.ts @@ -64,6 +64,7 @@ export interface Options { shell: string | boolean nothrow: boolean prefix: string + postfix: string quote: typeof quote quiet: boolean spawn: typeof spawn @@ -92,6 +93,7 @@ export const defaults: Options = { nothrow: false, quiet: false, prefix: '', + postfix: '', quote: () => { throw new Error('No quote function is defined: https://ï.at/no-quote-func') }, @@ -109,6 +111,7 @@ try { if (isWin) { try { defaults.shell = which.sync('powershell.exe') + defaults.postfix = '; exit $LastExitCode' defaults.quote = quotePowerShell } catch (err) { // no powershell? @@ -230,7 +233,7 @@ export class ProcessPromise extends Promise { this._zurk = exec({ input, - cmd: $.prefix + this._command, + cmd: $.prefix + this._command + $.postfix, cwd: $.cwd ?? $[processCwd], ac: $.ac, shell: typeof $.shell === 'string' ? $.shell : true, diff --git a/test/cli.test.js b/test/cli.test.js index 4bb2688a7a..dd7a06efe9 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -91,6 +91,13 @@ describe('cli', () => { assert.ok(p.stderr.includes(prefix)) }) + test('supports `--postfix` flag ', async () => { + let postfix = '; exit 0' + let p = + await $`node build/cli.js --verbose --postfix=${postfix} <<< '$\`echo \${$.postfix}\`'` + assert.ok(p.stderr.includes(postfix)) + }) + test('scripts from https', async () => { $`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080` let out =