@farris/cli 1.0.6 → 1.0.9

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 +179 -16
  2. package/package.json +1 -1
package/ci/cli.js CHANGED
@@ -5,6 +5,7 @@
5
5
  const fs = require('fs');
6
6
  const path = require('path');
7
7
  const childProcess = require("@lerna/child-process");
8
+ const { Project } = require("@lerna/project");
8
9
 
9
10
  const yargs = require('yargs/yargs');
10
11
  const cli = yargs(process.argv.slice(2), process.cwd())
@@ -17,8 +18,8 @@ cli
17
18
  return yargs;
18
19
  },
19
20
  (argv) => {
20
- const project = argv.project;
21
- checkProjectChanges(project);
21
+ const projectPath = argv.project;
22
+ checkProjectChanges(projectPath);
22
23
  }
23
24
  )
24
25
  .command(
@@ -29,12 +30,13 @@ cli
29
30
  },
30
31
  (argv) => {
31
32
  const project = argv.project;
33
+ const url = argv.url;
32
34
  let chain = Promise.resolve();
33
35
 
34
36
  chain = chain.then(() => checkProjectChanges(project));
35
37
  chain = chain.then((hasChanged) => {
36
38
  if (hasChanged) {
37
- return updateProjectPrereleaseVersion(project)
39
+ return updateProjectPrereleaseVersion(project, url)
38
40
  }
39
41
  });
40
42
  }
@@ -57,6 +59,18 @@ cli
57
59
  });
58
60
  }
59
61
  )
62
+ .command(
63
+ 'publishAll',
64
+ 'Publish all packages that have changed sence latst publish.',
65
+ (yargs) => {
66
+ return yargs;
67
+ },
68
+ (argv) => {
69
+ const projectPath = argv.project;
70
+ const url = argv.url;
71
+ publishAll(url);
72
+ }
73
+ )
60
74
  .argv;
61
75
 
62
76
  /**
@@ -76,7 +90,7 @@ function checkProjectChanges(projectPath) {
76
90
  if (hasChanges) {
77
91
  console.log(`${projectPath} has changed.`)
78
92
  } else {
79
- console.log(`Did not detect any changes from ${projectPath} sence last commit.`)
93
+ console.log(`Did not detect any changes from ${projectPath} sence last commit`)
80
94
  }
81
95
  // 返回检测结果
82
96
  return hasChanges;
@@ -88,7 +102,7 @@ function checkProjectChanges(projectPath) {
88
102
  * @param {string} projectPath 目标工程路径
89
103
  * @returns 更新后的版本
90
104
  */
