-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Expand file tree
/
Copy pathmonorepoUtils.js
More file actions
186 lines (159 loc) · 4.29 KB
/
Copy pathmonorepoUtils.js
File metadata and controls
186 lines (159 loc) · 4.29 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
/**
* 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.
*
* @flow strict
* @format
*/
const {REACT_NATIVE_PACKAGE_DIR, REPO_ROOT} = require('./consts');
const {promises: fs} = require('fs');
const glob = require('glob');
const path = require('path');
const WORKSPACES_CONFIG = '{packages,private}/*';
/*::
export type PackageJson = {
name: string,
version: string,
private?: boolean,
dependencies?: Record<string, string>,
devDependencies?: Record<string, string>,
main?: string,
...
};
type PackagesFilter = $ReadOnly<{
// Include the main react-native package
includeReactNative: boolean,
// Include packages marked with `private: true`
includePrivate?: boolean,
// Force include the rn-tester package. Special case of a private package
// that we version and depend on in fbsource.
forceIncludeRNTester?: boolean,
}>;
export type PackageInfo = {
// The name of the package
name: string,
// The absolute path to the package
path: string,
// The package version
version: string,
// The parsed package.json contents
packageJson: PackageJson,
};
export type ProjectInfo = {
[packageName: string]: PackageInfo,
};
*/
/**
* Locates monorepo packages and returns a mapping of package names to their
* metadata. Considers Yarn workspaces under `packages/` and `private/`.
*/
async function getPackages(
filter /*: PackagesFilter */,
) /*: Promise<ProjectInfo> */ {
const {
includeReactNative,
includePrivate = false,
forceIncludeRNTester = false,
} = filter;
const packagesEntries = await Promise.all(
glob
.sync(`${WORKSPACES_CONFIG}/package.json`, {
cwd: REPO_ROOT,
absolute: true,
ignore: includeReactNative
? []
: ['packages/react-native/package.json'],
})
.map(parsePackageInfo),
);
return Object.fromEntries(
packagesEntries.filter(([_, {name, packageJson}]) => {
if (name === '@react-native/tester') {
return forceIncludeRNTester;
}
return packageJson.private !== true || includePrivate;
}),
);
}
/**
* Get the parsed package metadata for the workspace root.
*/
async function getWorkspaceRoot() /*: Promise<PackageInfo> */ {
const [, packageInfo] = await parsePackageInfo(
path.join(REPO_ROOT, 'package.json'),
);
return packageInfo;
}
/**
* Get the parsed package metadata for the main react-native package.
*/
async function getReactNativePackage() /*: Promise<PackageInfo> */ {
const [, packageInfo] = await parsePackageInfo(
path.join(REACT_NATIVE_PACKAGE_DIR, 'package.json'),
);
return packageInfo;
}
async function parsePackageInfo(
packageJsonPath /*: string */,
) /*: Promise<[string, PackageInfo]> */ {
const packagePath = path.dirname(packageJsonPath);
const packageJson /*: PackageJson */ = JSON.parse(
await fs.readFile(packageJsonPath, 'utf-8'),
);
return [
packageJson.name,
{
name: packageJson.name,
path: packagePath,
version: packageJson.version,
packageJson,
},
];
}
/**
* Update a given package with the package versions.
*/
async function updatePackageJson(
{path: packagePath, packageJson} /*: PackageInfo */,
newPackageVersions /*: $ReadOnly<{[string]: string}> */,
) /*: Promise<void> */ {
const packageName = packageJson.name;
if (packageName in newPackageVersions) {
packageJson.version = newPackageVersions[packageName];
}
for (const dependencyField of [
'dependencies',
'devDependencies',
] /*:: as const */) {
const deps = packageJson[dependencyField];
if (deps == null) {
continue;
}
for (const dependency in newPackageVersions) {
if (dependency in deps) {
deps[dependency] = newPackageVersions[dependency];
}
}
}
return writePackageJson(path.join(packagePath, 'package.json'), packageJson);
}
/**
* Write a `package.json` file to disk.
*/
async function writePackageJson(
packageJsonPath /*: string */,
packageJson /*: PackageJson */,
) /*: Promise<void> */ {
return fs.writeFile(
packageJsonPath,
JSON.stringify(packageJson, null, 2) + '\n',
);
}
module.exports = {
getPackages,
getWorkspaceRoot,
updatePackageJson,
getReactNativePackage,
};