Commit fb9554f

Anton Golub <antongolub@antongolub.com>
2024-03-30 11:29:32
feat: add `$.postfix option (#756)
finalizes #536 Co-authored-by: Noah Koontz <noahkoontz@microsoft.com>
1 parent 1124e31
Changed files (3)
src/cli.ts
@@ -42,6 +42,7 @@ function printUsage() {
    --quiet              don't echo commands
    --shell=<path>       custom shell binary
    --prefix=<command>   prefix all commands
+   --postfix=<command>  postfix all commands
    --eval=<js>, -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
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<ProcessOutput> {
 
     this._zurk = exec({
       input,
-      cmd: $.prefix + this._command,
+      cmd: $.prefix + this._command + $.postfix,
       cwd: $.cwd ?? $[processCwd],
       ac: $.ac,
       shell: typeof $.shell === 'string' ? $.shell : true,
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 =