91
- function updateProjectPrereleaseVersion(projectPath) {
105
+ function updateProjectPrereleaseVersion(projectPath, projectUrl) {
92
106
  return childProcess
93
107
  // 使用 npm version prerelease 更新指定工程的预发布版本
94
108
  .exec('npm', ['version', 'prerelease', '--preid=beta', '--prefix', projectPath])
@@ -98,21 +112,51 @@ function updateProjectPrereleaseVersion(projectPath) {
98
112
  const packageConfig = JSON.parse(fs.readFileSync(packageJsonFilePath, 'utf-8'));
99
113
  // 提取更新后的版本
100
114
  const updatedVersion = returnValue.stdout;
101
- // 向git缓冲区中添加变更
102
- childProcess.execSync('git', ['add', '.']);
103
- // 提交变更记录
104
- childProcess.execSync('git', ['commit', '-m', `update ${packageConfig.name} prerelease version to ${updatedVersion}`]);
105
115
  console.log(`update ${packageConfig.name} prerelease version to ${updatedVersion}`);
106
- // 提交变更集
107
- // return childProcess.exec('git', ['push']);
108
- const branch = childProcess.execSync("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
109
- console.log(`executing git push origin ${branch}`);
110
- const url = `https://project_4166_bot:8sTE2mjQyAycz13EPJAA@git.iec.io/webadp/farris-ide.git/`
111
- return childProcess.exec("git", ["push", url]);
112
- // git push https://myusername:${PROJECT_ACCESS_TOKEN}@gitlab.mydomain.com/mygroup/myproject.git
116
+ const updateResult = {};
117
+ updateResult[packageConfig.name] = updatedVersion;
118
+ return updateResult;
113
119
  })
114
120
  }
115
121
 
122
+ function commitChanges(updatedVersions, commitUrl) {
123
+ const commitMessage = updatedVersions.reduce((latestMessage, updateVersion, index, originalArray) => {
124
+ if (Object.keys(updateVersion).length) {
125
+ const packageName = Object.keys(updateVersion)[0];
126
+ const version = updateVersion[packageName];
127
+ let message = `${packageName} to ${version}`;
128
+ if (index <= originalArray.length - 2) {
129
+ message = message + ', ';
130
+ }
131
+ latestMessage = latestMessage + message;
132
+ }
133
+ return latestMessage;
134
+ }, 'update ');
135
+
136
+ if (commitMessage) {
137
+ // 向git缓冲区中添加变更
138
+ childProcess.execSync('git', ['add', '.']);
139
+ // // 提交变更记录
140
+ childProcess.execSync('git', ['commit', '-m', `${commitMessage}. [skip ci]`]);
141
+
142
+ console.log(commitMessage);
143
+
144
+ const branch = childProcess.execSync("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
145
+ console.log(`executing git push ${branch}`);
146
+ // 提交变更集
147
+ if (commitUrl) {
148
+ return childProcess.exec("git", ["push", '-o', 'ci.skip', commitUrl]);
149
+ } else {
150
+ return childProcess.exec('git', ['push', '-o', 'ci.skip']);
151
+ }
152
+ }
153
+ }
154
+
155
+ function buildWorkspace(workspace) {
156
+ return childProcess.exec('lerna', ['run', 'build', `--scope=${workspace.packageName}`]);
157
+ // lerna run build --scope=@farris/ide-framework
158
+ }
159
+
116
160
  /**
117
161
  * 发布指定路径下的npm包
118
162
  * @param {string} projectPath 目标工程路径
@@ -127,3 +171,122 @@ function publish(projectPath) {
127
171
  // 调用 npm publish 发布指定路径下的npm包
128
172
  return childProcess.exec('npm', ['publish', packagePath])
129
173
  }
174
+
175
+ function publishAll(commitUrl) {
176
+ const monoRepoProject = new Project(process.cwd());
177
+ let chain = Promise.resolve();
178
+ chain = chain.then(() => {
179
+ return monoRepoProject.getPackages()
180
+ .then(packages => {
181
+ const monoWorkspace = { packages };
182
+ return monoWorkspace;
183
+ })
184
+ });
185
+ chain = chain.then((monoWorkspace) => {
186
+ const packages = monoWorkspace.packages;
187
+ monoWorkspace.workspaces = [];
188
+ monoWorkspace.projects = [];
189
+ packages.forEach(packageJson => {
190
+ const workspacePath = packageJson.location;
191
+ const angularJson = readAngularJson(workspacePath)
192
+ const workspace = readWorkspace(angularJson, packageJson);
193
+ monoWorkspace.workspaces.push(workspace);
194
+ workspace.projects.reduce((monoWorkspace, projectInfo) => {
195
+ monoWorkspace.projects.push(projectInfo);
196
+ return monoWorkspace;
197
+ }, monoWorkspace);
198
+ // .forEach((projectInfo) => monoWorkspace.projects.push(projectInfo));
199
+ });
200
+ return monoWorkspace;
201
+ });
202
+ chain = chain.then(monoWorkspace => {
203
+ const projects = monoWorkspace.projects;
204
+ const checkProjects = projects.map((projectInfo) => {
205
+ let checkPromise = checkProjectChanges(projectInfo.project.path);
206
+ let changedPromise = checkPromise.then(hasChanged => {
207
+ projectInfo.hasChanged = hasChanged;
208
+ return Promise.resolve(projectInfo);
209
+ });
210
+ return changedPromise;
211
+ });
212
+ return Promise.all(checkProjects)
213
+ .then(changedProjects => {
214
+ monoWorkspace.changedProjects = changedProjects;
215
+ return Promise.resolve(monoWorkspace);
216
+ });
217
+ });
218
+ chain = chain.then(monoWorkspace => {
219
+ const changedProjects = monoWorkspace.changedProjects;
220
+ const prereleasePromise = changedProjects.map(projectInfo => updateProjectPrereleaseVersion(projectInfo.project.path));
221
+ return Promise.all(prereleasePromise)
222
+ .then((updatedVersions) => {
223
+ commitChanges(updatedVersions, commitUrl);
224
+ return Promise.resolve(monoWorkspace);
225
+ });
226
+ });
227
+ chain = chain.then(monoWorkspace => {
228
+ const buildPromises = monoWorkspace.workspaces.map((workspace) => buildWorkspace(workspace));
229
+ return Promise.all(buildPromises)
230
+ .then(() => {
231
+ return Promise.resolve(monoWorkspace);
232
+ });
233
+ });
234
+ chain = chain.then(monoWorkspace => {
235
+ const changedProjects = monoWorkspace.changedProjects;
236
+ const publishPromises = changedProjects.map(projectInfo => publish(projectInfo.project.path));
237
+ Promise.all(publishPromises)
238
+ .then(() => {
239
+ console.log('published');
240
+ })
241
+ });
242
+ }
243
+
244
+ function readAngularJson(workspacePath) {
245
+ const angularJsonPath = path.normalize(`${workspacePath}/angular.json`);
246
+ return JSON.parse(fs.readFileSync(angularJsonPath, 'utf-8'));
247
+ }
248
+
249
+ function readWorkspace(angularJson, packageJson) {
250
+ const projects = angularJson.projects;
251
+ const workspacePath = packageJson.location;
252
+ const packageName = packageJson.name;
253
+ const workspace = {
254
+ packageName,
255
+ path: workspacePath,
256
+ projects: []
257
+ };
258
+ Object.keys(projects)
259
+ .reduce((workspace, projectName) => {
260
+ const projectInfo = readProjectInfo(projectName, projects[projectName], workspacePath);
261
+ if (projectInfo) {
262
+ // workspace.set(projectName, projectInfo);
263
+ workspace.projects.push(projectInfo);
264
+ }
265
+ return workspace; ßß
266
+ }, workspace);
267
+ return workspace;
268
+ }
269
+
270
+ function readProjectInfo(projectName, project, workspacePath) {
271
+ const projectPath = path.normalize(`${workspacePath}/${project.root}`);
272
+ const projectType = project.projectType;
273
+ if (projectType === 'library') {
274
+ const packageJsonPath = path.normalize(`${projectPath}/package.json`);
275
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
276
+ const packageName = packageJson.name;
277
+ const ngPackagePath = path.normalize(`${projectPath}/ng-package.json`);
278
+ const ngPackage = JSON.parse(fs.readFileSync(ngPackagePath, 'utf-8'));
279
+ const packagePath = path.normalize(`${projectPath}/${ngPackage.dest}`);
280
+ return {
281
+ project: {
282
+ name: projectName,
283
+ path: projectPath,
284
+ type: projectType
285
+ },
286
+ package: {
287
+ name: packageName,
288
+ path: packagePath
289
+ }
290
+ }
291
+ }
292
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farris/cli",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
4
4
  "description": "Farris command line interface",
5
5
  "main": "index.js",
6
6
  "scripts": {