-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathpublish-npm.ts
executable file
·424 lines (369 loc) · 13.7 KB
/
publish-npm.ts
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env node
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
/*
* This script publish to npm for all the TensorFlow.js packages. Before you run
* this script, run `yarn release` and commit the PRs.
* Then run this script as `yarn publish-npm`.
*/
import * as argparse from 'argparse';
import chalk from 'chalk';
import * as shell from 'shelljs';
import { RELEASE_UNITS, question, $, getReleaseBranch, checkoutReleaseBranch, ALPHA_RELEASE_UNIT, TFJS_RELEASE_UNIT, selectPackages, getLocalVersion, getNpmVersion, memoize, printReleaseUnit, checkPublishable, runVerdaccio, ReleaseUnit, getVersion, getTagFromVersion, filterPackages, ALL_PACKAGES, WEBSITE_RELEASE_UNIT, getPackages } from './release-util';
import semverCompare from 'semver/functions/compare';
import * as child_process from 'child_process';
import {BAZEL_PACKAGES} from './bazel_packages';
const TMP_DIR = '/tmp/tfjs-publish';
const VERDACCIO_REGISTRY = 'http://127.0.0.1:4873';
const NPM_REGISTRY = 'https://registry.npmjs.org/';
// This script can not publish the tfjs website
const PUBLISHABLE_RELEASE_UNITS = RELEASE_UNITS.filter(r => r !== WEBSITE_RELEASE_UNIT);
async function retry<T>(f: () => T, tries = 3, sleep=5_000): Promise<T> {
let lastError;
for (let i = 0; i < tries; i++) {
try {
return f();
} catch (e) {
lastError = e;
console.warn(e);
if (i + 1 < tries) {
// Only delay if the loop will run again.
await delay(sleep);
}
}
}
throw lastError;
}
/**
* For sets `a` and `b`, compute the set difference `a \ b`
*
* The set difference of `a` and `b`, denoted `a \ b`, is the set containing all
* elements of `a` that are not in `b`
*
* @param a The set to subtract from
* @param b The set to remove from `a` when creating the output set
*/
function setDifference<T>(a: Set<T>, b: Set<T>): Set<T> {
const difference = new Set<T>();
for (const val of a) {
if (!b.has(val)) {
difference.add(val);
}
}
return difference;
}
const parser = new argparse.ArgumentParser();
parser.addArgument('--git-protocol', {
action: 'storeTrue',
help: 'Use the git protocol rather than the http protocol when cloning repos.'
});
parser.addArgument('--registry', {
type: 'string',
defaultValue: NPM_REGISTRY,
help: 'Which registry to install packages from and publish to.',
});
parser.addArgument('--no-otp', {
action: 'storeTrue',
help: 'Do not use an OTP when publishing to the registry.',
});
parser.addArgument(['--release-this-branch', '--release-current-branch'], {
action: 'storeTrue',
help: 'Release the current branch instead of checking out a new one.',
});
parser.addArgument(['--dry'], {
action: 'storeTrue',
help: 'Dry run. Stage all packages in verdaccio but do not publish them to '
+ 'the registry.',
});
parser.addArgument(['--auto-publish-local-newer'], {
action: 'storeTrue',
help: 'Automatically publish local packages that have newer versions than'
+ ' the packages in the registry',
});
parser.addArgument(['--ci'], {
action: 'storeTrue',
help: 'Enable CI bazel flags for faster compilation and don\'t ask for user '
+ 'input before closing the verdaccio server once tests are done. '
+ 'Has no effect on results.',
});
parser.addArgument(['packages'], {
type: 'string',
nargs: '*',
help: 'Packages to publish. Leave empty to select interactively',
});
function delay(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function publish(pkg: string, registry: string, otp?: string,
build = true) {
const registryEnv = {
YARN_REGISTRY: registry,
NPM_CONFIG_REGISTRY: registry,
NPM_REGISTRY: registry, // For npx npm-cli-login
};
function run(command: string) {
return $(command, registryEnv);
}
function yarn(args: string) {
return run(`yarn --registry '${registry}' ${args}`);
}
const startDir = process.cwd();
try {
// Use a try block so cwd can be restored in 'finally' if an error occurs.
shell.cd(pkg);
checkPublishable('./package.json');
if (build && !BAZEL_PACKAGES.has(pkg)) {
console.log(chalk.magenta.bold(`~~~ Preparing package ${pkg}~~~`));
console.log(chalk.magenta('~~~ Installing packages ~~~'));
// Without a delay, this sometimes has issues downloading dependencies.
await delay(5_000);
// tfjs-node-gpu needs to get some files from tfjs-node.
if (pkg === 'tfjs-node-gpu') {
yarn('prep-gpu');
}
// Yarn above the other checks to make sure yarn doesn't change the lock
// file.
await retry(() =>
console.log(run(`yarn --registry '${registry}'`)));
console.log(chalk.magenta('~~~ Build npm ~~~'));
if (pkg === 'tfjs-react-native') {
yarn('build-npm');
} else {
yarn('build-npm for-publish');
}
}
// Used for nightly dev releases.
const version = getVersion('package.json');
const tag = getTagFromVersion(version);
let otpFlag = '';
if (otp) {
otpFlag = `--otp=${otp} `;
}
console.log(
chalk.magenta.bold(`~~~ Publishing ${pkg} to ${registry} with tag `
+ `${tag} ~~~`));
let login = '';
if (registry === VERDACCIO_REGISTRY) {
// If publishing to verdaccio, we must log in before every command.
login = 'npx npm-cli-login -u user -p password -e [email protected] && ';
}
if (BAZEL_PACKAGES.has(pkg)) {
let dashes = '-- --';
if (pkg === 'tfjs-backend-webgpu') {
// Special case for webgpu, which has an additional call to `yarn`
// in publish-npm.
dashes = '-- -- --';
}
await retry(() =>
run(`${login}yarn --registry '${registry}' publish-npm ${dashes} ${otpFlag} --tag=${tag} --force`));
} else {
// Special case for tfjs-node(-gpu), which must upload the node addon
// to GCP as well. Only do this when publishing to NPM.
if (registry === NPM_REGISTRY && pkg.startsWith('tfjs-node')) {
$('yarn build-and-upload-addon publish');
}
// Publish the package to the registry.
await retry(() =>
run(`${login}npm --registry '${registry}' publish --tag=${tag} ${otpFlag}`));
}
console.log(`Published ${pkg} to ${registry}.`);
} finally {
shell.cd(startDir);
}
}
async function main() {
const args = parser.parseArgs();
const killVerdaccio = await runVerdaccio();
let releaseUnits: ReleaseUnit[];
if (args.release_this_branch) {
console.log('Releasing current branch');
releaseUnits = PUBLISHABLE_RELEASE_UNITS;
} else {
PUBLISHABLE_RELEASE_UNITS.forEach(printReleaseUnit);
console.log();
const releaseUnitStr =
await question('Which release unit (leave empty for 0): ');
const releaseUnitInt = Number(releaseUnitStr);
if (releaseUnitInt < 0 || releaseUnitInt >= PUBLISHABLE_RELEASE_UNITS.length) {
console.log(chalk.red(`Invalid release unit: ${releaseUnitStr}`));
process.exit(1);
}
console.log(chalk.blue(`Using release unit ${releaseUnitInt}`));
console.log();
const releaseUnit = PUBLISHABLE_RELEASE_UNITS[releaseUnitInt];
const {name, } = releaseUnit;
let releaseBranch: string;
if (releaseUnit === ALPHA_RELEASE_UNIT) {
// Alpha release unit is published with the tfjs release unit.
releaseBranch = await getReleaseBranch(TFJS_RELEASE_UNIT.name);
} else {
releaseBranch = await getReleaseBranch(name);
}
console.log();
releaseUnits = [releaseUnit];
checkoutReleaseBranch(releaseBranch, args.git_protocol, TMP_DIR);
shell.cd(TMP_DIR);
}
const getNpmVersionMemoized = memoize((pkg: string) => {
const version = getLocalVersion(pkg);
const tag = getTagFromVersion(version);
return getNpmVersion(pkg, args.registry, tag);
});
async function getVersions(pkg: string) {
const localVersion = getLocalVersion(pkg);
const npmVersion = await getNpmVersionMemoized(pkg);
let localIsNewer = true;
if (npmVersion !== '') {
// Unpublished tags return '' for their version.
localIsNewer = semverCompare(localVersion, npmVersion) > 0;
}
return {localVersion, npmVersion, localIsNewer};
}
async function packageSelected(pkg: string) {
// Automatically select local packages with version numbers greater than
// npm.
try {
const {localVersion, localIsNewer} = await getVersions(pkg);
return localVersion !== '0.0.0' && localIsNewer;
} catch (e) {
console.warn(e);
return false;
}
}
// Get the list of packages to build and publish.
// There are three ways packages can be selected.
// 1. By passing them as CLI arguments in `packages`.
// 2. Automatically based on the versions on npm.
// 3. Interactively on the command line.
let packages: string[];
if (args.packages.length > 0) {
// Get packages to publish from the 'packages' arg
// Filter from the set of all packages to make sure they end up
// in topological order.
const allPackages = getPackages(PUBLISHABLE_RELEASE_UNITS);
const requestedPackages = new Set(args.packages);
packages = allPackages.filter(pkg => requestedPackages.has(pkg));
// Check if there are any unsupported packages requested by the user
const unsupportedPackages = setDifference(requestedPackages,
new Set(packages));
if (unsupportedPackages.size > 0) {
throw new Error(`Can not publish ${[...unsupportedPackages]}. `
+ `Supported packages are:\n${[...ALL_PACKAGES].join('\n')}`);
}
} else if (args.auto_publish_local_newer) {
// Automatically select packages based on npm versions
packages = await filterPackages(packageSelected, PUBLISHABLE_RELEASE_UNITS);
console.log(`Publishing ${packages}`);
} else {
// Select packages interactively
packages = await selectPackages({
message: 'Select packages to publish',
releaseUnits,
selected: packageSelected,
async modifyName(pkg) {
// Add the local and remote versions to the printed name.
try {
const {localVersion, npmVersion, localIsNewer} = await getVersions(pkg);
const pkgWithVersion =
`${pkg.padEnd(20)} (${npmVersion ?? 'unpublished'} → ${localVersion})`;
if (localIsNewer) {
return chalk.bold(pkgWithVersion);
} else {
return pkgWithVersion;
}
} catch (e) {
console.warn(e);
return pkg;
}
}
});
}
// Yarn in the top-level to download Bazel
$('yarn');
console.log();
// Pre-build all the bazel packages in a single bazel command for better
// efficiency.
const bazelTargets = packages.filter(pkg => BAZEL_PACKAGES.has(pkg))
.map(name => `//${name}:${name}_pkg`);
const bazelArgs = ['bazel', 'build']
if (args.ci) {
bazelArgs.push('--config=ci');
}
// Use child_process.spawnSync to show bazel build progress.
const result = child_process.spawnSync('yarn',
[...bazelArgs, ...bazelTargets],
{stdio:'inherit'});
if (result.status !== 0) {
throw new Error(`Bazel process failed with exit code ${result.status}`);
}
// Build and publish all packages to a local Verdaccio repo for staging.
console.log(
chalk.magenta.bold('~~~ Staging packages locally in Verdaccio ~~~'));
try {
for (const pkg of packages) {
await publish(pkg, VERDACCIO_REGISTRY);
}
} catch (e) {
// Make sure to kill the verdaccio server before exiting even if publish
// throws an error. Otherwise, it blocks the port for the next run.
killVerdaccio();
throw e;
}
if (args.dry) {
console.log('Not publishing packages due to \'--dry\'');
if (!args.ci) {
await question('Press enter to quit verdaccio.');
}
killVerdaccio();
} else {
// Publish all built packages to the selected registry
let otp = '';
if (!args.no_otp) {
otp = await question(`Enter one-time password from your authenticator: `);
}
console.log(`Publishing packages to ${args.registry}`);
killVerdaccio();
const toPublish = [...packages];
while (toPublish.length > 0) {
// Using a while loop instead of .map since a stale OTP will require
// a retry.
let pkg = toPublish[0];
if (args.no_otp) {
await publish(pkg, args.registry, '', false);
toPublish.shift(); // Remove the published package from 'toPublish'.
continue;
}
try {
await publish(pkg, args.registry, otp, false)
toPublish.shift(); // Remove the published package from 'toPublish'.
} catch (err) {
if ((err as Error).message.includes('code EOTP')) {
// Try again with a new otp
otp = await question(`OTP ${otp} failed. Enter a new one-time `
+ `password from your authenticator: `);
continue; // Don't shift the package since it failed to publish.
}
throw err;
}
}
console.log(`Published packages to ${args.registry}`);
}
process.exit(0);
}
main();