unity-builder/src/model/system.js

63 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-05-01 11:57:42 +00:00
import * as core from '@actions/core';
import { exec } from '@actions/exec';
class System {
static async run(command, arguments_, options) {
let result = '';
let error = '';
2020-05-01 11:57:42 +00:00
let debug = '';
const listeners = {
stdout: (dataBuffer) => {
result += dataBuffer.toString();
},
stderr: (dataBuffer) => {
error += dataBuffer.toString();
},
debug: (dataString) => {
2020-05-01 11:57:42 +00:00
debug += dataString.toString();
},
};
2020-05-21 18:40:03 +00:00
const showOutput = () => {
if (debug !== '') {
core.debug(debug);
}
2020-05-01 11:57:42 +00:00
2020-05-21 18:40:03 +00:00
if (result !== '') {
core.info(result);
}
2020-05-01 11:57:42 +00:00
2020-05-21 18:40:03 +00:00
if (error !== '') {
core.warning(error);
}
};
2020-05-01 11:57:42 +00:00
2020-05-21 18:40:03 +00:00
const throwErrorShowing = (message) => {
let commandAsString = command;
if (Array.isArray(arguments_)) {
2020-05-21 18:40:03 +00:00
commandAsString += ` ${arguments_.join(' ')}`;
} else if (typeof arguments_ === 'string') {
commandAsString += ` ${arguments_}`;
}
2020-05-21 18:40:03 +00:00
throw new Error(`Failed to run "${commandAsString}".\n ${message}`);
};
try {
const exitCode = await exec(command, arguments_, { silent: true, listeners, ...options });
showOutput();
if (exitCode !== 0) {
throwErrorShowing(error);
}
} catch (inCommandError) {
showOutput();
throwErrorShowing(inCommandError);
2020-05-21 18:05:06 +00:00
}
return result;
}
}
export default System;