imooc-cli-dev-gd 1.0.0
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.md +0 -0
- package/commands/init/README.md +11 -0
- package/commands/init/__tests__/init.test.js +7 -0
- package/commands/init/lib/getProjectTemplate.js +7 -0
- package/commands/init/lib/index.js +423 -0
- package/commands/init/package.json +41 -0
- package/commands/publish/README.md +11 -0
- package/commands/publish/__tests__/publish.test.js +7 -0
- package/commands/publish/lib/index.js +67 -0
- package/commands/publish/package.json +32 -0
- package/core/cli/README.md +11 -0
- package/core/cli/__tests__/core.test.js +7 -0
- package/core/cli/bin/index.js +9 -0
- package/core/cli/lib/const.js +5 -0
- package/core/cli/lib/index.js +151 -0
- package/core/cli/package.json +41 -0
- package/core/exec/README.md +11 -0
- package/core/exec/__tests__/exec.test.js +7 -0
- package/core/exec/lib/index.js +89 -0
- package/core/exec/package.json +31 -0
- package/lerna.json +9 -0
- package/models/cloudbuild/README.md +11 -0
- package/models/cloudbuild/__tests__/cloudbuild.test.js +7 -0
- package/models/cloudbuild/lib/index.js +99 -0
- package/models/cloudbuild/package.json +28 -0
- package/models/command/README.md +11 -0
- package/models/command/__tests__/Command.test.js +7 -0
- package/models/command/lib/index.js +56 -0
- package/models/command/package.json +31 -0
- package/models/git/README.md +11 -0
- package/models/git/__tests__/git.test.js +7 -0
- package/models/git/lib/GitServer.js +60 -0
- package/models/git/lib/Gitee.js +59 -0
- package/models/git/lib/GiteeRequest.js +50 -0
- package/models/git/lib/Github.js +63 -0
- package/models/git/lib/GithubRequest.js +53 -0
- package/models/git/lib/index.js +512 -0
- package/models/git/package.json +35 -0
- package/models/package/README.md +11 -0
- package/models/package/__tests__/package.test.js +7 -0
- package/models/package/lib/index.js +121 -0
- package/models/package/package.json +35 -0
- package/package.json +14 -0
- package/utils/format-path/README.md +11 -0
- package/utils/format-path/__tests__/format-path.test.js +7 -0
- package/utils/format-path/lib/index.js +15 -0
- package/utils/format-path/package.json +26 -0
- package/utils/get-npm-info/README.md +11 -0
- package/utils/get-npm-info/__tests__/get-npm-info.test.js +7 -0
- package/utils/get-npm-info/lib/index.js +65 -0
- package/utils/get-npm-info/package.json +28 -0
- package/utils/log/README.md +11 -0
- package/utils/log/__tests__/log.test.js +7 -0
- package/utils/log/lib/index.js +10 -0
- package/utils/log/package.json +32 -0
- package/utils/request/README.md +11 -0
- package/utils/request/__tests__/request.test.js +7 -0
- package/utils/request/lib/index.js +22 -0
- package/utils/request/package.json +29 -0
- package/utils/utils/README.md +11 -0
- package/utils/utils/__tests__/utils.test.js +7 -0
- package/utils/utils/lib/index.js +77 -0
- package/utils/utils/package.json +26 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = core;
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const semver = require('semver');
|
|
7
|
+
const colors = require('colors/safe');
|
|
8
|
+
const userHome = require('user-home');
|
|
9
|
+
const pathExists = require('path-exists').sync;
|
|
10
|
+
const commander = require('commander');
|
|
11
|
+
const log = require('@imooc-cli-dev-gd/log');
|
|
12
|
+
const exec = require('@imooc-cli-dev-gd/exec');
|
|
13
|
+
|
|
14
|
+
const constant = require('./const');
|
|
15
|
+
const pkg = require('../package.json');
|
|
16
|
+
|
|
17
|
+
const program = new commander.Command();
|
|
18
|
+
|
|
19
|
+
async function core() {
|
|
20
|
+
try {
|
|
21
|
+
await prepare();
|
|
22
|
+
registerCommand();
|
|
23
|
+
} catch (e) {
|
|
24
|
+
log.error(e.message);
|
|
25
|
+
if (program.debug) {
|
|
26
|
+
console.log(e);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function registerCommand() {
|
|
32
|
+
program
|
|
33
|
+
.name(Object.keys(pkg.bin)[0])
|
|
34
|
+
.usage('<command> [options]')
|
|
35
|
+
.version(pkg.version)
|
|
36
|
+
.option('-d, --debug', '是否开启调试模式', false)
|
|
37
|
+
.option('-tp, --targetPath <targetPath>', '是否指定本地调试文件路径', '');
|
|
38
|
+
|
|
39
|
+
program
|
|
40
|
+
.command('init [projectName]')
|
|
41
|
+
.option('-f, --force', '是否强制初始化项目')
|
|
42
|
+
.action(exec);
|
|
43
|
+
|
|
44
|
+
program
|
|
45
|
+
.command('publish')
|
|
46
|
+
.option('--refreshServer', '强制更新远程Git仓库')
|
|
47
|
+
.option('--refreshToken', '强制更新远程仓库token')
|
|
48
|
+
.option('--refreshOwner', '强制更新远程仓库类型')
|
|
49
|
+
.option('--buildCmd <buildCmd>', '构建命令')
|
|
50
|
+
.action(exec);
|
|
51
|
+
|
|
52
|
+
// 开启debug模式
|
|
53
|
+
program.on('option:debug', function() {
|
|
54
|
+
if (program.debug) {
|
|
55
|
+
process.env.LOG_LEVEL = 'verbose';
|
|
56
|
+
} else {
|
|
57
|
+
process.env.LOG_LEVEL = 'info';
|
|
58
|
+
}
|
|
59
|
+
log.level = process.env.LOG_LEVEL;
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// 指定targetPath
|
|
63
|
+
program.on('option:targetPath', function() {
|
|
64
|
+
process.env.CLI_TARGET_PATH = program.targetPath;
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// 对未知命令监听
|
|
68
|
+
program.on('command:*', function(obj) {
|
|
69
|
+
const availableCommands = program.commands.map(cmd => cmd.name());
|
|
70
|
+
console.log(colors.red('未知的命令:' + obj[0]));
|
|
71
|
+
if (availableCommands.length > 0) {
|
|
72
|
+
console.log(colors.red('可用命令:' + availableCommands.join(',')));
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
program.parse(process.argv);
|
|
77
|
+
|
|
78
|
+
if (program.args && program.args.length < 1) {
|
|
79
|
+
program.outputHelp();
|
|
80
|
+
console.log();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function prepare() {
|
|
85
|
+
checkPkgVersion();
|
|
86
|
+
checkRoot();
|
|
87
|
+
checkUserHome();
|
|
88
|
+
checkEnv();
|
|
89
|
+
await checkGlobalUpdate();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function checkGlobalUpdate() {
|
|
93
|
+
const currentVersion = pkg.version;
|
|
94
|
+
const npmName = pkg.name;
|
|
95
|
+
const { getNpmSemverVersion } = require('@imooc-cli-dev-gd/get-npm-info');
|
|
96
|
+
const lastVersion = await getNpmSemverVersion(currentVersion, npmName);
|
|
97
|
+
if (lastVersion && semver.gt(lastVersion, currentVersion)) {
|
|
98
|
+
log.warn(colors.yellow(`请手动更新 ${npmName},当前版本:${currentVersion},最新版本:${lastVersion}
|
|
99
|
+
更新命令: npm install -g ${npmName}`));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function checkEnv() {
|
|
104
|
+
const dotenv = require('dotenv');
|
|
105
|
+
const dotenvPath = path.resolve(userHome, '.env');
|
|
106
|
+
if (pathExists(dotenvPath)) {
|
|
107
|
+
dotenv.config({
|
|
108
|
+
path: dotenvPath,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
createDefaultConfig();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function createDefaultConfig() {
|
|
115
|
+
const cliConfig = {
|
|
116
|
+
home: userHome,
|
|
117
|
+
};
|
|
118
|
+
if (process.env.CLI_HOME) {
|
|
119
|
+
cliConfig['cliHome'] = path.join(userHome, process.env.CLI_HOME);
|
|
120
|
+
} else {
|
|
121
|
+
cliConfig['cliHome'] = path.join(userHome, constant.DEFAULT_CLI_HOME);
|
|
122
|
+
}
|
|
123
|
+
process.env.CLI_HOME_PATH = cliConfig.cliHome;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function checkUserHome() {
|
|
127
|
+
if (!userHome || !pathExists(userHome)) {
|
|
128
|
+
throw new Error(colors.red('当前登录用户主目录不存在!'));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function checkRoot() {
|
|
133
|
+
const rootCheck = require('root-check');
|
|
134
|
+
rootCheck();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function checkPkgVersion() {
|
|
138
|
+
log.info('cli', pkg.version);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
process.on('unhandledRejection', (reason, p) => {
|
|
142
|
+
// 我刚刚捕获了一个未处理的promise rejection, 因为我们已经有了对于未处理错误的后备的处理机制(见下面), 直接抛出,让它来处理
|
|
143
|
+
console.log('unhandledRejection', reason, p);
|
|
144
|
+
throw reason;
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
process.on('uncaughtException', (error) => {
|
|
148
|
+
// 我刚收到一个从未被处理的错误,现在处理它,并决定是否需要重启应用
|
|
149
|
+
console.log('uncaughtException', error);
|
|
150
|
+
process.exit(1);
|
|
151
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@imooc-cli-dev-gd/core",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "imooc-cli-dev core",
|
|
5
|
+
"author": "sam <247765564@qq.com>",
|
|
6
|
+
"homepage": "",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"main": "",
|
|
9
|
+
"bin": {
|
|
10
|
+
"imooc-cli-dev": "bin/index.js"
|
|
11
|
+
},
|
|
12
|
+
"directories": {
|
|
13
|
+
"lib": "lib",
|
|
14
|
+
"test": "__tests__"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"lib"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "echo \"Run testing from core\""
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@imooc-cli-dev-gd/get-npm-info": "file:../../utils/get-npm-info",
|
|
24
|
+
"@imooc-cli-dev-gd/exec": "file:../exec",
|
|
25
|
+
"@imooc-cli-dev-gd/log": "file:../../utils/log",
|
|
26
|
+
"@imooc-cli-dev-gd/init": "file:../../commands/init",
|
|
27
|
+
"colors": "^1.4.0",
|
|
28
|
+
"commander": "^6.2.1",
|
|
29
|
+
"dotenv": "^8.2.0",
|
|
30
|
+
"import-local": "^3.0.2",
|
|
31
|
+
"npmlog": "^4.1.2",
|
|
32
|
+
"path-exists": "^4.0.0",
|
|
33
|
+
"root-check": "^1.0.0",
|
|
34
|
+
"semver": "^7.3.4",
|
|
35
|
+
"user-home": "^2.0.0"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "ad99cd763be44ac7ec0a97cb233ce8d0f93d0b96",
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const Package = require('@imooc-cli-dev-gd/package');
|
|
5
|
+
const log = require('@imooc-cli-dev-gd/log');
|
|
6
|
+
const { exec: spawn } = require('@imooc-cli-dev-gd/utils');
|
|
7
|
+
|
|
8
|
+
const SETTINGS = {
|
|
9
|
+
init: '@imooc-cli/init',
|
|
10
|
+
publish: '@imooc-cli/publish',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const CACHE_DIR = 'dependencies';
|
|
14
|
+
|
|
15
|
+
async function exec() {
|
|
16
|
+
let targetPath = process.env.CLI_TARGET_PATH;
|
|
17
|
+
const homePath = process.env.CLI_HOME_PATH;
|
|
18
|
+
let storeDir = '';
|
|
19
|
+
let pkg;
|
|
20
|
+
log.verbose('targetPath', targetPath);
|
|
21
|
+
log.verbose('homePath', homePath);
|
|
22
|
+
|
|
23
|
+
const cmdObj = arguments[arguments.length - 1];
|
|
24
|
+
const cmdName = cmdObj.name();
|
|
25
|
+
const packageName = SETTINGS[cmdName];
|
|
26
|
+
const packageVersion = 'latest';
|
|
27
|
+
|
|
28
|
+
if (!targetPath) {
|
|
29
|
+
targetPath = path.resolve(homePath, CACHE_DIR); // 生成缓存路径
|
|
30
|
+
storeDir = path.resolve(targetPath, 'node_modules');
|
|
31
|
+
log.verbose('targetPath', targetPath);
|
|
32
|
+
log.verbose('storeDir', storeDir);
|
|
33
|
+
pkg = new Package({
|
|
34
|
+
targetPath,
|
|
35
|
+
storeDir,
|
|
36
|
+
packageName,
|
|
37
|
+
packageVersion,
|
|
38
|
+
});
|
|
39
|
+
if (await pkg.exists()) {
|
|
40
|
+
// 更新package
|
|
41
|
+
await pkg.update();
|
|
42
|
+
} else {
|
|
43
|
+
// 安装package
|
|
44
|
+
await pkg.install();
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
pkg = new Package({
|
|
48
|
+
targetPath,
|
|
49
|
+
packageName,
|
|
50
|
+
packageVersion,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const rootFile = pkg.getRootFilePath();
|
|
54
|
+
if (rootFile) {
|
|
55
|
+
try {
|
|
56
|
+
// 在当前进程中调用
|
|
57
|
+
// require(rootFile).call(null, Array.from(arguments));
|
|
58
|
+
// 在node子进程中调用
|
|
59
|
+
const args = Array.from(arguments);
|
|
60
|
+
const cmd = args[args.length - 1];
|
|
61
|
+
const o = Object.create(null);
|
|
62
|
+
Object.keys(cmd).forEach(key => {
|
|
63
|
+
if (cmd.hasOwnProperty(key) &&
|
|
64
|
+
!key.startsWith('_') &&
|
|
65
|
+
key !== 'parent') {
|
|
66
|
+
o[key] = cmd[key];
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
args[args.length - 1] = o;
|
|
70
|
+
const code = `require('${rootFile}').call(null, ${JSON.stringify(args)})`;
|
|
71
|
+
const child = spawn('node', ['-e', code], {
|
|
72
|
+
cwd: process.cwd(),
|
|
73
|
+
stdio: 'inherit',
|
|
74
|
+
});
|
|
75
|
+
child.on('error', e => {
|
|
76
|
+
log.error(e.message);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
});
|
|
79
|
+
child.on('exit', e => {
|
|
80
|
+
log.verbose('命令执行成功:' + e);
|
|
81
|
+
process.exit(e);
|
|
82
|
+
});
|
|
83
|
+
} catch (e) {
|
|
84
|
+
log.error(e.message);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = exec;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@imooc-cli-dev-gd/exec",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "imooc-cli-dev execute package",
|
|
5
|
+
"author": "sam <247765564@qq.com>",
|
|
6
|
+
"homepage": "",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"directories": {
|
|
10
|
+
"lib": "lib",
|
|
11
|
+
"test": "__tests__"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"lib"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://git.imooc.com/class-110/imooc-cli-dev.git"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@imooc-cli-dev-gd/package": "file:../../models/package",
|
|
28
|
+
"@imooc-cli-dev-gd/log": "file:../../utils/log",
|
|
29
|
+
"@imooc-cli-dev-gd/utils": "file:../../utils/utils"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/lerna.json
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const io = require('socket.io-client');
|
|
4
|
+
const log = require('@imooc-cli-dev-gd/log');
|
|
5
|
+
const get = require('lodash/get');
|
|
6
|
+
|
|
7
|
+
const WS_SERVER = 'http://book.youbaobao.xyz:7001';
|
|
8
|
+
const TIME_OUT = 5 * 60 * 1000;
|
|
9
|
+
const CONNECT_TIME_OUT = 5 * 1000;
|
|
10
|
+
|
|
11
|
+
const FAILED_CODE = ['prepare failed', 'download failed', 'install failed', 'build failed'];
|
|
12
|
+
|
|
13
|
+
function parseMsg(msg) {
|
|
14
|
+
const action = get(msg, 'data.action');
|
|
15
|
+
const message = get(msg, 'data.payload.message');
|
|
16
|
+
return {
|
|
17
|
+
action,
|
|
18
|
+
message,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class CloudBuild {
|
|
23
|
+
constructor(git, options) {
|
|
24
|
+
this.git = git;
|
|
25
|
+
this.buildCmd = options.buildCmd;
|
|
26
|
+
this.timeout = TIME_OUT;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
doTimeout(fn, timeout) {
|
|
30
|
+
this.timer && clearTimeout(this.timer);
|
|
31
|
+
log.info('设置任务超时时间:', `${timeout / 1000}秒`);
|
|
32
|
+
this.timer = setTimeout(fn, timeout);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
init() {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
const socket = io(WS_SERVER, {
|
|
38
|
+
query: {
|
|
39
|
+
repo: this.git.remote,
|
|
40
|
+
name: this.git.name,
|
|
41
|
+
branch: this.git.branch,
|
|
42
|
+
version: this.git.version,
|
|
43
|
+
buildCmd: this.buildCmd,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
socket.on('connect', () => {
|
|
47
|
+
clearTimeout(this.timer);
|
|
48
|
+
const { id } = socket;
|
|
49
|
+
log.success('云构建任务创建成功', `任务ID: ${id}`);
|
|
50
|
+
socket.on(id, msg => {
|
|
51
|
+
const parsedMsg = parseMsg(msg);
|
|
52
|
+
log.success(parsedMsg.action, parsedMsg.message);
|
|
53
|
+
});
|
|
54
|
+
resolve();
|
|
55
|
+
});
|
|
56
|
+
const disconnect = () => {
|
|
57
|
+
clearTimeout(this.timer);
|
|
58
|
+
socket.disconnect();
|
|
59
|
+
socket.close();
|
|
60
|
+
};
|
|
61
|
+
this.doTimeout(() => {
|
|
62
|
+
log.error('云构建服务连接超时,自动终止');
|
|
63
|
+
disconnect();
|
|
64
|
+
}, CONNECT_TIME_OUT);
|
|
65
|
+
socket.on('disconnect', () => {
|
|
66
|
+
log.success('disconnect', '云构建任务断开');
|
|
67
|
+
disconnect();
|
|
68
|
+
});
|
|
69
|
+
socket.on('error', (err) => {
|
|
70
|
+
log.error('error', '云构建出错!', err);
|
|
71
|
+
disconnect();
|
|
72
|
+
reject(err);
|
|
73
|
+
});
|
|
74
|
+
this.socket = socket;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
build() {
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
this.socket.emit('build');
|
|
81
|
+
this.socket.on('build', msg => {
|
|
82
|
+
const parsedMsg = parseMsg(msg);
|
|
83
|
+
if (FAILED_CODE.indexOf(parsedMsg.action) >= 0) {
|
|
84
|
+
log.error(parsedMsg.action, parsedMsg.message);
|
|
85
|
+
clearTimeout(this.timer);
|
|
86
|
+
this.socket.disconnect();
|
|
87
|
+
this.socket.close();
|
|
88
|
+
} else {
|
|
89
|
+
log.success(parsedMsg.action, parsedMsg.message);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
this.socket.on('building', msg => {
|
|
93
|
+
console.log(msg);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = CloudBuild;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@imooc-cli-dev-gd/cloudbuild",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "> TODO: description",
|
|
5
|
+
"author": "sam <247765564@qq.com>",
|
|
6
|
+
"homepage": "",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"directories": {
|
|
10
|
+
"lib": "lib",
|
|
11
|
+
"test": "__tests__"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"lib"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://git.imooc.com/class-110/imooc-cli-dev.git"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@imooc-cli-dev-gd/log": "file:../../utils/log",
|
|
25
|
+
"lodash": "^4.17.21",
|
|
26
|
+
"socket.io-client": "^2.0.0"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const semver = require('semver');
|
|
4
|
+
const colors = require('colors/safe');
|
|
5
|
+
const log = require('@imooc-cli-dev-gd/log');
|
|
6
|
+
|
|
7
|
+
const LOWEST_NODE_VERSION = '12.0.0';
|
|
8
|
+
|
|
9
|
+
class Command {
|
|
10
|
+
constructor(argv) {
|
|
11
|
+
// log.verbose('Command constructor', argv);
|
|
12
|
+
if (!argv) {
|
|
13
|
+
throw new Error('参数不能为空!');
|
|
14
|
+
}
|
|
15
|
+
if (!Array.isArray(argv)) {
|
|
16
|
+
throw new Error('参数必须为数组!');
|
|
17
|
+
}
|
|
18
|
+
if (argv.length < 1) {
|
|
19
|
+
throw new Error('参数列表为空!');
|
|
20
|
+
}
|
|
21
|
+
this._argv = argv;
|
|
22
|
+
let runner = new Promise((resolve, reject) => {
|
|
23
|
+
let chain = Promise.resolve();
|
|
24
|
+
chain = chain.then(() => this.checkNodeVersion());
|
|
25
|
+
chain = chain.then(() => this.initArgs());
|
|
26
|
+
chain = chain.then(() => this.init());
|
|
27
|
+
chain = chain.then(() => this.exec());
|
|
28
|
+
chain.catch(err => {
|
|
29
|
+
log.error(err.message);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
initArgs() {
|
|
35
|
+
this._cmd = this._argv[this._argv.length - 1];
|
|
36
|
+
this._argv = this._argv.slice(0, this._argv.length - 1);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
checkNodeVersion() {
|
|
40
|
+
const currentVersion = process.version;
|
|
41
|
+
const lowestVersion = LOWEST_NODE_VERSION;
|
|
42
|
+
if (!semver.gte(currentVersion, lowestVersion)) {
|
|
43
|
+
throw new Error(colors.red(`imooc-cli 需要安装 v${lowestVersion} 以上版本的 Node.js`));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
init() {
|
|
48
|
+
throw new Error('init必须实现!');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exec() {
|
|
52
|
+
throw new Error('exec必须实现!');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = Command;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@imooc-cli-dev-gd/command",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "imooc-cli-dev command",
|
|
5
|
+
"author": "sam <247765564@qq.com>",
|
|
6
|
+
"homepage": "",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"directories": {
|
|
10
|
+
"lib": "lib",
|
|
11
|
+
"test": "__tests__"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"lib"
|
|
15
|
+
],
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://git.imooc.com/class-110/imooc-cli-dev.git"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"colors": "^1.4.0",
|
|
28
|
+
"semver": "^7.3.4",
|
|
29
|
+
"@imooc-cli-dev-gd/log": "file:../../utils/log"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
function error(methodName) {
|
|
2
|
+
throw new Error(`${methodName} must be implemented!`);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
class GitServer {
|
|
6
|
+
constructor(type, token) {
|
|
7
|
+
this.type = type;
|
|
8
|
+
this.token = token;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
setToken(token) {
|
|
12
|
+
this.token = token;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
createRepo(name) {
|
|
16
|
+
error('createRepo');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
createOrgRepo(name, login) {
|
|
20
|
+
error('createOrgRepo');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getRemote() {
|
|
24
|
+
error('getRemote');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getUser() {
|
|
28
|
+
error('getUser');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getOrg() {
|
|
32
|
+
error('getOrg');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getRepo(login, name) {
|
|
36
|
+
error('getRepo');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
getTokenUrl() {
|
|
40
|
+
error('getTokenUrl');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getTokenHelpUrl() {
|
|
44
|
+
error('getTokenHelpUrl');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
isHttpResponse = (response) => {
|
|
48
|
+
return response && response.status;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
handleResponse = (response) => {
|
|
52
|
+
if (this.isHttpResponse(response) && response !== 200) {
|
|
53
|
+
return null;
|
|
54
|
+
} else {
|
|
55
|
+
return response;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = GitServer;
|