Skip to content

Commit

Permalink
Add minimist
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jul 4, 2021
1 parent 73cd163 commit d8b6b87
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand All @@ -66,6 +68,7 @@ export const sleep: sleep

declare global {
var $: $
var argv: ParsedArgs
var cd: cd
var chalk: typeof _chalk
// @ts-ignore
Expand Down
25 changes: 18 additions & 7 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)},
Expand Down Expand Up @@ -247,6 +257,7 @@ function quote(arg) {

Object.assign(global, {
$,
argv,
cd,
chalk,
fetch,
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 3 additions & 5 deletions zx.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d8b6b87

Please sign in to comment.