Files
Platform-ts/scripts/Runner.js

82 lines
1.6 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import {spawn} from 'child_process';
const node = process.execPath;
const nodeDir = path.dirname(node);
let npm = path.join(nodeDir, 'npm.cmd');
if (!fs.existsSync(npm)) {
const alternateNpm = path.join(nodeDir, 'npm');
if (fs.existsSync(alternateNpm)) {
npm = alternateNpm;
}
}
class Runner {
_node = process.execPath;
_nodeDir = path.dirname(this._node);
_npm;
constructor() {
this._npm = path.join(this._nodeDir, 'npm.cmd');
if (!fs.existsSync(this._npm)) {
const alternateNpm = path.join(this._nodeDir, 'npm');
if (fs.existsSync(alternateNpm)) {
this._npm = alternateNpm;
}
}
}
runCommands(commands, done) {
let command = commands[0];
if (typeof command === 'string') {
command = command.split(' ');
}
if (command.length > 0) {
if (command[0] === 'npm') {
command[0] = this._npm;
} else if (command[0] === 'node') {
command[0] = this._node;
}
}
this.run(command, () => {
if (commands.length > 1) {
this.runCommands(commands.slice(1), done);
} else {
if (done) {
done();
}
}
});
}
run(command, next) {
const childProcess = spawn(command[0], command.slice(1), {stdio: 'inherit'});
childProcess.on('error', error => {
console.error('Error [%o]', error);
process.exit(40);
});
childProcess.on('close', code => {
if (code !== 0) {
console.error('Command [%o] exited with code [%d]', command, code);
process.exit(code);
}
next();
});
}
}
export default new Runner();