Commit 3794306

Anton Golub <mailbox@antongolub.ru>
2022-03-17 08:37:33
feat: load experimental assets as globals via `--experimental` flag (#346)
* feat: load experimental assets as globals via `--experimental` flag * docs: fix typo * revert: rollback default shell resolver * docs: mention flag in cli helper * chore: add yarn.lock to .gitignore
1 parent ccf73d4
src/index.mjs
@@ -126,23 +126,15 @@ 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.
+try {
+  $.shell = which.sync('bash')
+  $.prefix = 'set -euo pipefail;'
+} catch (e) {
+}
 
 export function cd(path) {
   if ($.verbose) console.log('$', colorize(`cd ${path}`))
.gitignore
@@ -1,2 +1,3 @@
 /node_modules/
 package-lock.json
+yarn.lock
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 `--experimental` flag.
 
 #### `retry()`
 
zx.mjs
@@ -20,10 +20,21 @@ 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
+  }
+  if (typeof argv.prefix === 'string') {
+    $.prefix = argv.prefix
+  }
+  if (argv.experimental) {
+    Object.assign(global, await import('./src/experimental.mjs'))
+  }
   try {
     if (['--version', '-v', '-V'].includes(process.argv[2])) {
       console.log(createRequire(import.meta.url)('./package.json').version)
@@ -197,11 +208,12 @@ function printUsage() {
 
  Usage:
    zx [options] <script>
- 
+
  Options:
    --quiet            : don't echo commands
    --shell=<path>     : custom shell binary
    --prefix=<command> : prefix all commands
+   --experimental     : enable new api proposals
    --version, -v      : print current zx version
 `)
 }