Commit 6e674ae

Anton Golub <antongolub@antongolub.com>
2024-05-13 22:22:36
feat(cli): add `cwd`option (#804)
* 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
1 parent d653134
Changed files (3)
src/cli.ts
@@ -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
@@ -52,7 +53,7 @@ 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,
@@ -60,6 +61,7 @@ const argv = minimist(process.argv.slice(2), {
 
 ;(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
@@ -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)
 }
 
@@ -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)
 }
 
test/cli.test.js
@@ -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', () => {
@@ -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
@@ -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 =
package.json
@@ -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": {