refactor: spawn promise should support multiple exports
This commit is contained in:
2 files changed
+32
-19
No files matched your search
@@ -1,5 +1,5 @@
|
||||
const path = require('path');
|
||||
const spawnPromise = require('./spawn-promise');
|
||||
const { spawnAsyncJSONPromise } = require('./spawn-promise');
|
||||
|
||||
const cliPath = async () => path.resolve(__dirname, '..', 'cli', 'boundary');
|
||||
|
||||
@@ -18,6 +18,6 @@ module.exports = {
|
||||
'-format=json',
|
||||
'--output-json-errors'
|
||||
];
|
||||
return spawnPromise(command);
|
||||
return spawnAsyncJSONPromise(command);
|
||||
}
|
||||
}
|
||||
@@ -25,23 +25,36 @@ const jsonify = (data) => {
|
||||
// error from the underlying exec implementation. It's not a very
|
||||
// helpful message for users, so it's recommended to craft nice
|
||||
// POJO representation and throw it or promise->reject it.
|
||||
module.exports = function spawnPromise (command) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const boundaryPath = path.resolve(__dirname, '..', 'cli', 'boundary');
|
||||
const childProcess = spawn(boundaryPath, command);
|
||||
let outputStream = '';
|
||||
let errorStream = '';
|
||||
module.exports = {
|
||||
|
||||
childProcess.stdout.on('data', (data) => {
|
||||
outputStream += data.toString();
|
||||
const jsonData = jsonify(outputStream);
|
||||
if(jsonData) resolve(jsonData);
|
||||
});
|
||||
/**
|
||||
* Spawns an asynchronous child process that is expected to output JSON
|
||||
* data on either stdout or stderr. The process is allowed to continue
|
||||
* running after the promise resolves. This function is intended to launch
|
||||
* the local proxy.
|
||||
* @param {string} command
|
||||
* @return {Promise}
|
||||
*/
|
||||
spawnAsyncJSONPromise(command) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const boundaryPath = path.resolve(__dirname, '..', 'cli', 'boundary');
|
||||
const childProcess = spawn(boundaryPath, command);
|
||||
let outputStream = '';
|
||||
let errorStream = '';
|
||||
|
||||
childProcess.stderr.on('data', (data) => {
|
||||
errorStream += data.toString();
|
||||
const jsonData = jsonify(errorStream);
|
||||
if(jsonData) reject(new Error(jsonData?.error));
|
||||
childProcess.stdout.on('data', (data) => {
|
||||
outputStream += data.toString();
|
||||
const jsonData = jsonify(outputStream);
|
||||
if (jsonData) resolve(jsonData);
|
||||
});
|
||||
|
||||
childProcess.stderr.on('data', (data) => {
|
||||
errorStream += data.toString();
|
||||
const jsonData = jsonify(errorStream);
|
||||
const error = jsonData ? jsonData.error : undefined;
|
||||
if (jsonData) reject(new Error(error));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
};
|
||||
Reference in new issue
Block a user