Initial commit
commit
10f6994032
@ -0,0 +1,3 @@
|
|||||||
|
/@types/
|
||||||
|
/lib/
|
||||||
|
/node_modules/
|
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "simple-spawn",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"lockfileVersion": 1,
|
||||||
|
"requires": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@types/node": {
|
||||||
|
"version": "14.14.19",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.19.tgz",
|
||||||
|
"integrity": "sha512-4nhBPStMK04rruRVtVc6cDqhu7S9GZai0fpXgPXrFpcPX6Xul8xnrjSdGB4KPBVYG/R5+fXWdCM8qBoiULWGPQ=="
|
||||||
|
},
|
||||||
|
"emittery": {
|
||||||
|
"version": "0.8.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/emittery/-/emittery-0.8.0.tgz",
|
||||||
|
"integrity": "sha512-XMGToId3CejfmZg/0qOzdTT3WFuAN8fQYtcKXccabRfCzGiWMSTydMshHGLyx9C/ejMl4nw9tvqrn12QVFPIUg=="
|
||||||
|
},
|
||||||
|
"typescript": {
|
||||||
|
"version": "4.1.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz",
|
||||||
|
"integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==",
|
||||||
|
"dev": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "simple-spawn",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "Simple process spawning utility for node.js",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"types": "./@types/index.d.ts",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"watch": "npm run build -- --watch"
|
||||||
|
},
|
||||||
|
"repository": "https://git.kuba-orlik.name/kuba/node-simple-spawn",
|
||||||
|
"author": "Kuba Orlik",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/node": "^14.14.19",
|
||||||
|
"emittery": "^0.8.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^4.1.3"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
|
||||||
|
import Emittery from "emittery";
|
||||||
|
|
||||||
|
export default function simpleSpawn(
|
||||||
|
cmd: string,
|
||||||
|
arg: string[]
|
||||||
|
): Promise<string> {
|
||||||
|
const process = spawn(cmd, arg);
|
||||||
|
let output = "";
|
||||||
|
let err = "";
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
process.on("close", (code) => {
|
||||||
|
code === 0 ? resolve(output) : reject(err);
|
||||||
|
});
|
||||||
|
process.stdout.on("data", (data) => (output += data.toString("utf-8")));
|
||||||
|
process.stderr.on("data", (data) => (err += data.toString("utf-8")));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class DeferedSpawn extends Emittery {
|
||||||
|
constructor(private process: ChildProcessWithoutNullStreams) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
async waitForAnswer() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.on("success", resolve);
|
||||||
|
this.on("error", reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async waitForNextData(): Promise<string | null> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.on("data", (data) => resolve(data.toString("utf-8")));
|
||||||
|
this.on("error", reject);
|
||||||
|
this.on("end", () => resolve(null));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
kill() {
|
||||||
|
process.kill(-this.process.pid); // https://stackoverflow.com/a/49842576
|
||||||
|
debugger;
|
||||||
|
console.log("killed map!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function deferedSpawn(
|
||||||
|
cmd: string,
|
||||||
|
arg: string[]
|
||||||
|
): Promise<DeferedSpawn> {
|
||||||
|
const process = spawn(cmd, arg, { detached: true });
|
||||||
|
let spawned = false;
|
||||||
|
let output = "";
|
||||||
|
let err = "";
|
||||||
|
const emitter = new DeferedSpawn(process);
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
process.stdout.on("data", (data) => {
|
||||||
|
output += data.toString("utf-8");
|
||||||
|
emitter.emit("data", data);
|
||||||
|
});
|
||||||
|
process.stderr.on("data", (data) => {
|
||||||
|
err += data.toString("utf-8");
|
||||||
|
});
|
||||||
|
process.on("spawn", () => {
|
||||||
|
resolve(emitter);
|
||||||
|
});
|
||||||
|
process.on("close", (code) => {
|
||||||
|
if (!spawned) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
if (code === 0) {
|
||||||
|
emitter.emit("success", output);
|
||||||
|
} else {
|
||||||
|
emitter.emit("error", err);
|
||||||
|
}
|
||||||
|
emitter.emit("end", { err, output });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "esnext",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"noImplicitAny": true,
|
||||||
|
"noImplicitThis": true,
|
||||||
|
"strictNullChecks": true,
|
||||||
|
"target": "ES2020",
|
||||||
|
"declaration": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"lib": ["es6", "esnext"],
|
||||||
|
"outDir": "lib",
|
||||||
|
"checkJs": true,
|
||||||
|
"allowJs": true,
|
||||||
|
"declarationDir": "@types",
|
||||||
|
"sourceMap": true
|
||||||
|
},
|
||||||
|
"include": ["src/**/*"]
|
||||||
|
}
|
Loading…
Reference in New Issue