forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.js
More file actions
285 lines (224 loc) · 7.94 KB
/
publish.js
File metadata and controls
285 lines (224 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'use strict';
let fs = require('fs');
let os = require('os');
let path = require('path');
let semver = require('semver');
let deleteFile = require('./utils').deleteFile;
let execSync = require('child_process').execSync;
let forEachPackage = require('./enumerate').forEachPackage;
let getAllProjects = require('./enumerate').getAllProjects;
let _downstreamDeps = getDownstreamDependencies();
let _allPackages = getAllProjects();
let _changeTypes = {
major: 3,
minor: 2,
patch: 1,
dependency: 0,
3: 'major',
2: 'minor',
1: 'patch',
0: 'dependency'
};
let argv = require('yargs').argv;
let _shouldCommit = argv.commit;
let _authToken = argv.npmAuthToken;
/* Find all changes and return parsed change definitions. */
function findChangesSync() {
let changeFiles = [];
let allChanges = {};
let changesPath = path.join(process.cwd(), 'changes');
console.log(`Finding changes in: ${changesPath}`);
try {
changeFiles = fs.readdirSync(changesPath).filter(filename => filename.indexOf('.json') >= 0);
} catch (e) { }
// Add the minimum changes defined by the change descriptions.
changeFiles.forEach((file) => {
let fullPath = path.resolve('./changes', file);
let changeDescription = JSON.parse(fs.readFileSync(fullPath, 'utf8'));
for (let i = 0; i < changeDescription.changes.length; i++) {
let change = changeDescription.changes[i];
addChange(allChanges, change);
}
});
let packages = Object.keys(allChanges);
let updatedDeps = {};
// Update orders so that downstreams are marked to come after upstreams.
for (let packageName in allChanges) {
let change = allChanges[packageName];
let pkg = _allPackages[packageName].package;
let deps = _downstreamDeps[packageName];
// Write the new version expected for the change.
change.newVersion = (change.changeType > _changeTypes.dependency) ? semver.inc(pkg.version, _changeTypes[change.changeType]) : pkg.version;
if (deps) {
for (let depName of deps) {
let depChange = allChanges[depName];
if (depChange) {
depChange.order = Math.max(change.order + 1, depChange.order);
}
}
}
}
return allChanges;
}
/** Add a change request to the allChanges dictionary if necessary. */
function addChange(allChanges, change) {
let packageName = change.packageName;
let pkgEntry = _allPackages[packageName];
if (!pkgEntry) {
throw `The package ${packageName} was requested for publishing but does not exist. Please fix change requests.`;
}
let pkg = pkgEntry.package;
let currentChange;
if (!allChanges[packageName]) {
currentChange = allChanges[packageName] = {
packageName,
packagePath: pkgEntry.path,
changeType: _changeTypes[change.type],
comments: change.comments || [],
order: 0,
changes: [change]
};
} else {
currentChange = allChanges[packageName];
currentChange.changeType = Math.max(currentChange.changeType, _changeTypes[change.type]);
currentChange.comments = currentChange.comments.concat(change.comments);
currentChange.changes.push(change);
}
currentChange.newVersion = currentChange.changeType > 0 ? semver.inc(pkg.version, _changeTypes[currentChange.changeType]) : pkg.version;
currentChange.newVersionRange = `>=${currentChange.newVersion} <${semver.inc(currentChange.newVersion, 'major')}`;
updateDownstreamDependencies(allChanges, packageName, currentChange.newVersionRange);
}
/** Build a downstream dependencies lookup table. */
function getDownstreamDependencies() {
let downstreamDeps = {};
forEachPackage('.', (pkg, location) => {
for (let depName in pkg.dependencies) {
if (!downstreamDeps[depName]) {
downstreamDeps[depName] = [];
}
downstreamDeps[depName].push(pkg.name);
}
});
return downstreamDeps;
}
function updateDownstreamDependencies(allChanges, packageName, newVersionRange) {
let change = allChanges[packageName];
let downstreamNames = _downstreamDeps[packageName];
// Iterate through all downstream dependencies for the package.
if (downstreamNames) {
for (let depName of downstreamNames) {
let pkgEntry = _allPackages[depName];
let pkg = pkgEntry.package;
let requiredVersion = pkgEntry.package.dependencies[packageName];
// If the version range has not yet been updated to this version, update it.
if (requiredVersion !== newVersionRange) {
pkgEntry.package.dependencies[packageName] = newVersionRange;
// Either it already satisfies the new version, or doesn't. If not, the downstream dep needs to be republished.
let changeType = semver.satisfies(change.newVersion, requiredVersion) ? _changeTypes.dependency : _changeTypes.patch;
addChange(allChanges, {
packageName: pkg.name,
type: _changeTypes[changeType],
comments: [`Updating ${packageName}: ${newVersionRange} (was ${ requiredVersion })`]
});
}
}
}
}
/** Update the package.json for a given change. */
function updatePackage(change, allChanges) {
console.log(os.EOL + `* Applying ${_changeTypes[change.changeType]} update for ${change.packageName} to ${change.newVersion}`);
let pkg = _allPackages[change.packageName].package;
pkg.version = change.newVersion;
change.changes.forEach(subChange => subChange.comments.forEach(comment => console.log( ` - [${subChange.type}] ${comment}`)));
if (_shouldCommit) {
fs.writeFileSync(change.packagePath, JSON.stringify(pkg, null, 2), 'utf8');
}
}
function execCommand(commandLine, workingPath, isDisabled) {
workingPath = workingPath || process.cwd();
console.log(`Executing: "${commandLine}" from ${workingPath}`);
if (_shouldCommit && !isDisabled) {
try {
execSync(commandLine, {
cwd: workingPath,
stdio: [0, 1, 2]
});
} catch(error) {
console.log('ERROR: ' + error.toString());
throw error;
}
}
}
function gitAddChanges() {
execCommand('git add .');
}
function gitAddTags(allChanges) {
for (let packageName in allChanges) {
let change = allChanges[packageName];
if (change.changeType > _changeTypes.dependency) {
let tagName = packageName + '_v' + change.newVersion;
execCommand(`git tag -a ${tagName} -m "${packageName} v${change.newVersion}"`);
}
}
}
function gitCommit() {
execCommand('git commit -m "Applying package updates."');
}
function gitRefresh() {
execCommand('git checkout master');
execCommand('git pull origin master');
}
function gitPush() {
execCommand('git push origin HEAD:master --follow-tags --verbose');
}
function publishPackage(change) {
let authParam = '';
if (_authToken) {
authParam = `--//registry.npmjs.org/:_authToken=${_authToken}`;
}
execCommand(`npm publish ${authParam}`, path.dirname(change.packagePath));
}
function deleteChangeFiles() {
let changesPath = path.join(process.cwd(), 'changes');
let changeFiles = [];
try {
changeFiles = fs.readdirSync(changesPath).filter(filename => filename.indexOf('.json') >= 0);
} catch (e) { }
if (changeFiles.length) {
console.log(os.EOL + `Deleting ${changeFiles.length} change file(s).`);
for (let fileName of changeFiles) {
let filePath = path.join(changesPath, fileName);
console.log(` - ${filePath}`);
if (_shouldCommit) {
deleteFile(filePath);
}
}
}
}
/** Apply set of changes. */
function applyChanges(allChanges) {
let orderedChanges = (
Object
.keys(allChanges)
.map(key => allChanges[key])
.sort((a, b) => a.order < b.order ? -1 : 1));
if (orderedChanges.length > 1) {
for (let change of orderedChanges) {
updatePackage(change, allChanges);
}
gitRefresh();
deleteChangeFiles();
gitAddChanges(allChanges);
gitCommit();
gitPush();
for (let change of orderedChanges) {
if (change.changeType > _changeTypes.dependency) {
publishPackage(change);
}
}
gitAddTags(allChanges);
gitPush();
}
}
let changes = findChangesSync();
applyChanges(changes);