From 1d5021504c5e2ee856a161cf0c4ff0231868a8cc Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Wed, 16 Mar 2022 01:31:16 +0300 Subject: [PATCH] feat: load experimental assets as globals via `--experimental` flag --- README.md | 1 + src/index.mjs | 17 ++--------------- zx.mjs | 23 ++++++++++++++++++++--- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index eccb645462..5e93534e5d 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,7 @@ let {version} = require('./package.json') The zx also provides a few experimental functions. Please leave a feedback about those features in [the discussion](https://github.com/google/zx/discussions/299). +To enable new features via CLI pass `--experimantal` flag. #### `retry()` diff --git a/src/index.mjs b/src/index.mjs index 7d933b0601..84c8ff580a 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -126,23 +126,10 @@ export function $(pieces, ...args) { return promise } -$.verbose = !argv.quiet -if (typeof argv.shell === 'string') { - $.shell = argv.shell - $.prefix = '' -} else { - try { - $.shell = which.sync('bash') - $.prefix = 'set -euo pipefail;' - } catch (e) { - $.prefix = '' // Bash not found, no prefix. - } -} -if (typeof argv.prefix === 'string') { - $.prefix = argv.prefix -} $.quote = quote $.spawn = spawn +$.verbose = true +$.prefix = '' // Bash not found, no prefix. export function cd(path) { if ($.verbose) console.log('$', colorize(`cd ${path}`)) diff --git a/zx.mjs b/zx.mjs index b3ddb90069..29041785dc 100755 --- a/zx.mjs +++ b/zx.mjs @@ -20,10 +20,27 @@ import {tmpdir} from 'os' import {basename, dirname, extname, join, resolve} from 'path' import url from 'url' -import './src/globals.mjs' -import {fetch, ProcessOutput} from './src/index.mjs' +import {$, argv, fetch, ProcessOutput, registerGlobals} from './src/index.mjs' +import which from 'which' await async function main() { + registerGlobals() + $.verbose = !argv.quiet + if (typeof argv.shell === 'string') { + $.shell = argv.shell + } else { + try { + $.shell = which.sync('bash') + $.prefix = 'set -euo pipefail;' + } catch (e) { + } + } + if (typeof argv.prefix === 'string') { + $.prefix = argv.prefix + } + if (argv.experimental) { + Object.assign(global, await import('./experimental.mjs')) + } try { if (['--version', '-v', '-V'].includes(process.argv[2])) { console.log(createRequire(import.meta.url)('./package.json').version) @@ -197,7 +214,7 @@ function printUsage() { Usage: zx [options]