Commit 43695c8

Anton Medvedev <anton@medv.io>
2022-06-05 16:25:14
Move to log.ts
1 parent 081b9f0
Changed files (3)
src/core.ts
@@ -13,21 +13,14 @@
 // limitations under the License.
 
 import { ChalkInstance } from 'chalk'
-import { RequestInit } from 'node-fetch'
 import assert from 'node:assert'
 import { AsyncLocalStorage } from 'node:async_hooks'
 import { ChildProcess, spawn, StdioNull, StdioPipe } from 'node:child_process'
 import { Readable, Writable } from 'node:stream'
 import { inspect } from 'node:util'
 import { chalk, which } from './goods.js'
-import {
-  colorize,
-  exitCodeInfo,
-  noop,
-  psTree,
-  quote,
-  substitute,
-} from './util.js'
+import { log } from './log.js'
+import { exitCodeInfo, noop, psTree, quote, substitute } from './util.js'
 
 type Shell = (pieces: TemplateStringsArray, ...args: any[]) => ProcessPromise
 
@@ -346,46 +339,3 @@ export class ProcessOutput extends Error {
 export function within<R>(callback: () => R): R {
   return storage.run({ ...getStore() }, callback)
 }
-
-export type LogKind = 'cmd' | 'stdout' | 'stderr' | 'cd' | 'fetch'
-export type LogExtra = {
-  source?: ProcessPromise
-  init?: RequestInit
-}
-
-export function log(kind: LogKind, data: string, extra: LogExtra = {}) {
-  if (extra.source?.isQuiet()) return
-  if ($.verbose) {
-    switch (kind) {
-      case 'cmd':
-        process.stderr.write(formatCmd(data))
-        break
-      case 'stdout':
-      case 'stderr':
-        process.stderr.write(data)
-        break
-      case 'cd':
-        process.stderr.write('$ ' + colorize(`cd ${data}`) + '\n')
-        break
-      case 'fetch':
-        const init = extra.init ? ' ' + inspect(extra.init) : ''
-        process.stderr.write('$ ' + colorize(`fetch ${data}`) + init + '\n')
-        break
-      default:
-        throw new Error(`Unknown log kind "${kind}".`)
-    }
-  }
-}
-
-export function formatCmd(cmd: string) {
-  if (/\n/.test(cmd)) {
-    return (
-      cmd
-        .split('\n')
-        .map((line, i) => `${i == 0 ? '$' : '>'} ${colorize(line)}`)
-        .join('\n') + '\n'
-    )
-  } else {
-    return `$ ${colorize(cmd)}\n`
-  }
-}
src/index.ts
@@ -19,10 +19,6 @@ export {
   ProcessPromise,
   ProcessOutput,
   within,
-  log,
-  formatCmd,
-  LogKind,
-  LogExtra,
 } from './core.js'
 
 export {
@@ -43,6 +39,13 @@ export {
   YAML,
 } from './goods.js'
 
+export {
+  log,
+  formatCmd,
+  LogKind,
+  LogExtra,
+} from './log.js'
+
 /**
  *  @deprecated Use $.nothrow() instead.
  */
src/log.ts
@@ -0,0 +1,61 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import { RequestInit } from 'node-fetch'
+import { inspect } from 'node:util'
+import { $, ProcessPromise } from './core.js'
+import { colorize } from './util.js'
+
+export type LogKind = 'cmd' | 'stdout' | 'stderr' | 'cd' | 'fetch'
+export type LogExtra = {
+  source?: ProcessPromise
+  init?: RequestInit
+}
+
+export function log(kind: LogKind, data: string, extra: LogExtra = {}) {
+  if (extra.source?.isQuiet()) return
+  if ($.verbose) {
+    switch (kind) {
+      case 'cmd':
+        process.stderr.write(formatCmd(data))
+        break
+      case 'stdout':
+      case 'stderr':
+        process.stderr.write(data)
+        break
+      case 'cd':
+        process.stderr.write('$ ' + colorize(`cd ${data}`) + '\n')
+        break
+      case 'fetch':
+        const init = extra.init ? ' ' + inspect(extra.init) : ''
+        process.stderr.write('$ ' + colorize(`fetch ${data}`) + init + '\n')
+        break
+      default:
+        throw new Error(`Unknown log kind "${kind}".`)
+    }
+  }
+}
+
+export function formatCmd(cmd: string) {
+  if (/\n/.test(cmd)) {
+    return (
+      cmd
+        .split('\n')
+        .map((line, i) => `${i == 0 ? '$' : '>'} ${colorize(line)}`)
+        .join('\n') + '\n'
+    )
+  } else {
+    return `$ ${colorize(cmd)}\n`
+  }
+}