Commit d8b6b87

Anton Medvedev <anton@medv.io>
2021-07-04 23:53:52
Add minimist
1 parent 73cd163
index.d.ts
@@ -18,6 +18,7 @@ import * as _fs from 'fs-extra'
 import * as _os from 'os'
 import * as _chalk from 'chalk'
 import _fetch from 'node-fetch'
+import {ParsedArgs} from 'minimist'
 
 interface $ {
   (pieces: TemplateStringsArray, ...args: any[]): ProcessPromise<ProcessOutput>
@@ -55,6 +56,7 @@ type question = (query?: string, options?: QuestionOptions) => Promise<string>
 type sleep = (ms: number) => Promise<void>
 
 export const $: $
+export const argv: ParsedArgs
 export const cd: cd
 export const chalk: typeof _chalk
 export const fetch: typeof _fetch
@@ -66,6 +68,7 @@ export const sleep: sleep
 
 declare global {
   var $: $
+  var argv: ParsedArgs
   var cd: cd
   var chalk: typeof _chalk
   // @ts-ignore
index.mjs
@@ -20,6 +20,7 @@ import {createInterface} from 'readline'
 import {default as nodeFetch} from 'node-fetch'
 import which from 'which'
 import chalk from 'chalk'
+import minimist from 'minimist'
 
 export function $(pieces, ...args) {
   let __from = (new Error().stack.split('at ')[2]).trim()
@@ -76,13 +77,22 @@ export function $(pieces, ...args) {
   return promise
 }
 
-$.verbose = true
-try {
-  $.shell = await which('bash')
-  $.prefix = 'set -euo pipefail;'
-} catch (e) {
-  // Bash not found, no prefix.
+export const argv = minimist(process.argv.slice(2))
+
+$.verbose = !argv.quiet
+if (typeof argv.shell === 'string') {
+  $.shell = argv.shell
   $.prefix = ''
+} else {
+  try {
+    $.shell = await which('bash')
+    $.prefix = 'set -euo pipefail;'
+  } catch (e) {
+    $.prefix = '' // Bash not found, no prefix.
+  }
+}
+if (typeof argv.prefix === 'string') {
+  $.prefix = argv.prefix
 }
 $.quote = quote
 $.cwd = undefined
@@ -205,7 +215,7 @@ export class ProcessOutput extends Error {
   }
 
   [inspect.custom]() {
-    let stringify = (s, c) => s.length === 0 ? "''" : c(inspect(s))
+    let stringify = (s, c) => s.length === 0 ? '\'\'' : c(inspect(s))
     return `ProcessOutput {
   stdout: ${stringify(this.stdout, chalk.green)},
   stderr: ${stringify(this.stderr, chalk.red)},
@@ -247,6 +257,7 @@ function quote(arg) {
 
 Object.assign(global, {
   $,
+  argv,
   cd,
   chalk,
   fetch,
package.json
@@ -15,10 +15,12 @@
   },
   "dependencies": {
     "@types/fs-extra": "^9.0.11",
+    "@types/minimist": "^1.2.1",
     "@types/node": "^16.0",
     "@types/node-fetch": "^2.5.10",
     "chalk": "^4.1.1",
     "fs-extra": "^10.0.0",
+    "minimist": "^1.2.5",
     "node-fetch": "^2.6.1",
     "which": "^2.0.2"
   },
zx.mjs
@@ -19,16 +19,14 @@ import {tmpdir} from 'os'
 import fs from 'fs-extra'
 import {createRequire} from 'module'
 import url from 'url'
-import {$, fetch, ProcessOutput} from './index.mjs'
+import {$, fetch, ProcessOutput, argv} from './index.mjs'
 
 try {
-  let firstArg = process.argv[2]
-
-  if (['-v', '-V', '--version'].includes(firstArg)) {
+  if (argv.version || argv.v || argv.V) {
     console.log(`zx version ${createRequire(import.meta.url)('./package.json').version}`)
     process.exit(0)
   }
-
+  let firstArg = process.argv[2]
   if (typeof firstArg === 'undefined' || firstArg[0] === '-') {
     let ok = await scriptFromStdin()
     if (!ok) {