@plastichub/osr-cad
Version:
This is a CLI(CommandLineInterface) toolset to convert 3D files, using Solidworks and other software.
147 lines • 4.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Helper = exports.Process = exports.STATUS = void 0;
const index_1 = require("../../index");
const child_process_1 = require("child_process");
var STATUS;
(function (STATUS) {
STATUS[STATUS["OK"] = 0] = "OK";
STATUS[STATUS["ERROR"] = 1] = "ERROR";
STATUS[STATUS["PENDING"] = 2] = "PENDING";
})(STATUS = exports.STATUS || (exports.STATUS = {}));
const fatalHandler = (message, fn) => {
if (message.startsWith('fatal:')) {
fn('\t\ ' + message);
return true;
}
return false;
};
// tslint:disable-next-line:no-empty
const subscribe = (signal, collector = () => { }, options = {}) => {
const buffer = [];
signal.on('message', (message) => index_1.logger.debug('message', message));
signal.on('error', (error) => index_1.logger.error('std-error', error));
signal.on('data', (data) => {
const message = data.toString();
buffer.push(message); // .replace(/[\x00-\x1F\x7F-\x9F]/g, "")
collector(buffer);
index_1.logger.debug("\n Process : \n\t", data);
});
};
const merge = (buffer, data) => buffer.concat(data);
const hook = (process, resolve, reject, cmd, options = {}) => {
let buffer = [];
const collector = (data) => { buffer = buffer.concat(data); };
subscribe(process.stdout, collector, options);
subscribe(process.stderr, collector, options);
process.on('exit', (code, signal) => {
if (code) {
resolve({
code: STATUS.ERROR,
command: cmd,
error: code,
messages: buffer
});
}
else {
resolve({
code: STATUS.OK,
command: cmd,
messages: buffer
});
}
});
return process;
};
class Process {
constructor(options = {}) {
this.binary = 'magick';
this.cwd = '';
this.args = '';
this.binary = options.binary || this.binary;
this.cwd = options.cwd || process.cwd();
}
optionsToString(options) {
const args = [];
// tslint:disable-next-line:forin
for (const k in options) {
const val = options[k];
if (k.length === 1) {
// val is true, add '-k'
if (val === true) {
args.push('-' + k);
}
else if (val !== false) {
// if val is not false, add '-k val'
args.push('-' + k + ' ' + val);
}
}
else {
if (val === true) {
args.push('--' + k);
}
else if (val !== false) {
args.push('--' + k + '=' + val);
}
}
}
return args.join(' ');
}
optionsToArray(options) {
const args = [];
// tslint:disable-next-line:forin
for (const k in options) {
const val = options[k];
if (k.length === 1) {
// val is true, add '-k'
if (val === true) {
args.push('-' + k);
}
else if (val !== false) {
// if val is not false, add '-k val'
args.push('-' + k + ' ' + val);
}
}
else {
if (val === true) {
args.push('--' + k);
}
else if (val !== false) {
args.push('--' + k + '=' + val);
}
}
}
return args;
}
async exec(command, options = {}, args = []) {
args = [command].concat(args);
return new Promise((resolve, reject) => {
const p = (0, child_process_1.exec)(this.binary + ' ' + args.join(' '), {
cwd: this.cwd
});
return hook(p, resolve, reject, this.binary + ' ' + args.join(' '), options);
});
}
}
exports.Process = Process;
class Helper {
static async run(cwd, command, args, debug_stream = false) {
index_1.logger.trace(`Run ${command} in ${cwd} ${args.join(' ')}`);
const gitProcess = new Process({
cwd: cwd,
binary: command
});
const p = gitProcess.exec('', {
debug: debug_stream
}, args);
if (!debug_stream) {
p.catch((e) => index_1.logger.error('Error git command : ' + command, e));
}
else {
index_1.logger.trace(command + ' ' + args.join(' '));
}
return p;
}
}
exports.Helper = Helper;
//# sourceMappingURL=index.js.map