mirror of
https://github.com/react/react-native.git
synced 2026-09-29 16:58:04 +08:00
Refactor remaining forEachPackage call sites (#43112)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43112 Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D53942028 fbshipit-source-id: 335bff3c3a31026bae7140fac1d1a6aae23a0f1e
This commit is contained in:
committed by
Facebook GitHub Bot
parent
4ecf57ead2
commit
d6bf51cad9
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* 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
|
||||
* @oncall react_native
|
||||
*/
|
||||
|
||||
const {REPO_ROOT} = require('../consts');
|
||||
const fs = require('fs');
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
|
||||
const WORKSPACES_CONFIG = 'packages/*';
|
||||
|
||||
/*::
|
||||
export type PackageJson = {
|
||||
name: string,
|
||||
version: string,
|
||||
private?: boolean,
|
||||
dependencies?: Record<string, string>,
|
||||
devDependencies?: Record<string, string>,
|
||||
...
|
||||
};
|
||||
|
||||
type PackagesFilter = $ReadOnly<{
|
||||
includeReactNative: boolean,
|
||||
includePrivate?: boolean,
|
||||
}>;
|
||||
|
||||
export type PackageInfo = {
|
||||
// The name of the package
|
||||
name: string,
|
||||
|
||||
// The absolute path to the package
|
||||
path: string,
|
||||
|
||||
// The parsed package.json contents
|
||||
packageJson: PackageJson,
|
||||
};
|
||||
|
||||
type ProjectInfo = {
|
||||
[packageName: string]: PackageInfo,
|
||||
};
|
||||
*/
|
||||
|
||||
/**
|
||||
* Locates monrepo packages and returns a mapping of package names to their
|
||||
* metadata. Considers Yarn workspaces under `packages/`.
|
||||
*/
|
||||
async function getPackages(
|
||||
filter /*: PackagesFilter */,
|
||||
) /*: Promise<ProjectInfo> */ {
|
||||
const {includeReactNative, includePrivate = 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(async packageJsonPath => {
|
||||
const packagePath = path.dirname(packageJsonPath);
|
||||
const packageJson /*: PackageJson */ = JSON.parse(
|
||||
await fs.promises.readFile(packageJsonPath, 'utf-8'),
|
||||
);
|
||||
|
||||
return [
|
||||
packageJson.name,
|
||||
{
|
||||
name: packageJson.name,
|
||||
path: packagePath,
|
||||
packageJson,
|
||||
},
|
||||
];
|
||||
}),
|
||||
);
|
||||
|
||||
return Object.fromEntries(
|
||||
packagesEntries.filter(
|
||||
([_, {packageJson}]) => packageJson.private !== true || includePrivate,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getPackages,
|
||||
};
|
||||
Reference in New Issue
Block a user