@farris/cli 1.0.1 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +2 -2
- package/ci/cli.js +124 -107
- package/package.json +25 -25
package/README.md
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
# farris-cli
|
2
|
-
|
1
|
+
# farris-cli
|
2
|
+
|
package/ci/cli.js
CHANGED
@@ -1,107 +1,124 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
|
3
|
-
'use strict';
|
4
|
-
|
5
|
-
const fs = require('fs');
|
6
|
-
const path = require('path');
|
7
|
-
const childProcess = require("@lerna/child-process");
|
8
|
-
|
9
|
-
const yargs = require('yargs/yargs');
|
10
|
-
const cli = yargs(process.argv.slice(2), process.cwd())
|
11
|
-
|
12
|
-
cli
|
13
|
-
.command(
|
14
|
-
'
|
15
|
-
'
|
16
|
-
(yargs) => {
|
17
|
-
return yargs;
|
18
|
-
},
|
19
|
-
(argv) => {
|
20
|
-
const project = argv.project;
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
}
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
}
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
'use strict';
|
4
|
+
|
5
|
+
const fs = require('fs');
|
6
|
+
const path = require('path');
|
7
|
+
const childProcess = require("@lerna/child-process");
|
8
|
+
|
9
|
+
const yargs = require('yargs/yargs');
|
10
|
+
const cli = yargs(process.argv.slice(2), process.cwd())
|
11
|
+
|
12
|
+
cli
|
13
|
+
.command(
|
14
|
+
'check',
|
15
|
+
'check file changes from last commit.',
|
16
|
+
(yargs) => {
|
17
|
+
return yargs;
|
18
|
+
},
|
19
|
+
(argv) => {
|
20
|
+
const project = argv.project;
|
21
|
+
checkProjectChanges(project);
|
22
|
+
}
|
23
|
+
)
|
24
|
+
.command(
|
25
|
+
'prerelease',
|
26
|
+
'Update package prerelease version when package has changed.',
|
27
|
+
(yargs) => {
|
28
|
+
return yargs;
|
29
|
+
},
|
30
|
+
(argv) => {
|
31
|
+
const project = argv.project;
|
32
|
+
let chain = Promise.resolve();
|
33
|
+
|
34
|
+
chain = chain.then(() => checkProjectChanges(project));
|
35
|
+
chain = chain.then((hasChanged) => {
|
36
|
+
if (hasChanged) {
|
37
|
+
return updateProjectPrereleaseVersion(project)
|
38
|
+
}
|
39
|
+
});
|
40
|
+
}
|
41
|
+
)
|
42
|
+
.command(
|
43
|
+
'publish',
|
44
|
+
'Publish package which has changed sence last commit.',
|
45
|
+
(yargs) => {
|
46
|
+
return yargs;
|
47
|
+
},
|
48
|
+
(argv) => {
|
49
|
+
const project = argv.project;
|
50
|
+
let chain = Promise.resolve();
|
51
|
+
|
52
|
+
chain = chain.then(() => checkProjectChanges(project));
|
53
|
+
chain = chain.then((hasChanged) => {
|
54
|
+
if (hasChanged) {
|
55
|
+
return publish(project)
|
56
|
+
}
|
57
|
+
});
|
58
|
+
}
|
59
|
+
)
|
60
|
+
.argv;
|
61
|
+
|
62
|
+
/**
|
63
|
+
* 检查指定工程目录下是否存在变更文件
|
64
|
+
* @param {string} projectPath 目标工程路径
|
65
|
+
* @returns 检查结果
|
66
|
+
*/
|
67
|
+
function checkProjectChanges(projectPath) {
|
68
|
+
return childProcess
|
69
|
+
// 使用 git diff 命令查询自上传提交以来,目标工程路径下是否有变更的文件
|
70
|
+
.exec("git", ["diff", "--name-only", "HEAD^", projectPath])
|
71
|
+
.then((returnValue) => {
|
72
|
+
// 提取命令输出结果中的文件集合
|
73
|
+
const changedFiles = returnValue.stdout.split("\n").filter(Boolean);
|
74
|
+
// 根据文件个数确定是否存在变更
|
75
|
+
const hasChanges = changedFiles.length > 0;
|
76
|
+
if (hasChanges) {
|
77
|
+
console.log(`${projectPath} has changed.`)
|
78
|
+
} else {
|
79
|
+
console.log(`Did not detect any changes from ${projectPath} sence last commit.`)
|
80
|
+
}
|
81
|
+
// 返回检测结果
|
82
|
+
return hasChanges;
|
83
|
+
});
|
84
|
+
}
|
85
|
+
|
86
|
+
/**
|
87
|
+
* 更新指定工程的预发布版本
|
88
|
+
* @param {string} projectPath 目标工程路径
|
89
|
+
* @returns 更新后的版本
|
90
|
+
*/
|
91
|
+
function updateProjectPrereleaseVersion(projectPath) {
|
92
|
+
return childProcess
|
93
|
+
// 使用 npm version prerelease 更新指定工程的预发布版本
|
94
|
+
.exec('npm', ['version', 'prerelease', '--preid=beta', '--prefix', projectPath])
|
95
|
+
.then((returnValue) => {
|
96
|
+
// 读取目标工程的package.json文件
|
97
|
+
const packageJsonFilePath = `${projectPath}/package.json`;
|
98
|
+
const packageConfig = JSON.parse(fs.readFileSync(packageJsonFilePath, 'utf-8'));
|
99
|
+
// 提取更新后的版本
|
100
|
+
const updatedVersion = returnValue.stdout;
|
101
|
+
// 向git缓冲区中添加变更
|
102
|
+
childProcess.execSync('git', ['add', '.']);
|
103
|
+
// 提交变更记录
|
104
|
+
childProcess.execSync('git', ['commit', '-m', `update ${packageConfig.name} prerelease version to ${updatedVersion}`]);
|
105
|
+
console.log(`update ${packageConfig.name} prerelease version to ${updatedVersion}`);
|
106
|
+
// 提交变更集
|
107
|
+
return childProcess.exec('git', ['push']);
|
108
|
+
})
|
109
|
+
}
|
110
|
+
|
111
|
+
/**
|
112
|
+
* 发布指定路径下的npm包
|
113
|
+
* @param {string} projectPath 目标工程路径
|
114
|
+
* @returns 发布结果
|
115
|
+
*/
|
116
|
+
function publish(projectPath) {
|
117
|
+
// 读取目标工程的package.json文件
|
118
|
+
const ngPackageJsonFilePath = `${projectPath}/ng-package.json`;
|
119
|
+
const ngPackageConfig = JSON.parse(fs.readFileSync(ngPackageJsonFilePath, 'utf-8'));
|
120
|
+
const dest = ngPackageConfig.dest;
|
121
|
+
const packagePath = path.normalize(`${projectPath}/${dest}`);
|
122
|
+
// 调用 npm publish 发布指定路径下的npm包
|
123
|
+
return childProcess.exec('npm', ['publish', packagePath])
|
124
|
+
}
|
package/package.json
CHANGED
@@ -1,25 +1,25 @@
|
|
1
|
-
{
|
2
|
-
"name": "@farris/cli",
|
3
|
-
"version": "1.0.
|
4
|
-
"description": "Farris command line interface",
|
5
|
-
"main": "index.js",
|
6
|
-
"scripts": {
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
-
},
|
9
|
-
"repository": {
|
10
|
-
"type": "git",
|
11
|
-
"url": "https://git.iec.io/webadp/farris-cli.git"
|
12
|
-
},
|
13
|
-
"keywords": [
|
14
|
-
"Farris",
|
15
|
-
"CLI"
|
16
|
-
],
|
17
|
-
"bin": {
|
18
|
-
"farris": "ci/cli.js"
|
19
|
-
},
|
20
|
-
"author": "Sagi Chen",
|
21
|
-
"license": "ISC",
|
22
|
-
"dependencies": {
|
23
|
-
"lerna": "^4.0.0"
|
24
|
-
}
|
25
|
-
}
|
1
|
+
{
|
2
|
+
"name": "@farris/cli",
|
3
|
+
"version": "1.0.4",
|
4
|
+
"description": "Farris command line interface",
|
5
|
+
"main": "index.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"repository": {
|
10
|
+
"type": "git",
|
11
|
+
"url": "https://git.iec.io/webadp/farris-cli.git"
|
12
|
+
},
|
13
|
+
"keywords": [
|
14
|
+
"Farris",
|
15
|
+
"CLI"
|
16
|
+
],
|
17
|
+
"bin": {
|
18
|
+
"farris": "ci/cli.js"
|
19
|
+
},
|
20
|
+
"author": "Sagi Chen",
|
21
|
+
"license": "ISC",
|
22
|
+
"dependencies": {
|
23
|
+
"lerna": "^4.0.0"
|
24
|
+
}
|
25
|
+
}
|