Skip to content

Commit

Permalink
process: add input method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayase-252 committed Mar 28, 2021
1 parent 924238d commit d15115d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/internal/bootstrap/switches/is_main_thread.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { ObjectDefineProperty } = primordials;
const { ObjectDefineProperty, FunctionPrototypeApply } = primordials;
const rawMethods = internalBinding('process_methods');

// TODO(joyeecheung): deprecate and remove these underscore methods
Expand Down Expand Up @@ -141,7 +141,7 @@ function getStderr() {
return stderr;
}

function getStdin() {
function getStdin () {
if (stdin) return stdin;
const fd = 0;

Expand Down Expand Up @@ -233,3 +233,19 @@ rawMethods.resetStdioForTesting = function() {
stdout = undefined;
stderr = undefined;
};


process.input = async function (...args) {
const utils = require("util")
const { createInterface } = require('readline')

const rl = createInterface({
input: process.stdin,
output: process.stdout
})

const res = await FunctionPrototypeApply(utils.promisify(rl.question), rl, args)
// Close Readline to avoid process hang
rl.close()
return res
}
1 change: 1 addition & 0 deletions test/pseudo-tty/test-process-input.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
answer
6 changes: 6 additions & 0 deletions test/pseudo-tty/test-process-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const common = require('../common')
const assert = require('assert')

process.input('question here').then(common.mustCall((res) => {
assert.strictEqual(res, "answer")
}))
2 changes: 2 additions & 0 deletions test/pseudo-tty/test-process-input.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
answer
[1G[0Jquestion here[14Ganswer

0 comments on commit d15115d

Please sign in to comment.