Files
react-native/scripts/utils/monorepo.js
T
Alex HuntandFacebook GitHub Bot d6bf51cad9 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
2024-02-20 09:55:46 -08:00

95 lines
2.1 KiB
JavaScript

/**
* 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,
};