-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathupdateVersion.js
More file actions
96 lines (87 loc) Β· 2.59 KB
/
updateVersion.js
File metadata and controls
96 lines (87 loc) Β· 2.59 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
/**
* 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
* @flow strict
* @oncall react_native
*/
'use strict';
const fs = require('fs');
const invariant = require('invariant');
const path = require('path');
function updateVersion(version /*: ?string */) {
if (version == null) {
throw new Error('Please specify a version in the form "1.2.3"');
}
if (!version.match(/^\d+\.\d+\.\d+$/)) {
throw new Error(`Invalid version number (e.g. "1.2.3"): ${version}`);
}
const metroDirPath = path.join(__dirname, '..');
const subPackageNameSet = new Set(
fs
.readdirSync(path.join(metroDirPath, 'packages'))
.filter(dir => !dir.startsWith('.')),
);
updateAllPackageManifests(metroDirPath, version, subPackageNameSet);
}
function updateAllPackageManifests(
metroDirPath /*: string */,
newVersion /*: string */,
subPackageNameSet /*: ReadonlySet<string> */,
) {
subPackageNameSet.forEach(pkgName => {
const subPackagePackPath = path.join(
metroDirPath,
'packages',
pkgName,
'package.json',
);
mutateManifestFile(subPackagePackPath, manifest => {
manifest.version = newVersion;
// update local cross deps with new version as well
[
'dependencies',
'devDependencies',
'peerDependencies',
'resolutions',
].forEach(key => {
const deps = manifest[key];
invariant(
deps == null || (typeof deps === 'object' && !Array.isArray(deps)),
`Unexpected type for ${subPackagePackPath}#${key}`,
);
updateCrossDepsInline(deps, subPackageNameSet, newVersion);
});
});
});
}
// given a dependency object (from package.json) update version for local pkgs
function updateCrossDepsInline(
allDeps /*: {[string]: unknown, ...} */, // json object
subDeps /*: ReadonlySet<string> */,
version /*: string */,
) {
if (allDeps) {
Object.keys(allDeps).forEach(name => {
if (subDeps.has(name)) {
allDeps[name] = version;
}
});
}
}
// update the package.json as JSON object
function mutateManifestFile(
filePath /*: string */,
mutator /*: (manifest: {
[string]: string | number | Array<unknown> | {[string]: unknown, ...},
}) => void */,
) {
const manifest = JSON.parse(fs.readFileSync(filePath, 'utf8'));
mutator(manifest);
fs.writeFileSync(filePath, JSON.stringify(manifest, null, 2) + '\n', 'utf8');
}
// Usage: node ./scripts/updateVersion 1.2.3
updateVersion(process.argv[2]);