@lde/task-runner-native 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/package.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# task-runner-native
|
|
2
|
+
|
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
|
4
|
+
|
|
5
|
+
## Building
|
|
6
|
+
|
|
7
|
+
Run `nx build task-runner-native` to build the library.
|
|
8
|
+
|
|
9
|
+
## Running unit tests
|
|
10
|
+
|
|
11
|
+
Run `nx test task-runner-native` to execute the unit tests via [Jest](https://jestjs.io).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TaskRunner } from '@lde/task-runner';
|
|
2
|
+
import { ChildProcess } from 'node:child_process';
|
|
3
|
+
export declare class NativeTaskRunner implements TaskRunner<ChildProcess> {
|
|
4
|
+
private stdout;
|
|
5
|
+
private stderr;
|
|
6
|
+
private shell;
|
|
7
|
+
run(command: string): Promise<ChildProcess>;
|
|
8
|
+
wait(task: ChildProcess): Promise<string>;
|
|
9
|
+
stop(task: ChildProcess): Promise<string | null>;
|
|
10
|
+
private taskOutput;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAS,MAAM,oBAAoB,CAAC;AAGzD,qBAAa,gBAAiB,YAAW,UAAU,CAAC,YAAY,CAAC;IAC/D,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,KAAK,CAAQ;IAEf,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAsC3C,IAAI,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IAgBzC,IAAI,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAWtD,OAAO,CAAC,UAAU;CAQnB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
export class NativeTaskRunner {
|
|
4
|
+
stdout = new Map();
|
|
5
|
+
stderr = new Map();
|
|
6
|
+
shell = true;
|
|
7
|
+
async run(command) {
|
|
8
|
+
const task = spawn(command, {
|
|
9
|
+
detached: true,
|
|
10
|
+
shell: this.shell,
|
|
11
|
+
cwd: 'imports', // TODO: don't hard-code
|
|
12
|
+
});
|
|
13
|
+
task.on('close', (code) => {
|
|
14
|
+
/** code is null when the process was killed, which is expected when
|
|
15
|
+
* {@link stop} is called. */
|
|
16
|
+
if (code !== null && code !== 0) {
|
|
17
|
+
// Throw to detect errors in the command arguments.
|
|
18
|
+
throw new Error(this.taskOutput(task));
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
task.on('error', (code) => {
|
|
22
|
+
throw new Error(`Task errored with code ${code}`);
|
|
23
|
+
});
|
|
24
|
+
if (task.pid !== undefined) {
|
|
25
|
+
task.stdout.on('data', (data) => {
|
|
26
|
+
this.stdout.set(task.pid, this.stdout.get(task.pid) ?? '' + data.toString());
|
|
27
|
+
});
|
|
28
|
+
task.stderr.on('data', (data) => {
|
|
29
|
+
this.stderr.set(task.pid, this.stderr.get(task.pid) ?? '' + data.toString());
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return task;
|
|
33
|
+
}
|
|
34
|
+
async wait(task) {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
// When waiting for a task, reject on error instead of crashing the
|
|
37
|
+
// process, as we do on purpose in the close listener above.
|
|
38
|
+
task.removeAllListeners('close');
|
|
39
|
+
task.on('close', (code) => {
|
|
40
|
+
const output = this.taskOutput(task);
|
|
41
|
+
if (code === 0) {
|
|
42
|
+
resolve(output);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
reject(new Error(`Process failed with code ${code}: ${output}`));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async stop(task) {
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
task.on('close', () => {
|
|
53
|
+
resolve(this.taskOutput(task));
|
|
54
|
+
});
|
|
55
|
+
// Negative PID to kill whole process group: the {shell: true} argument
|
|
56
|
+
// to spawn splits off a separate process.
|
|
57
|
+
process.kill(-task.pid, 'SIGTERM');
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
taskOutput(task) {
|
|
61
|
+
const output = (this.stdout.get(task.pid) ?? '') + this.stderr.get(task.pid);
|
|
62
|
+
this.stdout.delete(task.pid);
|
|
63
|
+
this.stderr.delete(task.pid);
|
|
64
|
+
return output;
|
|
65
|
+
}
|
|
66
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lde/task-runner-native",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
"./package.json": "./package.json",
|
|
10
|
+
".": {
|
|
11
|
+
"development": "./src/index.ts",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"!**/*.tsbuildinfo"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"tslib": "^2.3.0",
|
|
23
|
+
"@lde/task-runner": "0.0.3"
|
|
24
|
+
}
|
|
25
|
+
}
|