@farris/cli 1.0.9 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/ci/cli.js +69 -17
  2. package/package.json +3 -2
package/ci/cli.js CHANGED
@@ -4,9 +4,12 @@
4
4
 
5
5
  const fs = require('fs');
6
6
  const path = require('path');
7
+ const log = require("npmlog");
8
+ const moment = require("moment");
7
9
  const childProcess = require("@lerna/child-process");
8
10
  const { Project } = require("@lerna/project");
9
11
 
12
+
10
13
  const yargs = require('yargs/yargs');
11
14
  const cli = yargs(process.argv.slice(2), process.cwd())
12
15
 
@@ -73,24 +76,53 @@ cli
73
76
  )
74
77
  .argv;
75
78
 
79
+ /**
80
+ * @param {import("@lerna/child-process").ExecOpts} execOpts
81
+ */
82
+ function getLastCommit() {
83
+ if (hasTags()) {
84
+ log.silly("getLastTagInBranch");
85
+ return childProcess.execSync("git", ["describe", "--tags", "--abbrev=0"]);
86
+ }
87
+ log.silly("getFirstCommit");
88
+ return childProcess.execSync("git", ["rev-list", "--max-parents=0", "HEAD"]);
89
+ }
90
+
91
+ /**
92
+ * @param {import("@lerna/child-process").ExecOpts} opts
93
+ */
94
+ function hasTags() {
95
+ let result = false;
96
+ try {
97
+ result = !!childProcess.execSync("git", ["tag"]);
98
+ } catch (err) {
99
+ log.warn("ENOTAGS", "No git tags were reachable from this branch!");
100
+ log.verbose("hasTags error", err);
101
+ }
102
+ log.verbose("hasTags", result);
103
+ return result;
104
+ }
105
+
106
+
76
107
  /**
77
108
  * 检查指定工程目录下是否存在变更文件
78
109
  * @param {string} projectPath 目标工程路径
79
110
  * @returns 检查结果
80
111
  */
