refactor: spawn promise cleanup

This commit is contained in:
Susmitha Girumala committed 2020-12-09 16:31:14 -05:00
1 parent 3077488a63
commit b7fe0c2334
4 files changed
+21 -27

No files matched your search

@@ -47,16 +47,18 @@ export default class ScopesScopeProjectsTargetsRoute extends Route {
@action
async connect(model) {
try {
await this.ipc.invoke('cli');
// Check for cli
const cliPath = await this.ipc.invoke('cliPath');
if(!cliPath) throw new Error('Cannot find boundary cli.')
// Create target session
const connect = this.ipc.invoke('connect', {
target_id: model.target.id,
token: this.session.data.authenticated.token,
});
connect.then((data) => {
await connect.then((data) => {
this.notify.success(JSON.stringify(data));
}, (e) => {
this.notify.error(JSON.stringify(e.message), { closeAfter: null });
});
} catch(e) {
this.notify.error(e.message, { closeAfter: null });
@@ -34,6 +34,7 @@
<Rose::Badge @style='informational'>
{{model.project.name}}
</Rose::Badge>
<p>{{model.project.id}}</p>
</row.cell>
<row.cell>
<Rose::Button @style="secondary" {{on "click" (route-action "connect" model)}}>
+2 -2
View File
@@ -21,9 +21,9 @@ handle('getOrigin', () => origin);
handle('setOrigin', (requestOrigin) => origin = requestOrigin);
/**
* Detect boundary cli in env path
* Detect boundary cli path
*/
handle('cli', async () => {
handle('cliPath', async () => {
const cliPath = await lookpath('boundary');
if(!cliPath) throw new Error({ message: 'Cannot find boundary cli.'})
return cliPath;
+12 -21
View File
@@ -1,17 +1,17 @@
const { spawn } = require('child_process');
/**
* Attempt to parse output string to JSON.
* When JSON is successfully parsed, consider stdout fully read.
*/
// TODO: Potential use for BufferList?
const resolveOutput = (resolve, outputStream) => {
// Verify json
const isJSON = (data) => {
let jsonData;
if(typeof data !== 'string') data = JSON.stringify(data)
try {
resolve(JSON.parse(outputStream));
jsonData = JSON.parse(data);
} catch (e) {
// Ignore JSON parse errors while stdout buffers.
// Ignore parse errors
}
};
return (typeof jsonData === 'object');
}
// You can throw exceptions, or allow them to occur, and this is supported.
// Exceptions thrown in this way will be returned to the UI as an
@@ -27,15 +27,6 @@ const resolveOutput = (resolve, outputStream) => {
// 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.
const rejectError = (reject, message) => {
try {
const errorMessage = JSON.parse(message);
reject(new Error(errorMessage.error));
} catch (e) {
// Ignore JSON parse errors while stderr buffers.
}
};
module.exports = function spawnPromise (command) {
return new Promise((resolve, reject) => {
const childProcess = spawn('boundary', command);
@@ -44,12 +35,12 @@ module.exports = function spawnPromise (command) {
childProcess.stdout.on('data', (data) => {
outputStream += data.toString();
resolveOutput(resolve, outputStream);
if(isJSON(outputStream)) resolve(JSON.parse(outputStream));
});
childProcess.stderr.on('data', (data) => {
errorStream += data.toString();
rejectError(reject, errorStream);
if(isJSON(errorStream)) reject(new Error(JSON.parse(errorStream)?.error));
});
});
}
}