Commit e93ab36

Anton Medvedev <anton@medv.io>
2022-06-10 23:34:05
Move stdin to goods
1 parent 8a3e8db
src/cli.ts
@@ -19,10 +19,10 @@ import { createRequire } from 'node:module'
 import { tmpdir } from 'node:os'
 import { basename, dirname, extname, join, resolve } from 'node:path'
 import url from 'node:url'
-import { $, argv, fetch, ProcessOutput, chalk } from './index.js'
-import { startRepl } from './repl.js'
-import { randomId, stdin } from './util.js'
 import './globals.js'
+import { $, argv, chalk, fetch, ProcessOutput } from './index.js'
+import { startRepl } from './repl.js'
+import { randomId } from './util.js'
 
 await (async function main() {
   $.verbose = !argv.quiet
@@ -50,12 +50,7 @@ await (async function main() {
     }
   }
   if (argv.eval || argv.e) {
-    Object.defineProperty(global, 'stdin', {
-      configurable: false,
-      enumerable: true,
-      get: stdin,
-    })
-    let script = (argv.eval || argv.e).toString()
+    const script = (argv.eval || argv.e).toString()
     await runScript(script)
     return (process.exitCode = 0)
   }
src/globals.ts
@@ -31,6 +31,7 @@ declare global {
   var question: typeof _.question
   var quiet: typeof _.quiet
   var sleep: typeof _.sleep
+  var stdin: typeof _.stdin
   var which: typeof _.which
   var within: typeof _.within
   var YAML: typeof _.YAML
src/goods.ts
@@ -92,3 +92,12 @@ export async function question(
     })
   )
 }
+
+export async function stdin() {
+  let buf = ''
+  process.stdin.setEncoding('utf8')
+  for await (const chunk of process.stdin) {
+    buf += chunk
+  }
+  return buf
+}
src/index.ts
@@ -36,6 +36,7 @@ export {
   path,
   question,
   sleep,
+  stdin,
   which,
   YAML,
 } from './goods.js'
src/question.ts
@@ -1,13 +0,0 @@
-// 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.
src/repl.ts
@@ -12,11 +12,11 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-import repl from 'node:repl'
-import path from 'node:path'
+import chalk from 'chalk'
 import os from 'node:os'
+import path from 'node:path'
+import repl from 'node:repl'
 import { inspect } from 'node:util'
-import chalk from 'chalk'
 import { ProcessOutput } from './core.js'
 
 export function startRepl() {
src/util.ts
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-import chalk, { ChalkInstance } from 'chalk'
+import chalk from 'chalk'
 import { promisify } from 'node:util'
 import psTreeModule from 'ps-tree'
 import { ProcessOutput, ProcessPromise } from './core.js'
@@ -99,15 +99,6 @@ export function exitCodeInfo(exitCode: number | null): string | undefined {
   }[exitCode || -1]
 }
 
-export async function stdin() {
-  let buf = ''
-  process.stdin.setEncoding('utf8')
-  for await (const chunk of process.stdin) {
-    buf += chunk
-  }
-  return buf
-}
-
 export type Duration = number | `${number}s` | `${number}ms`
 
 export function parseDuration(d: Duration) {
test/cli.test.js
@@ -140,7 +140,7 @@ test('eval works', async () => {
 })
 
 test('eval works with stdin', async () => {
-  let p = $`(printf foo; sleep 0.1; printf bar) | node build/cli.js --eval 'echo(await stdin)'`
+  let p = $`(printf foo; sleep 0.1; printf bar) | node build/cli.js --eval 'echo(await stdin())'`
   assert.is((await p).stdout, 'foobar\n')
 })