Commit bd11324

Anton Medvedev <anton@medv.io>
2022-06-06 23:06:36
Move startSpinner to experimental.ts
1 parent 681a09b
examples/parallel.mjs
@@ -14,6 +14,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import { startSpinner } from 'zx/experimental'
+
 let tests = await glob('test/*.test.js')
 let stop = startSpinner('running tests')
 try {
src/core.ts
@@ -206,10 +206,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
     this.child.stderr?.on('data', onStderr) // Stderr should be printed regardless of piping.
     this._postrun() // In case $1.pipe($2), after both subprocesses are running, we can pipe $1.stdout to $2.stdin.
     if (this._timeout && this._timeoutSignal) {
-      const t = setTimeout(
-        () => this.kill(this._timeoutSignal),
-        this._timeout
-      )
+      const t = setTimeout(() => this.kill(this._timeoutSignal), this._timeout)
       this.finally(() => clearTimeout(t)).catch(noop)
     }
   }
src/experimental.ts
@@ -30,14 +30,20 @@ export function retry(count = 5, delay = 0) {
   }
 }
 
-// Runs and sets a timeout for a cmd
-export function withTimeout(timeout: number, signal: string) {
-  return async function (cmd: TemplateStringsArray, ...args: any[]) {
-    let p = $(cmd, ...args)
-    if (!timeout) return p
-
-    let timer = setTimeout(() => p.kill(signal), timeout)
-
-    return p.finally(() => clearTimeout(timer))
+/**
+ * Starts a simple CLI spinner.
+ * @param title Spinner's title.
+ * @return A stop() func.
+ */
+export function startSpinner(title = '') {
+  let i = 0,
+    v = $.verbose
+  $.verbose = false
+  let spin = () =>
+    process.stderr.write(`  ${'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'[i++ % 10]} ${title}\r`)
+  let id = setInterval(spin, 100)
+  return () => {
+    clearInterval(id)
+    $.verbose = v
   }
 }
src/globals.ts
@@ -31,7 +31,6 @@ declare global {
   var question: typeof _.question
   var quiet: typeof _.quiet
   var sleep: typeof _.sleep
-  var startSpinner: typeof _.startSpinner
   var which: typeof _.which
   var within: typeof _.within
   var YAML: typeof _.YAML
src/goods.ts
@@ -93,21 +93,3 @@ export async function question(
     })
   )
 }
-
-/**
- * Starts a simple CLI spinner.
- * @param title Spinner's title.
- * @return A stop() func.
- */
-export function startSpinner(title = '') {
-  let i = 0,
-    v = $.verbose
-  $.verbose = false
-  let spin = () =>
-    process.stdout.write(`  ${'⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'[i++ % 10]} ${title}\r`)
-  let id = setInterval(spin, 100)
-  return () => {
-    clearInterval(id)
-    $.verbose = v
-  }
-}
src/index.ts
@@ -36,7 +36,6 @@ export {
   path,
   question,
   sleep,
-  startSpinner,
   which,
   YAML,
 } from './goods.js'
test/experimental.test.js
@@ -14,8 +14,8 @@
 
 import { suite } from 'uvu'
 import * as assert from 'uvu/assert'
+import { retry } from '../build/experimental.js'
 import '../build/globals.js'
-import { retry, withTimeout } from '../build/experimental.js'
 
 const test = suite('experimental')
 
@@ -33,20 +33,13 @@ test('retry works', async () => {
   assert.ok(Date.now() >= now + 50 * (5 - 1))
 })
 
-test('withTimeout works', async () => {
-  let exitCode = 0
-  let signal
-  try {
-    await withTimeout(100, 'SIGKILL')`sleep 9999`
-  } catch (p) {
-    exitCode = p.exitCode
-    signal = p.signal
-  }
-  assert.is(exitCode, null)
-  assert.is(signal, 'SIGKILL')
-
-  let p = await withTimeout(0)`echo 'test'`
-  assert.is(p.stdout.trim(), 'test')
+test('spinner works', async () => {
+  let out = await $`node build/cli.js --experimental <<'EOF'
+  let stop = startSpinner('waiting')
+  await sleep(1000)
+  stop()
+EOF`
+  assert.match(out.stderr, 'waiting')
 })
 
 test.run()
test/index.test.js
@@ -450,12 +450,6 @@ test(`within() isolates nested context and returns cb result`, async () => {
   })
 })
 
-test('spinner works', async () => {
-  let s = startSpinner('waiting')
-  await sleep(1000)
-  s()
-})
-
 test('stdio() works', async () => {
   let p = $`printf foo`
   await p