Commit 7c19c6b

Anton Medvedev <anton@medv.io>
2022-06-03 08:09:11
Move echo to goods
1 parent b659bfb
src/experimental.ts
@@ -42,27 +42,3 @@ export function withTimeout(timeout: number, signal: string) {
     return p.finally(() => clearTimeout(timer))
   }
 }
-
-// A console.log() alternative which can take ProcessOutput.
-export function echo(pieces: TemplateStringsArray, ...args: any[]) {
-  let msg
-  let lastIdx = pieces.length - 1
-  if (
-    Array.isArray(pieces) &&
-    pieces.every(isString) &&
-    lastIdx === args.length
-  ) {
-    msg =
-      args.map((a, i) => pieces[i] + stringify(a)).join('') + pieces[lastIdx]
-  } else {
-    msg = [pieces, ...args].map(stringify).join(' ')
-  }
-  console.log(msg)
-}
-
-function stringify(arg: ProcessOutput | any) {
-  if (arg instanceof ProcessOutput) {
-    return arg.toString().replace(/\n$/, '')
-  }
-  return `${arg}`
-}
src/globals.ts
@@ -21,6 +21,7 @@ declare global {
   var argv: typeof _.argv
   var cd: typeof _.cd
   var chalk: typeof _.chalk
+  var echo: typeof _.echo
   var fs: typeof _.fs
   var glob: typeof _.glob
   var globby: typeof _.globby
src/goods.ts
@@ -16,7 +16,7 @@ import * as globbyModule from 'globby'
 import minimist from 'minimist'
 import { setTimeout as sleep } from 'node:timers/promises'
 import nodeFetch, { RequestInfo, RequestInit } from 'node-fetch'
-import { colorize } from './util.js'
+import { colorize, isString, stringify } from './util.js'
 
 export { default as chalk } from 'chalk'
 export { default as fs } from 'fs-extra'
@@ -53,6 +53,23 @@ export function cd(dir: string) {
   $.cwd = path.resolve($.cwd, dir)
 }
 
+// A console.log() alternative which can take ProcessOutput.
+export function echo(pieces: TemplateStringsArray, ...args: any[]) {
+  let msg
+  let lastIdx = pieces.length - 1
+  if (
+    Array.isArray(pieces) &&
+    pieces.every(isString) &&
+    lastIdx === args.length
+  ) {
+    msg =
+      args.map((a, i) => pieces[i] + stringify(a)).join('') + pieces[lastIdx]
+  } else {
+    msg = [pieces, ...args].map(stringify).join(' ')
+  }
+  console.log(msg)
+}
+
 /**
  * Starts a simple CLI spinner.
  * @param title Spinner's title.
src/index.ts
@@ -16,6 +16,7 @@ import {
   argv,
   cd,
   chalk,
+  echo,
   fetch,
   fs,
   glob,
@@ -44,6 +45,7 @@ export {
   argv,
   cd,
   chalk,
+  echo,
   fetch,
   fs,
   glob,
src/util.ts
@@ -15,7 +15,7 @@
 import chalk from 'chalk'
 import { promisify } from 'node:util'
 import psTreeModule from 'ps-tree'
-import { ProcessPromise } from './core.js'
+import { ProcessOutput, ProcessPromise } from './core.js'
 
 export const psTree = promisify(psTreeModule)
 
@@ -55,6 +55,13 @@ export function substitute(arg: ProcessPromise | any) {
   return `${arg}`
 }
 
+export function stringify(arg: ProcessOutput | any) {
+  if (arg instanceof ProcessOutput) {
+    return arg.toString().replace(/\n$/, '')
+  }
+  return `${arg}`
+}
+
 export function colorize(cmd: string) {
   return cmd.replace(/^[\w_.-]+(\s|$)/, (substr) => {
     return chalk.greenBright(substr)
test/experimental.test.js
@@ -15,7 +15,7 @@
 import { test } from 'uvu'
 import * as assert from 'uvu/assert'
 import '../build/globals.js'
-import { echo, retry, withTimeout } from '../build/experimental.js'
+import { retry, withTimeout } from '../build/experimental.js'
 
 import chalk from 'chalk'
 
@@ -49,14 +49,4 @@ test('withTimeout works', async () => {
   assert.is(p.stdout.trim(), 'test')
 })
 
-test('echo works', async () => {
-  echo(chalk.cyan('foo'), chalk.green('bar'), chalk.bold('baz'))
-  echo`${chalk.cyan('foo')} ${chalk.green('bar')} ${chalk.bold('baz')}`
-  echo(
-    await $`echo ${chalk.cyan('foo')}`,
-    await $`echo ${chalk.green('bar')}`,
-    await $`echo ${chalk.bold('baz')}`
-  )
-})
-
 test.run()
test/index.test.js
@@ -12,6 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import chalk from 'chalk'
 import { test } from 'uvu'
 import * as assert from 'uvu/assert'
 import { inspect } from 'node:util'
@@ -221,6 +222,23 @@ test('fetch', async () => {
   )
 })
 
+test('echo works', async () => {
+  let stdout = ''
+  let log = console.log
+  console.log = (...args) => {
+    stdout += args.join(' ')
+  }
+  echo(chalk.cyan('foo'), chalk.green('bar'), chalk.bold('baz'))
+  echo`${chalk.cyan('foo')} ${chalk.green('bar')} ${chalk.bold('baz')}`
+  echo(
+    await $`echo ${chalk.cyan('foo')}`,
+    await $`echo ${chalk.green('bar')}`,
+    await $`echo ${chalk.bold('baz')}`
+  )
+  console.log = log
+  assert.match(stdout, 'foo')
+})
+
 test('executes a script from $PATH', async () => {
   const isWindows = process.platform === 'win32'
   const oldPath = process.env.PATH