Skip to content

Commit

Permalink
feat: add `$.postfix option (#756)
Browse files Browse the repository at this point in the history
finalizes #536

Co-authored-by: Noah Koontz <[email protected]>
  • Loading branch information
antongolub and prototypicalpro authored Mar 30, 2024
1 parent 1124e31 commit fb9554f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface Options {
shell: string | boolean
nothrow: boolean
prefix: string
postfix: string
quote: typeof quote
quiet: boolean
spawn: typeof spawn
Expand Down Expand Up @@ -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')
},
Expand All @@ -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?
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit fb9554f

Please sign in to comment.