@yuw-cli-dev/core 1.0.35 → 1.0.37
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/bin/index.js +7 -2
- package/lib/const.js +7 -0
- package/lib/index.js +158 -0
- package/package.json +17 -4
- package/lib/core.js +0 -7
package/bin/index.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#!/
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const importLocal = require("import-local");
|
|
4
|
+
if (importLocal(__filename)) {
|
|
5
|
+
require("npmlog").info("cli", "正在使用 yuw-cli-dev 本地版本");
|
|
6
|
+
} else {
|
|
7
|
+
require("../lib/index.js")(process.argv.slice(2));
|
|
8
|
+
}
|
package/lib/const.js
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = core;
|
|
4
|
+
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const colors = require("colors/safe");
|
|
7
|
+
const semver = require("semver");
|
|
8
|
+
const userHome = require("user-home");
|
|
9
|
+
const Commander = require("commander");
|
|
10
|
+
const pathExists = require("path-exists").sync;
|
|
11
|
+
const pkg = require("../package.json");
|
|
12
|
+
const log = require("@yuw-cli-dev/log");
|
|
13
|
+
const exec = require("@yuw-cli-dev/exec");
|
|
14
|
+
const constants = require("./const");
|
|
15
|
+
const program = new Commander.Command();
|
|
16
|
+
|
|
17
|
+
async function core() {
|
|
18
|
+
try {
|
|
19
|
+
perpare();
|
|
20
|
+
registerCommand();
|
|
21
|
+
} catch (error) {
|
|
22
|
+
log.error(error.message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function perpare() {
|
|
27
|
+
checkPkgVersion();
|
|
28
|
+
checkNodeVersion();
|
|
29
|
+
checkRoot();
|
|
30
|
+
checkUserHome();
|
|
31
|
+
// checkInputArgs();
|
|
32
|
+
checkEnv();
|
|
33
|
+
await checkGlobalUpdate();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function checkPkgVersion() {
|
|
37
|
+
log.info("cli", pkg.version);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function checkNodeVersion() {
|
|
41
|
+
const currentVersion = process.version;
|
|
42
|
+
const lowestVersion = constants.LOWEST_NODE_VERSION;
|
|
43
|
+
if (!semver.gte(currentVersion, lowestVersion)) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
colors.red(
|
|
46
|
+
`yuw-cli 需要安装 v${lowestVersion} 以上版本的 Node.js,当前版本为 ${currentVersion},请升级您的 Node.js 版本。`,
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function checkRoot() {
|
|
53
|
+
const rootCheck = require("root-check");
|
|
54
|
+
rootCheck();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function checkUserHome() {
|
|
58
|
+
if (!userHome || !pathExists(userHome)) {
|
|
59
|
+
throw new Error(colors.red("当前登录用户主目录不存在!"));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function checkInputArgs() {
|
|
64
|
+
const minimist = require("minimist");
|
|
65
|
+
const args = minimist(process.argv.slice(2));
|
|
66
|
+
checkArgs(args);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function checkArgs(args) {
|
|
70
|
+
if (args.debug) {
|
|
71
|
+
process.env.LOG_LEVEL = "verbose";
|
|
72
|
+
} else {
|
|
73
|
+
process.env.LOG_LEVEL = "info";
|
|
74
|
+
}
|
|
75
|
+
log.level = process.env.LOG_LEVEL;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function checkEnv() {
|
|
79
|
+
const dotenv = require("dotenv");
|
|
80
|
+
const dotenvPath = path.resolve(userHome, ".env");
|
|
81
|
+
if (pathExists(dotenvPath)) {
|
|
82
|
+
dotenv.config({
|
|
83
|
+
path: dotenvPath,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
createDefaultConfig();
|
|
87
|
+
log.verbose("环境变量", process.env.CLI_HOME_PATH);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function createDefaultConfig() {
|
|
91
|
+
const cliConfig = {
|
|
92
|
+
home: userHome,
|
|
93
|
+
};
|
|
94
|
+
if (process.env.CLI_HOME_PATH) {
|
|
95
|
+
cliConfig.cliHome = path.join(userHome, process.env.CLI_HOME_PATH);
|
|
96
|
+
} else {
|
|
97
|
+
cliConfig.cliHome = path.join(userHome, constants.DEFAULT_CLI_HOME);
|
|
98
|
+
}
|
|
99
|
+
process.env.CLI_HOME_PATH = cliConfig.cliHome;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function checkGlobalUpdate() {
|
|
103
|
+
const currentVersion = pkg.version;
|
|
104
|
+
const npmName = pkg.name;
|
|
105
|
+
const { getNpmSemverVersion } = require("@yuw-cli-dev/get-npm-info");
|
|
106
|
+
const latestVersion = await getNpmSemverVersion(npmName, currentVersion);
|
|
107
|
+
if (latestVersion && semver.gt(latestVersion, currentVersion)) {
|
|
108
|
+
log.warn(
|
|
109
|
+
"更新提示",
|
|
110
|
+
colors.yellow(
|
|
111
|
+
`请手动更新 ${npmName},当前版本:${currentVersion},最新版本:${latestVersion},更新命令:npm install -g ${npmName}`,
|
|
112
|
+
),
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function registerCommand() {
|
|
118
|
+
program.name(Object.keys(pkg.bin)[0]);
|
|
119
|
+
program
|
|
120
|
+
.version(pkg.version)
|
|
121
|
+
.usage("<command> [options]")
|
|
122
|
+
.option("-d, --debug", "是否开启调试模式", false)
|
|
123
|
+
.option("-t, --targetPath <targetPath>", "是否指定本地调试文件路径", "");
|
|
124
|
+
|
|
125
|
+
program
|
|
126
|
+
.command("init [projectName]")
|
|
127
|
+
.option("-f, --force", "是否强制初始化项目")
|
|
128
|
+
.action(exec);
|
|
129
|
+
|
|
130
|
+
program.on("option:debug", function () {
|
|
131
|
+
const options = program.opts();
|
|
132
|
+
if (options.debug) {
|
|
133
|
+
process.env.LOG_LEVEL = "verbose";
|
|
134
|
+
} else {
|
|
135
|
+
process.env.LOG_LEVEL = "info";
|
|
136
|
+
}
|
|
137
|
+
log.level = process.env.LOG_LEVEL;
|
|
138
|
+
log.verbose("debug", "已开启调试模式");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
program.on("option:targetPath", function (targetPath) {
|
|
142
|
+
process.env.CLI_TARGET_PATH = targetPath;
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
program.on("command:*", function (obj) {
|
|
146
|
+
const availableCommands = program.commands.map((cmd) => cmd.name());
|
|
147
|
+
console.log(colors.red("未知的命令:" + obj[0]));
|
|
148
|
+
if (availableCommands.length > 0) {
|
|
149
|
+
console.log(colors.red("可用命令:" + availableCommands.join(",")));
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
program.parse(process.argv);
|
|
154
|
+
if (program.args && program.args.length < 1) {
|
|
155
|
+
program.outputHelp();
|
|
156
|
+
console.log();
|
|
157
|
+
}
|
|
158
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yuw-cli-dev/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.37",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "yuwei <yuwei@huya.com>",
|
|
6
6
|
"homepage": "",
|
|
7
7
|
"license": "ISC",
|
|
8
8
|
"main": "lib/core.js",
|
|
9
9
|
"bin": {
|
|
10
|
-
"yuw-cli": "bin/index.js"
|
|
10
|
+
"yuw-cli-dev": "bin/index.js"
|
|
11
11
|
},
|
|
12
12
|
"directories": {
|
|
13
13
|
"lib": "lib",
|
|
@@ -20,9 +20,22 @@
|
|
|
20
20
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@yuw-cli-dev/
|
|
23
|
+
"@yuw-cli-dev/exec": "^1.0.37",
|
|
24
|
+
"@yuw-cli-dev/get-npm-info": "^1.0.37",
|
|
25
|
+
"@yuw-cli-dev/init": "^1.0.37",
|
|
26
|
+
"@yuw-cli-dev/log": "^1.0.36",
|
|
27
|
+
"@yuw-cli-dev/utils": "^1.0.37",
|
|
28
|
+
"colors": "^1.4.0",
|
|
29
|
+
"commander": "^14.0.2",
|
|
30
|
+
"dotenv": "^17.2.3",
|
|
31
|
+
"import-local": "^3.2.0",
|
|
32
|
+
"minimist": "^1.2.8",
|
|
33
|
+
"npmlog": "^7.0.1",
|
|
34
|
+
"path-exists": "^4.0.0",
|
|
35
|
+
"root-check": "^1.0.0",
|
|
36
|
+
"user-home": "^3.0.0"
|
|
24
37
|
},
|
|
25
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "ab03556d97ed9d4caf7af33d1720f4e45e34749a",
|
|
26
39
|
"publishConfig": {
|
|
27
40
|
"access": "public"
|
|
28
41
|
}
|