Skip to content

Commit

Permalink
Inherit stdin on access stdout & stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 27, 2021
1 parent 1ec5804 commit e310330
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/interactive.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env zx

// Copyright 2021 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.


let {stdin, stdout} = $`npm init`

let put = text => {
stdin.write(text)
process.stdout.write(text)
}

for await (let chunk of stdout) {
if (chunk.includes('package name:')) put('test\n')
if (chunk.includes('version:')) put('1.0.0\n')
if (chunk.includes('description:')) put('My test package\n')
if (chunk.includes('entry point:')) put('index.mjs\n')
if (chunk.includes('test command:')) put('test.mjs\n')
if (chunk.includes('git repository:')) put('my-org/repo\n')
if (chunk.includes('keywords:')) put('foo, bar\n')
if (chunk.includes('author:')) put('Anton Medvedev\n')
if (chunk.includes('license:')) put('MIT\n')
if (chunk.includes('Is this OK?')) put('yes\n')
}
2 changes: 2 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ export class ProcessPromise extends Promise {
}

get stdout() {
this._inheritStdin = false
this._run()
return this.child.stdout
}

get stderr() {
this._inheritStdin = false
this._run()
return this.child.stderr
}
Expand Down

0 comments on commit e310330

Please sign in to comment.