Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add `$.postfix option #756

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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