@farris/cli 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +2 -0
- package/ci/cli.js +107 -0
- package/package.json +25 -0
package/README.md
ADDED
package/ci/cli.js
ADDED
@@ -0,0 +1,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
|
+
'prerelease',
|
15
|
+
'Update package prerelease version when package has changed.',
|
16
|
+
(yargs) => {
|
17
|
+
return yargs;
|
18
|
+
},
|
19
|
+
(argv) => {
|
20
|
+
const project = argv.project;
|
21
|
+
let chain = Promise.resolve();
|
22
|
+
|
23
|
+
chain = chain.then(() => checkProjectChanges(project));
|
24
|
+
chain = chain.then((hasChanged) => {
|
25
|
+
if (hasChanged) {
|
26
|
+
return updateProjectPrereleaseVersion(project)
|
27
|
+
}
|
28
|
+
});
|
29
|
+
}
|
30
|
+
)
|
31
|
+
.command(
|
32
|
+
'publish',
|
33
|
+
'Publish package which has changed sence last commit.',
|
34
|
+
(yargs) => {
|
35
|
+
return yargs;
|
36
|
+
},
|
37
|
+
(argv) => {
|
38
|
+
const project = argv.project;
|
39
|
+
let chain = Promise.resolve();
|
40
|
+
|
41
|
+
chain = chain.then(() => checkProjectChanges(project));
|
42
|
+
chain = chain.then((hasChanged) => {
|
43
|
+
if (hasChanged) {
|
44
|
+
return publish(project)
|
45
|
+
}
|
46
|
+
});
|
47
|
+
}
|
48
|
+
)
|
49
|
+
.argv;
|
50
|
+
|
51
|
+
/**
|
52
|
+
* 检查指定工程目录下是否存在变更文件
|
53
|
+
* @param {string} projectPath 目标工程路径
|
54
|
+
* @returns 检查结果
|
55
|
+
*/
|
56
|
+
function checkProjectChanges(projectPath) {
|
57
|
+
return childProcess
|
58
|
+
// 使用 git diff 命令查询自上传提交以来,目标工程路径下是否有变更的文件
|
59
|
+
.exec("git", ["diff", "--name-only", "HEAD^", projectPath])
|
60
|
+
.then((returnValue) => {
|
61
|
+
// 提取命令输出结果中的文件集合
|
62
|
+
const changedFiles = returnValue.stdout.split("\n").filter(Boolean);
|
63
|
+
// 根据文件个数确定是否存在变更
|
64
|
+
const hasChanges = changedFiles.length > 0;
|
65
|
+
// 返回检测结果
|
66
|
+
return hasChanges;
|
67
|
+
});
|
68
|
+
}
|
69
|
+
|
70
|
+
/**
|
71
|
+
* 更新指定工程的预发布版本
|
72
|
+
* @param {string} projectPath 目标工程路径
|
73
|
+
* @returns 更新后的版本
|
74
|
+
*/
|
75
|
+
function updateProjectPrereleaseVersion(projectPath) {
|
76
|
+
return childProcess
|
77
|
+
// 使用 npm version prerelease 更新指定工程的预发布版本
|
78
|
+
.exec('npm', ['version', 'prerelease', '--preid=beta', '--prefix', projectPath])
|
79
|
+
.then((returnValue) => {
|
80
|
+
// 读取目标工程的package.json文件
|
81
|
+
const packageJsonFilePath = `${projectPath}/package.json`;
|
82
|
+
const packageConfig = JSON.parse(fs.readFileSync(packageJsonFilePath, 'utf-8'));
|
83
|
+
// 提取更新后的版本
|
84
|
+
const updatedVersion = returnValue.stdout;
|
85
|
+
// 向git缓冲区中添加变更
|
86
|
+
childProcess.execSync('git', ['add', '.']);
|
87
|
+
// 提交变更记录
|
88
|
+
childProcess.execSync('git', ['commit', '-m', `update ${packageConfig.name} prerelease version to ${updatedVersion}`]);
|
89
|
+
// 提交变更集
|
90
|
+
return childProcess.exec('git', ['push']);
|
91
|
+
})
|
92
|
+
}
|
93
|
+
|
94
|
+
/**
|
95
|
+
* 发布指定路径下的npm包
|
96
|
+
* @param {string} projectPath 目标工程路径
|
97
|
+
* @returns 发布结果
|
98
|
+
*/
|
99
|
+
function publish(projectPath) {
|
100
|
+
// 读取目标工程的package.json文件
|
101
|
+
const ngPackageJsonFilePath = `${projectPath}/ng-package.json`;
|
102
|
+
const ngPackageConfig = JSON.parse(fs.readFileSync(ngPackageJsonFilePath, 'utf-8'));
|
103
|
+
const dest = ngPackageConfig.dest;
|
104
|
+
const packagePath = path.normalize(`${projectPath}/${dest}`);
|
105
|
+
// 调用 npm publish 发布指定路径下的npm包
|
106
|
+
return childProcess.exec('npm', ['publish', packagePath])
|
107
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
{
|
2
|
+
"name": "@farris/cli",
|
3
|
+
"version": "1.0.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/cil.js"
|
19
|
+
},
|
20
|
+
"author": "Sagi Chen",
|
21
|
+
"license": "ISC",
|
22
|
+
"dependencies": {
|
23
|
+
"lerna": "^4.0.0"
|
24
|
+
}
|
25
|
+
}
|