-
Notifications
You must be signed in to change notification settings - Fork 24.6k
/
Copy pathfind-and-publish-all-bumped-packages.js
96 lines (78 loc) · 2.47 KB
/
find-and-publish-all-bumped-packages.js
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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
const path = require('path');
const chalk = require('chalk');
const {exec} = require('shelljs');
const forEachPackage = require('./for-each-package');
const ROOT_LOCATION = path.join(__dirname, '..', '..');
const NPM_CONFIG_OTP = process.env.NPM_CONFIG_OTP;
const findAndPublishAllBumpedPackages = () => {
console.log('Traversing all packages inside /packages...');
forEachPackage(
(packageAbsolutePath, packageRelativePathFromRoot, packageManifest) => {
if (packageManifest.private) {
console.log(
`\u23ED Skipping private package ${chalk.dim(packageManifest.name)}`,
);
return;
}
const diff = exec(
`git log -p --format="" HEAD~1..HEAD ${packageRelativePathFromRoot}/package.json`,
{cwd: ROOT_LOCATION, silent: true},
).stdout;
const previousVersionPatternMatches = diff.match(
/- {2}"version": "([0-9]+.[0-9]+.[0-9]+)"/,
);
if (!previousVersionPatternMatches) {
console.log(
`\uD83D\uDD0E No version bump for ${chalk.green(
packageManifest.name,
)}`,
);
return;
}
const [, previousVersion] = previousVersionPatternMatches;
const nextVersion = packageManifest.version;
console.log(
`\uD83D\uDCA1 ${chalk.yellow(
packageManifest.name,
)} was updated: ${chalk.red(previousVersion)} -> ${chalk.green(
nextVersion,
)}`,
);
if (!nextVersion.startsWith('0.')) {
throw new Error(
`Package version expected to be 0.x.y, but received ${nextVersion}`,
);
}
const npmOTPFlag = NPM_CONFIG_OTP ? `--otp ${NPM_CONFIG_OTP}` : '';
const {code, stderr} = exec(`npm publish ${npmOTPFlag}`, {
cwd: packageAbsolutePath,
silent: true,
});
if (code) {
console.log(
chalk.red(
`\u274c Failed to publish version ${nextVersion} of ${packageManifest.name}. Stderr:`,
),
);
console.log(stderr);
process.exit(1);
} else {
console.log(
`\u2705 Successfully published new version of ${chalk.green(
packageManifest.name,
)}`,
);
}
},
);
process.exit(0);
};
findAndPublishAllBumpedPackages();