@farris/cli 1.0.18 → 1.0.21

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