-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
62 lines (51 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const concat = require("concat-stream");
const execa = require("execa");
/**
* @param {string[]} args CLI args to pass in
* @param {string[]} answers answers to be passed to stdout
* @param {Object} [options] specify the testPath and timeout
*
* returns {Promise<Object>}
*/
module.exports = (args, answers, options) => {
// Defaults to process.cwd()
const runnerOptions =
options && options.testPath ? { cwd: options.testPath } : {};
// Timeout between each keystroke simulation
const timeout = options && options.timeout ? options.timeout : 500;
const runner = execa("node", args, runnerOptions);
runner.stdin.setDefaultEncoding("utf-8");
const writeToStdin = (answers) => {
if (answers.length > 0) {
setTimeout(() => {
runner.stdin.write(answers[0]);
writeToStdin(answers.slice(1));
}, timeout);
} else {
runner.stdin.end();
}
};
// Simulate user input (keystrokes)
writeToStdin(answers);
return new Promise((resolve) => {
const obj = {};
runner.stdout.pipe(
concat((result) => {
obj.stdout = result.toString();
})
);
runner.stderr.pipe(
concat((result) => {
obj.stderr = result.toString();
})
);
runner.on("exit", (exitCode) => {
obj.exitCode = exitCode;
resolve(obj);
});
});
};
module.exports.DOWN = "\x1B\x5B\x42";
module.exports.UP = "\x1B\x5B\x41";
module.exports.ENTER = "\x0D";
module.exports.SPACE = "\x20";