@farris/cli 1.0.16 → 1.0.19
Sign up to get free protection for your applications and to get access to all the features.
- package/ci/cli.js +161 -39
- 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
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
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)
|
@@ -81,10 +126,10 @@ cli
|
|
81
126
|
*/
|
82
127
|
function getLastCommit() {
|
83
128
|
if (hasTags()) {
|
84
|
-
log
|
129
|
+
console.log("getLastTagInBranch");
|
85
130
|
return childProcess.execSync("git", ["describe", "--tags", "--abbrev=0"]);
|
86
131
|
}
|
87
|
-
log
|
132
|
+
console.log("getFirstCommit");
|
88
133
|
return childProcess.execSync("git", ["rev-list", "--max-parents=0", "HEAD"]);
|
89
134
|
}
|
90
135
|
|
@@ -106,11 +151,13 @@ function hasTags() {
|
|
106
151
|
|
107
152
|
/**
|
108
153
|
* 检查指定工程目录下是否存在变更文件
|
109
|
-
* @param {string}
|
154
|
+
* @param {string} projectInfo 目标工程信息
|
155
|
+
* @param {string} lastCommit 最后提交标识
|
110
156
|
* @returns 检查结果
|
111
157
|
*/
|
112
|
-
function checkProjectChanges(
|
113
|
-
const
|
158
|
+
function checkProjectChanges(projectInfo, lastCommit) {
|
159
|
+
const projectPath = projectInfo?.project?.path;
|
160
|
+
const packageName = projectInfo?.package?.name || projectPath;
|
114
161
|
return childProcess
|
115
162
|
// 使用 git diff 命令查询自上传提交以来,目标工程路径下是否有变更的文件
|
116
163
|
.exec("git", ["diff", "--name-only", lastCommit, projectPath])
|
@@ -120,9 +167,10 @@ function checkProjectChanges(projectPath) {
|
|
120
167
|
// 根据文件个数确定是否存在变更
|
121
168
|
const hasChanges = changedFiles.length > 0;
|
122
169
|
if (hasChanges) {
|
123
|
-
|
170
|
+
projectInfo.workspace.hasChanged = hasChanges;
|
171
|
+
console.log(`${packageName} has changed sence last commit ${lastCommit}.`)
|
124
172
|
} else {
|
125
|
-
console.log(
|
173
|
+
console.log(`${packageName} has not changed.`)
|
126
174
|
}
|
127
175
|
// 返回检测结果
|
128
176
|
return hasChanges;
|
@@ -131,13 +179,32 @@ function checkProjectChanges(projectPath) {
|
|
131
179
|
|
132
180
|
/**
|
133
181
|
* 更新指定工程的预发布版本
|
134
|
-
* @param {string}
|
182
|
+
* @param {string} projectInfo 目标工程信息
|
135
183
|
* @returns 更新后的版本
|
136
184
|
*/
|
137
|
-
function
|
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
|
+
}
|
138
205
|
return childProcess
|
139
206
|
// 使用 npm version prerelease 更新指定工程的预发布版本
|
140
|
-
.exec('npm',
|
207
|
+
.exec('npm', npmCommandArray)
|
141
208
|
.then((returnValue) => {
|
142
209
|
// 读取目标工程的package.json文件
|
143
210
|
const packageJsonFilePath = `${projectPath}/package.json`;
|
@@ -151,6 +218,29 @@ function updateProjectPrereleaseVersion(projectPath) {
|
|
151
218
|
})
|
152
219
|
}
|
153
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
|
+
|
154
244
|
function builderVersionChangeMessage(prefix, updatedVersions, suffix = '') {
|
155
245
|
const versionMessage = Object.keys(updatedVersions).reduce((latestMessage, packageName, index, originalArray) => {
|
156
246
|
const version = updatedVersions[packageName];
|
@@ -178,7 +268,7 @@ function commitChanges(updatedVersions, commitUrl) {
|
|
178
268
|
console.log(`executing git push ${branch}`);
|
179
269
|
// 提交变更集
|
180
270
|
if (commitUrl) {
|
181
|
-
return childProcess.exec("git", ["push",
|
271
|
+
return childProcess.exec("git", ["push", commitUrl]);
|
182
272
|
} else {
|
183
273
|
return childProcess.exec('git', ['push']);
|
184
274
|
}
|
@@ -191,11 +281,12 @@ function buildWorkspace(workspace) {
|
|
191
281
|
// lerna run build --scope=@farris/ide-framework
|
192
282
|
}
|
193
283
|
|
194
|
-
function
|
284
|
+
function addTagToMonoWorkspace(monoWorkspace, commitUrl) {
|
195
285
|
// const tagMessage = builderVersionChangeMessage('Publish npm packages ', monoWorkspace.updateResult, '.');
|
196
|
-
const date = moment(new Date());
|
197
|
-
const tagVersion = `v${date.format('YYYYMMDDHHmmss')}`;
|
198
|
-
|
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]']);
|
199
290
|
// 提交变更集
|
200
291
|
if (commitUrl) {
|
201
292
|
console.log(`git push --tags ${commitUrl}`);
|
@@ -211,7 +302,7 @@ function tagMonoWorkspace(monoWorkspace, commitUrl) {
|
|
211
302
|
* @param {string} projectPath 目标工程路径
|
212
303
|
* @returns 发布结果
|
213
304
|
*/
|
214
|
-
function
|
305
|
+
function publishToNpmRepository(projectPath) {
|
215
306
|
// 读取目标工程的package.json文件
|
216
307
|
const ngPackageJsonFilePath = `${projectPath}/ng-package.json`;
|
217
308
|
const ngPackageConfig = JSON.parse(fs.readFileSync(ngPackageJsonFilePath, 'utf-8'));
|
@@ -221,37 +312,57 @@ function publish(projectPath) {
|
|
221
312
|
return childProcess.exec('npm', ['publish', packagePath])
|
222
313
|
}
|
223
314
|
|
224
|
-
|
315
|
+
/**
|
316
|
+
* 将所有变更发布为新的npm包
|
317
|
+
* @param {string} commitUrl commit api 地址
|
318
|
+
*/
|
319
|
+
function publish(commitUrl, updateVersionType) {
|
320
|
+
// 初始化当前工作目录下的多代码仓库项目
|
225
321
|
const monoRepoProject = new Project(process.cwd());
|
226
322
|
let chain = Promise.resolve();
|
323
|
+
// 1. 提取当前多代码仓库中的所有子项目,创建多项目工作区
|
227
324
|
chain = chain.then(() => {
|
325
|
+
// 提取所有子项目
|
228
326
|
return monoRepoProject.getPackages()
|
229
327
|
.then(packages => {
|
328
|
+
// 创建并返回多项目工作区
|
230
329
|
const monoWorkspace = { packages };
|
231
330
|
return monoWorkspace;
|
232
331
|
})
|
233
332
|
});
|
333
|
+
// 2. 读取多项目工作区下的Angular工作区和所有Angular项目
|
234
334
|
chain = chain.then((monoWorkspace) => {
|
335
|
+
// 提取所有子仓库项目
|
235
336
|
const packages = monoWorkspace.packages;
|
337
|
+
// 初始化Angular工作区集合
|
236
338
|
monoWorkspace.workspaces = [];
|
339
|
+
// 初始化Angular工程集合
|
237
340
|
monoWorkspace.projects = [];
|
341
|
+
// 遍历所有子仓库
|
238
342
|
packages.forEach(packageJson => {
|
343
|
+
// 读取Angular工作区路径
|
239
344
|
const workspacePath = packageJson.location;
|
345
|
+
// 读取Angular工作区下的angular.json文件内容
|
240
346
|
const angularJson = readAngularJson(workspacePath)
|
347
|
+
// 构造Angular工作区
|
241
348
|
const workspace = readWorkspace(angularJson, packageJson);
|
349
|
+
// 记录Angular工作区对象
|
242
350
|
monoWorkspace.workspaces.push(workspace);
|
351
|
+
// 遍历Angular工作区下的Angular项目,归集到根工作区中
|
243
352
|
workspace.projects.reduce((monoWorkspace, projectInfo) => {
|
244
353
|
monoWorkspace.projects.push(projectInfo);
|
245
354
|
return monoWorkspace;
|
246
355
|
}, monoWorkspace);
|
247
|
-
// .forEach((projectInfo) => monoWorkspace.projects.push(projectInfo));
|
248
356
|
});
|
249
357
|
return monoWorkspace;
|
250
358
|
});
|
359
|
+
// 3. 检查代码提交记录,获取自上次发布以来所有发生变化的Angular工程
|
251
360
|
chain = chain.then(monoWorkspace => {
|
252
361
|
const projects = monoWorkspace.projects;
|
253
362
|
const checkProjects = projects.map((projectInfo) => {
|
254
|
-
|
363
|
+
const lastCommit = getLastCommit();
|
364
|
+
console.log(`last commit ${lastCommit}`); Í
|
365
|
+
let checkPromise = checkProjectChanges(projectInfo, lastCommit);
|
255
366
|
let changedPromise = checkPromise.then(hasChanged => {
|
256
367
|
projectInfo.hasChanged = hasChanged;
|
257
368
|
return Promise.resolve(projectInfo);
|
@@ -264,9 +375,13 @@ function publishAll(commitUrl) {
|
|
264
375
|
return Promise.resolve(monoWorkspace);
|
265
376
|
});
|
266
377
|
});
|
378
|
+
// 4. 更新所有变更项目预发布版本,提交代码仓库,供发布时使用
|
267
379
|
chain = chain.then(monoWorkspace => {
|
268
380
|
const changedProjects = monoWorkspace.changedProjects;
|
269
|
-
const prereleasePromise = changedProjects.map(projectInfo =>
|
381
|
+
const prereleasePromise = changedProjects.map(projectInfo => updateProjectVersion(projectInfo, updateVersionType));
|
382
|
+
if (changedProjects) {
|
383
|
+
|
384
|
+
}
|
270
385
|
return Promise.all(prereleasePromise)
|
271
386
|
.then((results) => {
|
272
387
|
results.reduce((workspace, updateResult) => {
|
@@ -274,24 +389,30 @@ function publishAll(commitUrl) {
|
|
274
389
|
Object.assign(workspace.updateResult, updateResult);
|
275
390
|
return workspace;
|
276
391
|
}, monoWorkspace);
|
392
|
+
updateMonoWorkspaceVersion(monoWorkspace, updateVersionType);
|
277
393
|
const updatedVersions = monoWorkspace.updateResult;
|
278
394
|
commitChanges(updatedVersions, commitUrl);
|
279
395
|
return Promise.resolve(monoWorkspace);
|
280
396
|
});
|
281
397
|
});
|
398
|
+
// 5. 编译所有Angular工作区
|
282
399
|
chain = chain.then(monoWorkspace => {
|
283
|
-
const buildPromises = monoWorkspace.workspaces
|
400
|
+
const buildPromises = monoWorkspace.workspaces
|
401
|
+
.filter(workspace => workspace.hasChanged)
|
402
|
+
.map((workspace) => buildWorkspace(workspace));
|
284
403
|
return Promise.all(buildPromises)
|
285
404
|
.then((results) => {
|
405
|
+
console.log(results.stdout);
|
286
406
|
return Promise.resolve(monoWorkspace);
|
287
407
|
});
|
288
408
|
});
|
409
|
+
// 6. 将变更项目的内容发布为npm包,并增加发布Tag标签
|
289
410
|
chain = chain.then(monoWorkspace => {
|
290
411
|
const changedProjects = monoWorkspace.changedProjects;
|
291
|
-
const publishPromises = changedProjects.map(projectInfo =>
|
412
|
+
const publishPromises = changedProjects.map(projectInfo => publishToNpmRepository(projectInfo.project.path));
|
292
413
|
Promise.all(publishPromises)
|
293
414
|
.then(() => {
|
294
|
-
return
|
415
|
+
return addTagToMonoWorkspace(monoWorkspace, commitUrl);
|
295
416
|
});
|
296
417
|
});
|
297
418
|
}
|
@@ -308,13 +429,13 @@ function readWorkspace(angularJson, packageJson) {
|
|
308
429
|
const workspace = {
|
309
430
|
packageName,
|
310
431
|
path: workspacePath,
|
311
|
-
projects: []
|
432
|
+
projects: [],
|
433
|
+
hasChanged: false
|
312
434
|
};
|
313
435
|
Object.keys(projects)
|
314
436
|
.reduce((workspace, projectName) => {
|
315
|
-
const projectInfo = readProjectInfo(projectName, projects[projectName],
|
437
|
+
const projectInfo = readProjectInfo(projectName, projects[projectName], workspace);
|
316
438
|
if (projectInfo) {
|
317
|
-
// workspace.set(projectName, projectInfo);
|
318
439
|
workspace.projects.push(projectInfo);
|
319
440
|
}
|
320
441
|
return workspace;
|
@@ -322,8 +443,8 @@ function readWorkspace(angularJson, packageJson) {
|
|
322
443
|
return workspace;
|
323
444
|
}
|
324
445
|
|
325
|
-
function readProjectInfo(projectName, project,
|
326
|
-
const projectPath = path.normalize(`${
|
446
|
+
function readProjectInfo(projectName, project, workspace) {
|
447
|
+
const projectPath = path.normalize(`${workspace.path}/${project.root}`);
|
327
448
|
const projectType = project.projectType;
|
328
449
|
if (projectType === 'library') {
|
329
450
|
const packageJsonPath = path.normalize(`${projectPath}/package.json`);
|
@@ -333,6 +454,7 @@ function readProjectInfo(projectName, project, workspacePath) {
|
|
333
454
|
const ngPackage = JSON.parse(fs.readFileSync(ngPackagePath, 'utf-8'));
|
334
455
|
const packagePath = path.normalize(`${projectPath}/${ngPackage.dest}`);
|
335
456
|
return {
|
457
|
+
workspace,
|
336
458
|
project: {
|
337
459
|
name: projectName,
|
338
460
|
path: projectPath,
|