@farris/cli 1.0.8 → 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 +175 -15
  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(
@@ -58,6 +59,18 @@ cli
58
59
  });
59
60
  }
60
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
+ )
61
74
  .argv;
62
75
 
63
76
  /**
@@ -77,7 +90,7 @@ function checkProjectChanges(projectPath) {
77
90
  if (hasChanges) {
78
91
  console.log(`${projectPath} has changed.`)
79
92
  } else {
80
- 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`)
81
94
  }
82
95
  // 返回检测结果
83
96
  return hasChanges;
@@ -99,21 +112,49 @@ function updateProjectPrereleaseVersion(projectPath, projectUrl) {
99
112
  const packageConfig = JSON.parse(fs.readFileSync(packageJsonFilePath, 'utf-8'));
100
113
  // 提取更新后的版本
101
114
  const updatedVersion = returnValue.stdout;
102
- // 向git缓冲区中添加变更
103
- childProcess.execSync('git', ['add', '.']);
104
- // 提交变更记录
105
- childProcess.execSync('git', ['commit', '-m', `update ${packageConfig.name} prerelease version to ${updatedVersion}. [skip ci]`]);
106
115
  console.log(`update ${packageConfig.name} prerelease version to ${updatedVersion}`);
116
+ const updateResult = {};
117
+ updateResult[packageConfig.name] = updatedVersion;
118
+ return updateResult;
119
+ })
120
+ }
107
121
 
108
- const branch = childProcess.execSync("git", ["rev-parse", "--abbrev-ref", "HEAD"]);
109
- console.log(`executing git push ${branch}`);
110
- // 提交变更集
111
- if (projectUrl) {
112
- return childProcess.exec("git", ["push", '-o', 'ci.skip', projectUrl]);
113
- } else {
114
- return childProcess.exec('git', ['push', '-o', 'ci.skip']);
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 + ', ';
115
130
  }
116
- })
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
117
158
  }
118
159
 
119
160
  /**
@@ -130,3 +171,122 @@ function publish(projectPath) {
130
171
  // 调用 npm publish 发布指定路径下的npm包
131
172
  return childProcess.exec('npm', ['publish', packagePath])
132
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.8",
3
+ "version": "1.0.9",
4
4
  "description": "Farris command line interface",
5
5
  "main": "index.js",
6
6
  "scripts": {