Skip to content

Commit fb9554f

Browse files
antongolubprototypicalpro
andauthoredMar 30, 2024··
feat: add `$.postfix option (#756)
finalizes #536 Co-authored-by: Noah Koontz <noahkoontz@microsoft.com>
1 parent 1124e31 commit fb9554f

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed
 

‎src/cli.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function printUsage() {
4242
--quiet don't echo commands
4343
--shell=<path> custom shell binary
4444
--prefix=<command> prefix all commands
45+
--postfix=<command> postfix all commands
4546
--eval=<js>, -e evaluate script
4647
--install, -i install dependencies
4748
--version, -v print current zx version
@@ -51,7 +52,7 @@ function printUsage() {
5152
}
5253

5354
const argv = minimist(process.argv.slice(2), {
54-
string: ['shell', 'prefix', 'eval'],
55+
string: ['shell', 'prefix', 'postfix', 'eval'],
5556
boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl'],
5657
alias: { e: 'eval', i: 'install', v: 'version', h: 'help' },
5758
stopEarly: true,
@@ -64,6 +65,7 @@ await (async function main() {
6465
if (argv.quiet) $.verbose = false
6566
if (argv.shell) $.shell = argv.shell
6667
if (argv.prefix) $.prefix = argv.prefix
68+
if (argv.postfix) $.postfix = argv.postfix
6769
if (argv.version) {
6870
console.log(getVersion())
6971
return

‎src/core.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export interface Options {
6464
shell: string | boolean
6565
nothrow: boolean
6666
prefix: string
67+
postfix: string
6768
quote: typeof quote
6869
quiet: boolean
6970
spawn: typeof spawn
@@ -92,6 +93,7 @@ export const defaults: Options = {
9293
nothrow: false,
9394
quiet: false,
9495
prefix: '',
96+
postfix: '',
9597
quote: () => {
9698
throw new Error('No quote function is defined: https://ï.at/no-quote-func')
9799
},
@@ -109,6 +111,7 @@ try {
109111
if (isWin) {
110112
try {
111113
defaults.shell = which.sync('powershell.exe')
114+
defaults.postfix = '; exit $LastExitCode'
112115
defaults.quote = quotePowerShell
113116
} catch (err) {
114117
// no powershell?
@@ -230,7 +233,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
230233

231234
this._zurk = exec({
232235
input,
233-
cmd: $.prefix + this._command,
236+
cmd: $.prefix + this._command + $.postfix,
234237
cwd: $.cwd ?? $[processCwd],
235238
ac: $.ac,
236239
shell: typeof $.shell === 'string' ? $.shell : true,

‎test/cli.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ describe('cli', () => {
9191
assert.ok(p.stderr.includes(prefix))
9292
})
9393

94+
test('supports `--postfix` flag ', async () => {
95+
let postfix = '; exit 0'
96+
let p =
97+
await $`node build/cli.js --verbose --postfix=${postfix} <<< '$\`echo \${$.postfix}\`'`
98+
assert.ok(p.stderr.includes(postfix))
99+
})
100+
94101
test('scripts from https', async () => {
95102
$`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080`
96103
let out =

0 commit comments

Comments
 (0)
Please sign in to comment.