@shun-js/shun-cli 0.0.4
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 +3 -0
- package/bin/pm2.js +29 -0
- package/bin/shun-start.js +79 -0
- package/bin/shun-version.js +5 -0
- package/bin/shun.js +11 -0
- package/bin/util.js +27 -0
- package/package.json +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 qiaowenbin<uikoo9@qq.com>
|
|
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
package/bin/pm2.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// pm2
|
|
2
|
+
const pm2 = require('pm2');
|
|
3
|
+
|
|
4
|
+
// qiao
|
|
5
|
+
const cli = require('qiao-cli');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* pm2Start
|
|
9
|
+
* @param {*} options
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
exports.pm2Start = (options) => {
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
pm2.connect((error) => {
|
|
15
|
+
if (error) {
|
|
16
|
+
console.log(cli.colors.red('连接PM2失败。'));
|
|
17
|
+
console.log();
|
|
18
|
+
console.log(error);
|
|
19
|
+
|
|
20
|
+
process.exit(2);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
pm2.start(options, function (error) {
|
|
24
|
+
pm2.disconnect();
|
|
25
|
+
return error ? reject(error) : resolve();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// path
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
// qiao
|
|
5
|
+
const cli = require('qiao-cli');
|
|
6
|
+
const { isExists } = require('qiao-file');
|
|
7
|
+
|
|
8
|
+
// util
|
|
9
|
+
const { getNPMGlobalPath } = require('./util.js');
|
|
10
|
+
const { pm2Start } = require('./pm2.js');
|
|
11
|
+
|
|
12
|
+
// debug
|
|
13
|
+
const debug = require('debug')('@shun-js/shun-cli');
|
|
14
|
+
|
|
15
|
+
// cmd
|
|
16
|
+
cli.cmd.command('start <servers...>').description('待启动的服务名').action(startServers);
|
|
17
|
+
|
|
18
|
+
// start servers
|
|
19
|
+
async function startServers(servers) {
|
|
20
|
+
try {
|
|
21
|
+
for (let i = 0; i < servers.length; i++) {
|
|
22
|
+
await startServer(servers[i]);
|
|
23
|
+
}
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.log(cli.colors.red('启动服务出错。'));
|
|
26
|
+
console.log();
|
|
27
|
+
console.log(error);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// start servers
|
|
32
|
+
async function startServer(serverName) {
|
|
33
|
+
const methodName = 'startServer';
|
|
34
|
+
console.log(cli.colors.gray(`开始启动服务:${serverName}`));
|
|
35
|
+
|
|
36
|
+
// check
|
|
37
|
+
if (!serverName.startsWith('@shun-js')) {
|
|
38
|
+
console.log(cli.colors.red(`非法服务:${serverName}`));
|
|
39
|
+
console.log();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// check package
|
|
44
|
+
const npmGlobalPath = getNPMGlobalPath();
|
|
45
|
+
const serverAppPath = path.resolve(npmGlobalPath, `./${serverName}/app.js`);
|
|
46
|
+
const serverAppPathIsExists = await isExists(serverAppPath);
|
|
47
|
+
debug(methodName, 'serverAppPath', serverAppPath);
|
|
48
|
+
debug(methodName, 'serverAppPathIsExists', serverAppPathIsExists);
|
|
49
|
+
if (!serverAppPathIsExists) {
|
|
50
|
+
console.log(cli.colors.red(`服务未安装:${serverName}`));
|
|
51
|
+
console.log();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// check config
|
|
56
|
+
const workDir = process.cwd();
|
|
57
|
+
const serverConfigPrefix = serverName.split('/')[1];
|
|
58
|
+
const serverConfigPath = path.resolve(workDir, `./${serverConfigPrefix}.json`);
|
|
59
|
+
const serverConfigPathIsExists = await isExists(serverConfigPath);
|
|
60
|
+
debug(methodName, 'serverConfigPath', serverConfigPath);
|
|
61
|
+
debug(methodName, 'serverConfigPathIsExists', serverConfigPathIsExists);
|
|
62
|
+
|
|
63
|
+
// pm2
|
|
64
|
+
try {
|
|
65
|
+
await pm2Start({
|
|
66
|
+
name: serverConfigPrefix,
|
|
67
|
+
script: serverAppPath,
|
|
68
|
+
args: serverConfigPath,
|
|
69
|
+
});
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.log(cli.colors.red(`服务启动失败:${serverName}`));
|
|
72
|
+
console.log();
|
|
73
|
+
console.log(error);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// r
|
|
77
|
+
console.log(cli.colors.green(`服务启动成功:${serverName}`));
|
|
78
|
+
console.log();
|
|
79
|
+
}
|
package/bin/shun.js
ADDED
package/bin/util.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// qiao
|
|
2
|
+
const cli = require('qiao-cli');
|
|
3
|
+
|
|
4
|
+
// cp
|
|
5
|
+
const { execSync } = require('child_process');
|
|
6
|
+
|
|
7
|
+
// debug
|
|
8
|
+
const debug = require('debug')('shun.js');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* getNPMGlobalPath
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
exports.getNPMGlobalPath = () => {
|
|
15
|
+
const methodName = 'getNPMRootPath';
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const npmGlobalPath = execSync('npm root -g').toString().trim();
|
|
19
|
+
debug(methodName, 'npmGlobalPath', npmGlobalPath);
|
|
20
|
+
|
|
21
|
+
return npmGlobalPath;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.log(cli.colors.red('获取NPM全局路径出错。'));
|
|
24
|
+
console.log();
|
|
25
|
+
console.log(error);
|
|
26
|
+
}
|
|
27
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shun-js/shun-cli",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "shun.js cli",
|
|
5
|
+
"files": [
|
|
6
|
+
"bin",
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"bin": {
|
|
10
|
+
"shunjs": "./bin/shun.js"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"debug": "^4.4.3",
|
|
14
|
+
"pm2": "^6.0.13",
|
|
15
|
+
"qiao-cli": "^5.0.0",
|
|
16
|
+
"qiao-file": "^5.0.1"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public",
|
|
20
|
+
"registry": "https://registry.npmjs.org/"
|
|
21
|
+
},
|
|
22
|
+
"gitHead": "a660cd2290d540fbb873da3aa0b125ba4a9e68c3"
|
|
23
|
+
}
|