81
112
  function checkProjectChanges(projectPath) {
113
+ const lastCommit = getLastCommit();
82
114
  return childProcess
83
115
  // 使用 git diff 命令查询自上传提交以来,目标工程路径下是否有变更的文件
84
- .exec("git", ["diff", "--name-only", "HEAD^", projectPath])
116
+ .exec("git", ["diff", "--name-only", lastCommit, projectPath])
85
117
  .then((returnValue) => {
86
- // 提取命令输出结果中的文件集合
118
+ // 提取命令输出结果中的文件集合ß
87
119
  const changedFiles = returnValue.stdout.split("\n").filter(Boolean);
88
120
  // 根据文件个数确定是否存在变更
89
121
  const hasChanges = changedFiles.length > 0;
90
122
  if (hasChanges) {
91
- console.log(`${projectPath} has changed.`)
123
+ console.log(`${projectPath} has changed sence last commit ${lastCommit}.`)
92
124
  } else {
93
- console.log(`Did not detect any changes from ${projectPath} sence last commit`)
125
+ console.log(`Did not detect any changes from ${projectPath} sence last commit ${lastCommit}.`)
94
126
  }
95
127
  // 返回检测结果
96
128
  return hasChanges;
@@ -102,7 +134,8 @@ function checkProjectChanges(projectPath) {
102
134
  * @param {string} projectPath 目标工程路径
103
135
  * @returns 更新后的版本
104
136
  */
105
- function updateProjectPrereleaseVersion(projectPath, projectUrl) {
137
+ function updateProjectPrereleaseVersion(projectPath, monoWorkspace) {
138
+ monoWorkspace.updateResult = monoWorkspace.updateResult || {};
106
139
  return childProcess
107
140
  // 使用 npm version prerelease 更新指定工程的预发布版本
108
141
  .exec('npm', ['version', 'prerelease', '--preid=beta', '--prefix', projectPath])
@@ -113,14 +146,13 @@ function updateProjectPrereleaseVersion(projectPath, projectUrl) {
113
146
  // 提取更新后的版本
114
147
  const updatedVersion = returnValue.stdout;
115
148
  console.log(`update ${packageConfig.name} prerelease version to ${updatedVersion}`);
116
- const updateResult = {};
117
- updateResult[packageConfig.name] = updatedVersion;
118
- return updateResult;
149
+ monoWorkspace.updateResult[packageConfig.name] = updatedVersion;
150
+ return monoWorkspace;
119
151
  })
120
152
  }
121
153
 
122
- function commitChanges(updatedVersions, commitUrl) {
123
- const commitMessage = updatedVersions.reduce((latestMessage, updateVersion, index, originalArray) => {
154
+ function builderVersionChangeMessage(prefix, updatedVersions, suffix = '') {
155
+ const versionMessage = updatedVersions.reduce((latestMessage, updateVersion, index, originalArray) => {
124
156
  if (Object.keys(updateVersion).length) {
125
157
  const packageName = Object.keys(updateVersion)[0];
126
158
  const version = updateVersion[packageName];
@@ -131,13 +163,18 @@ function commitChanges(updatedVersions, commitUrl) {
131
163
  latestMessage = latestMessage + message;
132
164
  }
133
165
  return latestMessage;
134
- }, 'update ');
166
+ }, `${prefix} `);
167
+ return `${versionMessage} ${suffix}ß`;
168
+ }
169
+
170
+ function commitChanges(updatedVersions, commitUrl) {
171
+ const commitMessage = builderVersionChangeMessage('Update', updatedVersions, '.');
135
172
 
136
173
  if (commitMessage) {
137
174
  // 向git缓冲区中添加变更
138
175
  childProcess.execSync('git', ['add', '.']);
139
176
  // // 提交变更记录
140
- childProcess.execSync('git', ['commit', '-m', `${commitMessage}. [skip ci]`]);
177
+ childProcess.execSync('git', ['commit', '-m', `${commitMessage} [skip ci]`]);
141
178
 
142
179
  console.log(commitMessage);
143
180
 
@@ -157,6 +194,20 @@ function buildWorkspace(workspace) {
157
194
  // lerna run build --scope=@farris/ide-framework
158
195
  }
159
196
 
197
+ function tagMonoWorkspace(monoWorkspace, commitUrl) {
198
+ const tagMessage = builderVersionChangeMessage('Publish npm packages ', monoWorkspace.updateResult, '.');
199
+ /*from www.w3cschool.cn*/
200
+ const date = moment(new Date());
201
+ const tagVersion = `v${date.format('yyyyMMddhhmmss')}`;
202
+ childProcess.execSync('git', ['tag', tagVersion, '-m', tagMessage]);
203
+ // 提交变更集
204
+ if (commitUrl) {
205
+ return childProcess.exec("git", ["push", '-o', 'ci.skip', commitUrl]);
206
+ } else {
207
+ return childProcess.exec('git', ['push', '-o', 'ci.skip']);
208
+ }
209
+ }
210
+
160
211
  /**
161
212
  * 发布指定路径下的npm包
162
213
  * @param {string} projectPath 目标工程路径
@@ -217,9 +268,10 @@ function publishAll(commitUrl) {
217
268
  });
218
269
  chain = chain.then(monoWorkspace => {
219
270
  const changedProjects = monoWorkspace.changedProjects;
220
- const prereleasePromise = changedProjects.map(projectInfo => updateProjectPrereleaseVersion(projectInfo.project.path));
271
+ const prereleasePromise = changedProjects.map(projectInfo => updateProjectPrereleaseVersion(projectInfo.project.path, monoWorkspace));
221
272
  return Promise.all(prereleasePromise)
222
- .then((updatedVersions) => {
273
+ .then((monoWorkspace) => {
274
+ const updatedVersions = monoWorkspace.updateResult;
223
275
  commitChanges(updatedVersions, commitUrl);
224
276
  return Promise.resolve(monoWorkspace);
225
277
  });
@@ -236,8 +288,8 @@ function publishAll(commitUrl) {
236
288
  const publishPromises = changedProjects.map(projectInfo => publish(projectInfo.project.path));
237
289
  Promise.all(publishPromises)
238
290
  .then(() => {
239
- console.log('published');
240
- })
291
+ return tagMonoWorkspace(monoWorkspace, commitUrl);
292
+ });
241
293
  });
242
294
  }
243
295
 
@@ -262,7 +314,7 @@ function readWorkspace(angularJson, packageJson) {
262
314
  // workspace.set(projectName, projectInfo);
263
315
  workspace.projects.push(projectInfo);
264
316
  }
265
- return workspace; ßß
317
+ return workspace;
266
318
  }, workspace);
267
319
  return workspace;
268
320
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farris/cli",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Farris command line interface",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,6 +20,7 @@
20
20
  "author": "Sagi Chen",
21
21
  "license": "ISC",
22
22
  "dependencies": {
23
- "lerna": "^4.0.0"
23
+ "lerna": "^4.0.0",
24
+ "moment": "^2.29.3"
24
25
  }
25
26
  }