@farris/cli 1.0.17 → 1.0.20

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.
Files changed (2) hide show
  1. package/ci/cli.js +158 -37
  2. package/package.json +1 -1
package/ci/cli.js CHANGED
@@ -22,7 +22,9 @@ cli
22
22
  },
23
23
  (argv) => {
24
24
  const projectPath = argv.project;
25
- checkProjectChanges(projectPath);
25
+ const lastCommit = getLastCommit();
26
+ console.log(`last commit ${lastCommit}`);
27
+ checkProjectChanges(projectPath, latestMessage);
26
28
  }
27
29
  )
28
30
  .command(
@@ -32,16 +34,58 @@ cli
32
34
  return yargs;
33
35
  },
34
36
  (argv) => {
37
+ // 读取预发布工程路径
35
38
  const project = argv.project;
39
+ // 读取commit url地址
36
40
  const url = argv.url;
37
- let chain = Promise.resolve();
38
-
39
- chain = chain.then(() => checkProjectChanges(project));
40
- chain = chain.then((hasChanged) => {
41
- if (hasChanged) {
42
- return updateProjectPrereleaseVersion(project, url)
43
- }
44
- });
41
+ // 读取是否预发布全部工程
42
+ const prereleaseAll = argv.all;
43
+ if (prereleaseAll) {
44
+ console.log('Prerelease all.');
45
+ // 发布所有变更的项目
46
+ return publish(url, 'prerelease');
47
+ } else if (project) {
48
+ let chain = Promise.resolve();
49
+ const lastCommit = getLastCommit();
50
+ console.log(`last commit ${lastCommit}`);
51
+ chain = chain.then(() => checkProjectChanges(project, lastCommit));
52
+ chain = chain.then((hasChanged) => {
53
+ const projectInfo = { 'project': { 'path': project } };
54
+ if (hasChanged) {
55
+ return updateProjectVersion(projectInfo, 'prerelease')
56
+ }
57
+ });
58
+ } else {
59
+ console.log('You must provide param of either --all or --project.')
60
+ }
61
+ }
62
+ )
63
+ .command(
64
+ 'patch',
65
+ 'Update package patch version when package has changed.',
66
+ (yargs) => {
67
+ return yargs;
68
+ },
69
+ (argv) => {
70
+ const project = argv.project;
71
+ const url = argv.url;
72
+ const patchAll = argv.all;
73
+ if (patchAll) {
74
+ return publish(url, 'patch');
75
+ } else if (project) {
76
+ let chain = Promise.resolve();
77
+ const lastCommit = getLastCommit();
78
+ console.log(`last commit ${lastCommit}`);
79
+ chain = chain.then(() => checkProjectChanges(project, lastCommit));
80
+ chain = chain.then((hasChanged) => {
81
+ if (hasChanged) {
82
+ const projectInfo = { 'project': { 'path': project } };
83
+ return updateProjectVersion(projectInfo, 'patch')
84
+ }
85
+ });
86
+ } else {
87
+ console.log('You must provide param of either --all or --project.')
88
+ }
45
89
  }
46
90
  )
