Skip to content

Commit

Permalink
feat(cli): add cwdoption (#804)
Browse files Browse the repository at this point in the history
* test: add ts smoke tests

* test: fix ts smoke test

* fix(types): add @webpod/ingrid to dts bundle

* ci: print TS version

* feat: introduce `tempdir` and `tempfile` utils

* feat(cli): add `cwd` options

* test: exclude deno.js from cov report
  • Loading branch information
antongolub committed May 13, 2024
1 parent d653134 commit 6e674ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"test:smoke:cjs": "node ./test/smoke/node.test.cjs",
"test:smoke:mjs": "node ./test/smoke/node.test.mjs",
"test:smoke:deno": "deno test ./test/smoke/deno.test.js --allow-read --allow-sys --allow-env --allow-run",
"coverage": "c8 -x build/vendor.cjs -x build/esblib.cjs -x 'test/**' -x scripts --check-coverage npm test",
"coverage": "c8 -x build/deno.js -x build/vendor.cjs -x build/esblib.cjs -x 'test/**' -x scripts --check-coverage npm test",
"version": "cat package.json | fx .version"
},
"optionalDependencies": {
Expand Down
8 changes: 5 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function printUsage() {
--shell=<path> custom shell binary
--prefix=<command> prefix all commands
--postfix=<command> postfix all commands
--cwd=<path> set current directory
--eval=<js>, -e evaluate script
--install, -i install dependencies
--version, -v print current zx version
Expand All @@ -52,14 +53,15 @@ function printUsage() {
}

const argv = minimist(process.argv.slice(2), {
string: ['shell', 'prefix', 'postfix', 'eval'],
string: ['shell', 'prefix', 'postfix', 'eval', 'cwd'],
boolean: ['version', 'help', 'quiet', 'verbose', 'install', 'repl'],
alias: { e: 'eval', i: 'install', v: 'version', h: 'help' },
stopEarly: true,
})

;(async function main() {
await import('./globals.js')
if (argv.cwd) $.cwd = argv.cwd
if (argv.verbose) $.verbose = true
if (argv.quiet) $.verbose = false
if (argv.shell) $.shell = argv.shell
Expand Down Expand Up @@ -106,7 +108,7 @@ const argv = minimist(process.argv.slice(2), {
})

async function runScript(script: string) {
const filepath = join(process.cwd(), `zx-${randomId()}.mjs`)
const filepath = join($.cwd ?? process.cwd(), `zx-${randomId()}.mjs`)
await writeAndImport(script, filepath)
}

Expand Down Expand Up @@ -136,7 +138,7 @@ async function scriptFromHttp(remote: string) {
const pathname = new URL(remote).pathname
const name = basename(pathname)
const ext = extname(pathname) || '.mjs'
const filepath = join(process.cwd(), `${name}-${randomId()}${ext}`)
const filepath = join($.cwd ?? process.cwd(), `${name}-${randomId()}${ext}`)
await writeAndImport(script, filepath)
}

Expand Down
11 changes: 10 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import assert from 'node:assert'
import { test, describe, beforeEach } from 'node:test'
import { fileURLToPath } from 'node:url'
import '../build/globals.js'

describe('cli', () => {
Expand Down Expand Up @@ -45,7 +46,7 @@ describe('cli', () => {
assert.match(help.stdout, /zx/)
})

test('zx prints usage', async () => {
test('zx prints usage if no param passed', async () => {
let p = $`node build/cli.js`
p.stdin.end()
let out = await p
Expand Down Expand Up @@ -98,6 +99,14 @@ describe('cli', () => {
assert.ok(p.stderr.includes(postfix))
})

test('supports `--cwd` option ', async () => {
let cwd = path.resolve(fileURLToPath(import.meta.url), '../../temp')
fs.mkdirSync(cwd, { recursive: true })
let p =
await $`node build/cli.js --verbose --cwd=${cwd} <<< '$\`echo \${$.cwd}\`'`
assert.ok(p.stderr.endsWith(cwd + '\n'))
})

test('scripts from https', async () => {
$`cat ${path.resolve('test/fixtures/echo.http')} | nc -l 8080`
let out =
Expand Down

0 comments on commit 6e674ae

Please sign in to comment.