@sinclair/hammer
Version:
Build Tool for Browser and Node Applications
254 lines (247 loc) • 8.77 kB
JavaScript
"use strict";
/*--------------------------------------------------------------------------
MIT License
Copyright (c) Hammer 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.shell = exports.Shell = exports.LinuxShell = exports.WindowsShell = void 0;
const child_process_1 = require("child_process");
// -------------------------------------------------------------------------------
// WindowsShell
// -------------------------------------------------------------------------------
class WindowsShell {
command;
cp;
promise;
resolve;
disposed;
exited;
constructor(command, stdio) {
this.command = command;
this.promise = new Promise((resolve) => {
this.resolve = resolve;
});
const [cmd, params] = this.parseArguments(this.command);
this.cp = (0, child_process_1.spawn)(cmd, params, { stdio });
this.cp.on('close', (code) => this.onClose(code));
this.cp.on('exit', (code) => this.onExit(code));
this.disposed = false;
this.exited = false;
}
// -------------------------------------------------
// Methods
// -------------------------------------------------
wait() {
return this.promise;
}
dispose() {
if (!this.exited && !this.disposed) {
this.disposed = true;
this.terminate();
}
}
// -------------------------------------------------
// Events
// -------------------------------------------------
onExit(code) {
this.resolve(code);
this.exited = true;
}
onClose(code) {
this.resolve(code);
if (!this.exited)
this.terminate();
}
// -------------------------------------------------
// Terminate
// -------------------------------------------------
terminate() {
try {
(0, child_process_1.execSync)(`taskkill /pid ${this.cp.pid} /T /F`);
}
catch (error) {
if (error instanceof Error) {
console.warn(error.message);
return;
}
console.warn(error);
}
}
// -------------------------------------------------
// Parser
// -------------------------------------------------
parseArguments(command) {
const chars = command.split('');
const args = [];
const temp = [];
while (chars.length > 0) {
const char = chars.shift();
switch (char) {
case '"': {
const result = this.consumeCharsAsString(chars, char);
args.push(result);
break;
}
case "'": {
const result = this.consumeCharsAsString(chars, char);
args.push(result);
break;
}
case ' ': {
const result = this.consumeChars(temp);
if (result.length > 0)
args.push(result);
break;
}
default: {
temp.push(char);
break;
}
}
}
const result = this.consumeChars(temp);
if (result.length > 0)
args.push(result);
return ['cmd', ['/c', ...args]];
}
consumeChars(chars) {
const result = chars.join('');
while (chars.length > 0)
chars.shift();
return result;
}
consumeCharsAsString(buffer, close) {
const result = [];
while (buffer.length > 0) {
const char = buffer.shift();
if (char === close)
return result.join('');
else
result.push(char);
}
return result.join('');
}
}
exports.WindowsShell = WindowsShell;
// -------------------------------------------------------------------------------
// LinuxShell
// -------------------------------------------------------------------------------
class LinuxShell {
command;
cp;
promise;
resolve;
disposed;
exited;
constructor(command, stdio) {
this.command = command;
this.promise = new Promise((resolve) => {
this.resolve = resolve;
});
const [cmd, params] = this.parseArguments(this.command);
this.cp = (0, child_process_1.spawn)(cmd, params, { stdio });
this.cp.on('close', (code) => this.onClose(code));
this.cp.on('exit', (code) => this.onExit(code));
this.disposed = false;
this.exited = false;
}
// -------------------------------------------------
// Methods
// -------------------------------------------------
wait() {
return this.promise;
}
dispose() {
if (!this.exited && !this.disposed) {
this.disposed = true;
this.terminate();
}
}
// -------------------------------------------------
// Events
// -------------------------------------------------
onExit(code) {
this.resolve(code);
this.exited = true;
}
onClose(code) {
this.resolve(code);
if (!this.exited)
this.terminate();
}
// -------------------------------------------------
// Terminate
// -------------------------------------------------
terminate() {
try {
const params = ['-o', 'pid', '--no-headers', '--ppid', this.cp.pid.toString()];
const result = (0, child_process_1.spawnSync)('ps', params, { encoding: 'utf8' });
const pid = parseInt(result.output[1]);
process.kill(pid, 'SIGTERM');
this.cp.kill();
}
catch (error) {
if (error instanceof Error) {
console.warn(error.message);
return;
}
console.warn(error);
}
}
// -------------------------------------------------
// Parser
// -------------------------------------------------
parseArguments(command) {
return ['sh', ['-c', command]];
}
}
exports.LinuxShell = LinuxShell;
// -------------------------------------------------------------------------------
// Shell
// -------------------------------------------------------------------------------
class Shell {
shell;
constructor(command, stdio) {
this.shell = /^win/.test(process.platform) ? new WindowsShell(command, stdio) : new LinuxShell(command, stdio);
}
wait() {
return this.shell.wait();
}
dispose() {
return this.shell.dispose();
}
}
exports.Shell = Shell;
/**
* Executes the given shell command and returns its exitcode. This function will inherit the
* stdio interfaces of the the host process. This function will throw if the processes exitcode
* does not equal the expected value. If left undefined, this function will resolve successfully
* irrespective of if the process crashed.
*/
async function shell(command, options = {}) {
const stdio = options.stdio !== undefined && options.stdio === false ? 'ignore' : 'inherit';
const expect = options.expect !== undefined ? options.expect : 0;
const shell = new Shell(command, stdio);
const exitcode = await shell.wait();
if (expect === undefined)
return exitcode;
if (expect !== exitcode)
throw new Error(`The shell command '${command}' exited with code ${exitcode} but expected code ${expect}.`);
return exitcode;
}
exports.shell = shell;