47
91
  .command(
@@ -53,8 +97,9 @@ cli
53
97
  (argv) => {
54
98
  const project = argv.project;
55
99
  let chain = Promise.resolve();
56
-
57
- chain = chain.then(() => checkProjectChanges(project));
100
+ const lastCommit = getLastCommit();
101
+ console.log(`last commit ${lastCommit}`);
102
+ chain = chain.then(() => checkProjectChanges(project, lastCommit));
58
103
  chain = chain.then((hasChanged) => {
59
104
  if (hasChanged) {
60
105
  return publish(project)
@@ -106,12 +151,13 @@ function hasTags() {
106
151
 
107
152
  /**
108
153
  * 检查指定工程目录下是否存在变更文件
109
- * @param {string} projectPath 目标工程路径
154
+ * @param {string} projectInfo 目标工程信息
155
+ * @param {string} lastCommit 最后提交标识
110
156
  * @returns 检查结果
111
157
  */
112
- function checkProjectChanges(projectPath) {
113
- const lastCommit = getLastCommit();
114
- console.log(`last commit ${lastCommit}`);
158
+ function checkProjectChanges(projectInfo, lastCommit) {
159
+ const projectPath = projectInfo?.project?.path;
160
+ const packageName = projectInfo?.package?.name || projectPath;
115
161
  return childProcess
116
162
  // 使用 git diff 命令查询自上传提交以来,目标工程路径下是否有变更的文件
117
163
  .exec("git", ["diff", "--name-only", lastCommit, projectPath])
@@ -121,9 +167,10 @@ function checkProjectChanges(projectPath) {
121
167
  // 根据文件个数确定是否存在变更
122
168
  const hasChanges = changedFiles.length > 0;
123
169
  if (hasChanges) {
124
- console.log(`${projectPath} has changed sence last commit ${lastCommit}.`)
170
+ projectInfo.workspace.hasChanged = hasChanges;
171
+ console.log(`${packageName} has changed sence last commit ${lastCommit}.`)
125
172
  } else {
126
- console.log(`Did not detect any changes from ${projectPath} sence last commit ${lastCommit}.`)
173
+ console.log(`${packageName} has not changed.`)
127
174
  }
128
175
  // 返回检测结果
129
176
  return hasChanges;
@@ -132,13 +179,32 @@ function checkProjectChanges(projectPath) {
132
179
 
133
180
  /**
134
181
  * 更新指定工程的预发布版本
135
- * @param {string} projectPath 目标工程路径
182
+ * @param {string} projectInfo 目标工程信息
136
183
  * @returns 更新后的版本
137
184
  */
138
- function updateProjectPrereleaseVersion(projectPath) {
185
+ function updateProjectVersion(projectInfo, updateVersionType) {
186
+ let npmCommandArray = [];
187
+ const projectPath = projectInfo.project.path;
188
+ if (!projectPath) {
189
+ throw new Error(`update project version error: you must provide project path by project info object.`)
190
+ }
191
+ switch (updateVersionType) {
192
+ case 'prerelease':
193
+ npmCommandArray = ['version', 'prerelease', '--preid=beta', '--prefix', projectPath];
194
+ break;
195
+ case 'patch':
196
+ npmCommandArray = ['version', 'patch', '--prefix', projectPath];
197
+ break;
198
+ case 'minor':
199
+ npmCommandArray = ['version', 'minor', '--prefix', projectPath];
200
+ break;
201
+ case 'major':
202
+ npmCommandArray = ['version', 'major', '--prefix', projectPath];
203
+ break;
204
+ }
139
205
  return childProcess
140
206
  // 使用 npm version prerelease 更新指定工程的预发布版本
141
- .exec('npm', ['version', 'prerelease', '--preid=beta', '--prefix', projectPath])
207
+ .exec('npm', npmCommandArray)
142
208
  .then((returnValue) => {
143
209
  // 读取目标工程的package.json文件
144
210
  const packageJsonFilePath = `${projectPath}/package.json`;
@@ -152,6 +218,29 @@ function updateProjectPrereleaseVersion(projectPath) {
152
218
  })
153
219
  }
154
220
 
221
+ function updateMonoWorkspaceVersion(monoWorkspace, updateVersionType) {
222
+ let npmCommandArray = [];
223
+ switch (updateVersionType) {
224
+ case 'prerelease':
225
+ npmCommandArray = ['version', 'prerelease', '--preid=beta'];
226
+ break;
227
+ case 'patch':
228
+ npmCommandArray = ['version', 'patch'];
229
+ break;
230
+ case 'minor':
231
+ npmCommandArray = ['version', 'minor'];
232
+ break;
233
+ case 'major':
234
+ npmCommandArray = ['version', 'major'];
235
+ break;
236
+ }
237
+ childProcess.execSync('npm', npmCommandArray);
238
+ const packageJsonFilePath = `./package.json`;
239
+ const packageConfig = JSON.parse(fs.readFileSync(packageJsonFilePath, 'utf-8'));
240
+ console.log(`update MonoWorkspace version to v${packageConfig.version}`);
241
+ monoWorkspace.version = packageConfig.version;
242
+ }
243
+
155
244
  function builderVersionChangeMessage(prefix, updatedVersions, suffix = '') {
156
245
  const versionMessage = Object.keys(updatedVersions).reduce((latestMessage, packageName, index, originalArray) => {
157
246
  const version = updatedVersions[packageName];
@@ -192,11 +281,12 @@ function buildWorkspace(workspace) {
192
281
  // lerna run build --scope=@farris/ide-framework
193
282
  }
194
283
 
195
- function tagMonoWorkspace(monoWorkspace, commitUrl) {
284
+ function addTagToMonoWorkspace(monoWorkspace, commitUrl) {
196
285
  // const tagMessage = builderVersionChangeMessage('Publish npm packages ', monoWorkspace.updateResult, '.');
197
- const date = moment(new Date());
198
- const tagVersion = `v${date.format('YYYYMMDDHHmmss')}`;
199
- childProcess.execSync('git', ['tag', tagVersion, '-m', 'Publish npm packages. [skip ci]']);
286
+ // const date = moment(new Date());
287
+ // const tagVersion = `v${date.format('YYYYMMDDHHmmss')}`;
288
+ const monoWorkspaceVersion = monoWorkspace.version;
289
+ childProcess.execSync('git', ['tag', monoWorkspaceVersion, '-m', 'Publish npm packages. [skip ci]']);
200
290
  // 提交变更集
201
291
  if (commitUrl) {
202
292
  console.log(`git push --tags ${commitUrl}`);
@@ -212,7 +302,7 @@ function tagMonoWorkspace(monoWorkspace, commitUrl) {
212
302
  * @param {string} projectPath 目标工程路径
213
303
  * @returns 发布结果
214
304
  */
215
- function publish(projectPath) {
305
+ function publishToNpmRepository(projectPath) {
216
306
  // 读取目标工程的package.json文件
217
307
  const ngPackageJsonFilePath = `${projectPath}/ng-package.json`;
218
308
  const ngPackageConfig = JSON.parse(fs.readFileSync(ngPackageJsonFilePath, 'utf-8'));
@@ -222,37 +312,57 @@ function publish(projectPath) {
222
312
  return childProcess.exec('npm', ['publish', packagePath])
223
313
  }
224
314
 
225
- function publishAll(commitUrl) {
315
+ /**
316
+ * 将所有变更发布为新的npm包
317
+ * @param {string} commitUrl commit api 地址
318
+ */
319
+ function publish(commitUrl, updateVersionType) {
320
+ // 初始化当前工作目录下的多代码仓库项目
226
321
  const monoRepoProject = new Project(process.cwd());
227
322
  let chain = Promise.resolve();
323
+ // 1. 提取当前多代码仓库中的所有子项目,创建多项目工作区
228
324
  chain = chain.then(() => {
325
+ // 提取所有子项目
229
326
  return monoRepoProject.getPackages()
230
327
  .then(packages => {
328
+ // 创建并返回多项目工作区
231
329
  const monoWorkspace = { packages };
232
330
  return monoWorkspace;
233
331
  })
234
332
  });
333
+ // 2. 读取多项目工作区下的Angular工作区和所有Angular项目
235
334
  chain = chain.then((monoWorkspace) => {
335
+ // 提取所有子仓库项目
236
336
  const packages = monoWorkspace.packages;
337
+ // 初始化Angular工作区集合
237
338
  monoWorkspace.workspaces = [];
339
+ // 初始化Angular工程集合
238
340
  monoWorkspace.projects = [];
341
+ // 遍历所有子仓库
239
342
  packages.forEach(packageJson => {
343
+ // 读取Angular工作区路径
240
344
  const workspacePath = packageJson.location;
345
+ // 读取Angular工作区下的angular.json文件内容
241
346
  const angularJson = readAngularJson(workspacePath)
347
+ // 构造Angular工作区
242
348
  const workspace = readWorkspace(angularJson, packageJson);
349
+ // 记录Angular工作区对象
243
350
  monoWorkspace.workspaces.push(workspace);
351
+ // 遍历Angular工作区下的Angular项目,归集到根工作区中
244
352
  workspace.projects.reduce((monoWorkspace, projectInfo) => {
245
353
  monoWorkspace.projects.push(projectInfo);
246
354
  return monoWorkspace;
247
355
  }, monoWorkspace);
248
- // .forEach((projectInfo) => monoWorkspace.projects.push(projectInfo));
249
356
  });
250
357
  return monoWorkspace;
251
358
  });
359
+ // 3. 检查代码提交记录,获取自上次发布以来所有发生变化的Angular工程
252
360
  chain = chain.then(monoWorkspace => {
253
361
  const projects = monoWorkspace.projects;
254
362
  const checkProjects = projects.map((projectInfo) => {
255
- let checkPromise = checkProjectChanges(projectInfo.project.path);
363
+ const lastCommit = getLastCommit();
364
+ console.log(`last commit ${lastCommit}`); Í
365
+ let checkPromise = checkProjectChanges(projectInfo, lastCommit);
256
366
  let changedPromise = checkPromise.then(hasChanged => {
257
367
  projectInfo.hasChanged = hasChanged;
258
368
  return Promise.resolve(projectInfo);
@@ -265,9 +375,13 @@ function publishAll(commitUrl) {
265
375
  return Promise.resolve(monoWorkspace);
266
376
  });
267
377
  });
378
+ // 4. 更新所有变更项目预发布版本,提交代码仓库,供发布时使用
268
379
  chain = chain.then(monoWorkspace => {
269
380
  const changedProjects = monoWorkspace.changedProjects;
270
- const prereleasePromise = changedProjects.map(projectInfo => updateProjectPrereleaseVersion(projectInfo.project.path, monoWorkspace));
381
+ const prereleasePromise = changedProjects.map(projectInfo => updateProjectVersion(projectInfo, updateVersionType));
382
+ if (changedProjects) {
383
+
384
+ }
271
385
  return Promise.all(prereleasePromise)
272
386
  .then((results) => {
273
387
  results.reduce((workspace, updateResult) => {
@@ -275,24 +389,30 @@ function publishAll(commitUrl) {
275
389
  Object.assign(workspace.updateResult, updateResult);
276
390
  return workspace;
277
391
  }, monoWorkspace);
392
+ updateMonoWorkspaceVersion(monoWorkspace, updateVersionType);
278
393
  const updatedVersions = monoWorkspace.updateResult;
279
394
  commitChanges(updatedVersions, commitUrl);
280
395
  return Promise.resolve(monoWorkspace);
281
396
  });
282
397
  });
398
+ // 5. 编译所有Angular工作区
283
399
  chain = chain.then(monoWorkspace => {
284
- const buildPromises = monoWorkspace.workspaces.map((workspace) => buildWorkspace(workspace));
400
+ const buildPromises = monoWorkspace.workspaces
401
+ .filter(workspace => workspace.hasChanged)
402
+ .map((workspace) => buildWorkspace(workspace));
285
403
  return Promise.all(buildPromises)
286
404
  .then((results) => {
405
+ console.log(results.stdout);
287
406
  return Promise.resolve(monoWorkspace);
288
407
  });
289
408
  });
409
+ // 6. 将变更项目的内容发布为npm包,并增加发布Tag标签
290
410
  chain = chain.then(monoWorkspace => {
291
411
  const changedProjects = monoWorkspace.changedProjects;
292
- const publishPromises = changedProjects.map(projectInfo => publish(projectInfo.project.path));
412
+ const publishPromises = changedProjects.map(projectInfo => publishToNpmRepository(projectInfo.project.path));
293
413
  Promise.all(publishPromises)
294
414
  .then(() => {
295
- return tagMonoWorkspace(monoWorkspace, commitUrl);
415
+ return addTagToMonoWorkspace(monoWorkspace, commitUrl);
296
416
  });
297
417
  });
298
418
  }
@@ -309,13 +429,13 @@ function readWorkspace(angularJson, packageJson) {
309
429
  const workspace = {
310
430
  packageName,
311
431
  path: workspacePath,
312
- projects: []
432
+ projects: [],
433
+ hasChanged: false
313
434
  };
314
435
  Object.keys(projects)
315
436
  .reduce((workspace, projectName) => {
316
- const projectInfo = readProjectInfo(projectName, projects[projectName], workspacePath);
437
+ const projectInfo = readProjectInfo(projectName, projects[projectName], workspace);
317
438
  if (projectInfo) {
318
- // workspace.set(projectName, projectInfo);
319
439
  workspace.projects.push(projectInfo);
320
440
  }
321
441
  return workspace;
@@ -323,8 +443,8 @@ function readWorkspace(angularJson, packageJson) {
323
443
  return workspace;
324
444
  }
325
445
 
326
- function readProjectInfo(projectName, project, workspacePath) {
327
- const projectPath = path.normalize(`${workspacePath}/${project.root}`);
446
+ function readProjectInfo(projectName, project, workspace) {
447
+ const projectPath = path.normalize(`${workspace.path}/${project.root}`);
328
448
  const projectType = project.projectType;
329
449
  if (projectType === 'library') {
330
450
  const packageJsonPath = path.normalize(`${projectPath}/package.json`);
@@ -334,6 +454,7 @@ function readProjectInfo(projectName, project, workspacePath) {
334
454
  const ngPackage = JSON.parse(fs.readFileSync(ngPackagePath, 'utf-8'));
335
455
  const packagePath = path.normalize(`${projectPath}/${ngPackage.dest}`);
336
456
  return {
457
+ workspace,
337
458
  project: {
338
459
  name: projectName,
339
460
  path: projectPath,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farris/cli",
3
- "version": "1.0.17",
3
+ "version": "1.0.20",
4
4
  "description": "Farris command line interface",
5
5
  "main": "index.js",
6
6
  "scripts": {