@01/launcher 0.2.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/LICENSE +21 -0
- package/README.md +44 -0
- package/cli/index +8 -0
- package/dist/index.js +109 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 01 Alchemist
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Nodejs Process Launcher
|
|
2
|
+
## Usage
|
|
3
|
+
```ts
|
|
4
|
+
import {launch} from '@01/launcher'
|
|
5
|
+
|
|
6
|
+
await launch({
|
|
7
|
+
cmds:['echo', 'Hello']
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Options
|
|
13
|
+
```ts
|
|
14
|
+
type Options = {
|
|
15
|
+
env?: string | string[];
|
|
16
|
+
cmds?: string[];
|
|
17
|
+
cwd?: string;
|
|
18
|
+
mode?: LaunchMode;
|
|
19
|
+
exitProcessOnClose?: boolean;
|
|
20
|
+
silent?: boolean;
|
|
21
|
+
} & SpawnOptions;
|
|
22
|
+
```
|
|
23
|
+
### env
|
|
24
|
+
Option to pass custom environment variable to child process.
|
|
25
|
+
- `env:'ENV_VAR=value'`
|
|
26
|
+
- `env:['ENV1=v1', 'ENV2=v2']`
|
|
27
|
+
- `env:'ENV1=v1,ENV2=v2'`
|
|
28
|
+
### cmds
|
|
29
|
+
Child process cmd with arguments. First element is the program location or name and remaining elements are it's arguments.
|
|
30
|
+
```
|
|
31
|
+
cmds: ['echo', 'Hello']
|
|
32
|
+
```
|
|
33
|
+
### cwd
|
|
34
|
+
Current working directory. Path is absolute if it's starts with slash `/` otherwise relative.
|
|
35
|
+
- `cwd: './relative/path'`
|
|
36
|
+
- `cwd: '/absolute/path'`
|
|
37
|
+
### mode
|
|
38
|
+
Launch mode
|
|
39
|
+
- `cli` Exit program on exceptions
|
|
40
|
+
- `program` Throw error on exceptions
|
|
41
|
+
### exitProcessOnClose
|
|
42
|
+
Exit parent node process on close.
|
|
43
|
+
### silent
|
|
44
|
+
Skip verbose logs
|
package/cli/index
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import * as S from "child_process";
|
|
2
|
+
import * as E from "readline";
|
|
3
|
+
import * as O from "fs";
|
|
4
|
+
import * as j from "path";
|
|
5
|
+
const l = {
|
|
6
|
+
log: (...t) => {
|
|
7
|
+
process.stdout.write(
|
|
8
|
+
t.reduce((o, e) => `${o} ${e}`) + `
|
|
9
|
+
`,
|
|
10
|
+
"utf-8"
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
}, k = require("minimist"), A = require("chalk"), { cursorTo: P } = E, { spawn: R } = S, { white: w, red: y, bgRed: v } = A, $ = {
|
|
14
|
+
mode: "program",
|
|
15
|
+
stdio: "inherit",
|
|
16
|
+
exitProcessOnClose: !1
|
|
17
|
+
}, g = process.cwd();
|
|
18
|
+
let p = `${g}/env/${process.env.USER}.env`;
|
|
19
|
+
O.existsSync(p) || (p = `${g}/.env`);
|
|
20
|
+
O.existsSync(p) && (l.log(`Loading env vars from: ${p}`), require("dotenv").config({
|
|
21
|
+
path: p
|
|
22
|
+
}));
|
|
23
|
+
function x(t, o) {
|
|
24
|
+
return Array.isArray(t) && Array.isArray(o) ? [...t, ...o] : typeof t == "object" && typeof o == "object" ? {
|
|
25
|
+
...t,
|
|
26
|
+
...o
|
|
27
|
+
} : o;
|
|
28
|
+
}
|
|
29
|
+
function C(t, o, e, c) {
|
|
30
|
+
let n;
|
|
31
|
+
return o && (n = o), e && (n = x(n, e)), c && (n = x(n, c)), {
|
|
32
|
+
name: t,
|
|
33
|
+
value: n
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function D(t = {}, o = []) {
|
|
37
|
+
const e = k(o);
|
|
38
|
+
return Object.keys($).concat(Object.keys(t)).concat(Object.keys(e)).map((i) => C(
|
|
39
|
+
i,
|
|
40
|
+
$[i],
|
|
41
|
+
t[i],
|
|
42
|
+
e[i]
|
|
43
|
+
)).reduce(
|
|
44
|
+
(i, s) => (i[s.name] = s.value, i),
|
|
45
|
+
{}
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
async function q(t = {}) {
|
|
49
|
+
const o = t.mode === "cli" ? process.argv.slice(2) : [], e = D(t, o);
|
|
50
|
+
e.env && (e.env instanceof Array ? e.env : e.env.split(",")).forEach((d) => {
|
|
51
|
+
const [f, m] = d.split("="), r = m.indexOf("process.env.") > -1;
|
|
52
|
+
process.env[f] = r ? process.env[m.slice(12)] : m;
|
|
53
|
+
});
|
|
54
|
+
const c = e.cmds || process.argv.slice(2).filter((a) => a.indexOf("--cwd") === -1);
|
|
55
|
+
let n = g;
|
|
56
|
+
if (e.cwd && (n = e.cwd[0] === "/" ? e.cwd : j.resolve(g, e.cwd)), !c || c.length === 0) {
|
|
57
|
+
const a = e.mode === "cli", d = "Oops 😬, Did you forgot to pass";
|
|
58
|
+
if (a) {
|
|
59
|
+
const f = y(
|
|
60
|
+
`
|
|
61
|
+
${d} ${v(
|
|
62
|
+
w(" program ")
|
|
63
|
+
)}?. Please tell me, which program you want to launch!
|
|
64
|
+
`
|
|
65
|
+
) + `
|
|
66
|
+
Example: launch echo "Hello World"
|
|
67
|
+
`;
|
|
68
|
+
l.log(f), process.exit(1);
|
|
69
|
+
} else {
|
|
70
|
+
const f = y(
|
|
71
|
+
`${d} option ${v(
|
|
72
|
+
w(" cmds ")
|
|
73
|
+
)}?. Please tell me, which program you want to launch!
|
|
74
|
+
`
|
|
75
|
+
) + `
|
|
76
|
+
Example: launch({cmds:["echo", "Hello World"]});
|
|
77
|
+
`;
|
|
78
|
+
throw new Error(f);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
e.silent || (l.log(
|
|
82
|
+
"###############################################################################################################"
|
|
83
|
+
), l.log("# 🚀 Launching : " + c.join(" ")), l.log("# 📂 CWD : " + n), l.log(
|
|
84
|
+
"###############################################################################################################"
|
|
85
|
+
));
|
|
86
|
+
const { stdio: u, shell: i } = e, s = R(c[0], c.slice(1), {
|
|
87
|
+
stdio: u,
|
|
88
|
+
cwd: n,
|
|
89
|
+
shell: i
|
|
90
|
+
});
|
|
91
|
+
s.name = c[0];
|
|
92
|
+
function b(a) {
|
|
93
|
+
s && (P(process.stdout, 0), l.log(`Killing [${s.name}] instance.pid: ${s.pid}`), s.kill(a || "SIGTERM"));
|
|
94
|
+
}
|
|
95
|
+
return process.on("SIGINT", b), new Promise(function(a, d) {
|
|
96
|
+
let f = "", m = "";
|
|
97
|
+
u !== "inherit" && (s.stdout && s.stdout.on("data", function(r) {
|
|
98
|
+
const h = r.toString();
|
|
99
|
+
h && (f += h);
|
|
100
|
+
}), s.stderr && s.stderr.on("data", function(r) {
|
|
101
|
+
m = r.toString();
|
|
102
|
+
})), s.on("close", async (r) => {
|
|
103
|
+
e.exitProcessOnClose ? (l.log(`[${s.name}] exit code:${r}`), process.exit(r || void 0)) : (l.log(`[${s.name}] exit code:${r}`), r === 0 ? a(f) : d(m));
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
export {
|
|
108
|
+
q as launch
|
|
109
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@01/launcher",
|
|
3
|
+
"version": "0.2.3",
|
|
4
|
+
"author": "01 Alchemist <01@01alchemist.com>",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"registry": "https://registry.npmjs.org"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/01alchemist/web-service-lib.git",
|
|
11
|
+
"directory": "packages/launcher"
|
|
12
|
+
},
|
|
13
|
+
"license": "UNLICENSED",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"process",
|
|
16
|
+
"launcher",
|
|
17
|
+
"spawn"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=8"
|
|
21
|
+
},
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"umd:main": "dist/index.js",
|
|
24
|
+
"module": "dist/index.js",
|
|
25
|
+
"typings": "dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"cli"
|
|
29
|
+
],
|
|
30
|
+
"bin": {
|
|
31
|
+
"launch": "./cli/index"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"yalc:publish": "yalc publish --push --sig",
|
|
35
|
+
"start": "vite",
|
|
36
|
+
"build.dev": "vite build",
|
|
37
|
+
"dev.build": "NODE_ENV=production vite build && yalc push",
|
|
38
|
+
"build": "NODE_ENV=production vite build",
|
|
39
|
+
"yalc.retreat": "yalc retreat --all",
|
|
40
|
+
"publish-pkg": "../../scripts/publish-package.js $PWD",
|
|
41
|
+
"precommit": "lint-staged",
|
|
42
|
+
"test": "echo no tests",
|
|
43
|
+
"prettier": "prettier --write '**/*.{js,json,ts,gql,graphql}'",
|
|
44
|
+
"lint": "prettier --check '**/*.{js,json,ts,gql,graphql}'"
|
|
45
|
+
},
|
|
46
|
+
"lint-staged": {
|
|
47
|
+
"*.ts": [
|
|
48
|
+
"prettier --write",
|
|
49
|
+
"git add"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"chalk": "3.0.0",
|
|
54
|
+
"dotenv": "^16.3.1",
|
|
55
|
+
"minimist": "^1.2.8",
|
|
56
|
+
"tslib": "^2.6.2"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@01/core-tools": "^0.3.3",
|
|
60
|
+
"@types/jest": "^29.5.1",
|
|
61
|
+
"@types/minimist": "^1.2.2",
|
|
62
|
+
"@types/node": "^20.1.7",
|
|
63
|
+
"jest": "^29.5.0",
|
|
64
|
+
"jest-cli": "^29.5.0",
|
|
65
|
+
"prettier": "^2.8.8",
|
|
66
|
+
"terser": "^5.17.4",
|
|
67
|
+
"ts-jest": "^29.1.0",
|
|
68
|
+
"typescript": "^5.0.4"
|
|
69
|
+
}
|
|
70
|
+
}
|