Files
react-native/scripts/monorepo/for-each-package.js
T
Alex Hunt 49c3c3412a Refactor package discovery in publish script (#42938)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/42938

Substitutes the `forEachPackage` util with a replacement async `getPackages` function. This will be used further in the next diff.

The new function aims to be more erganomic/versatile than `forEachPackage` by returning a package mapping (see updated test mock). The API aligns roughly with `yarn workspaces list` and [Lerna's `detectProjects`](https://lerna.js.org/docs/api-reference/utilities#detectprojects).

This also aligns with / replaces similar attempts in our existing scripts:
- [`getPackagesToPublish`](https://github.com/facebook/react-native/blob/2ca7bec0c2a7d821ceaaf39840a6cdc5eceb8678/scripts/monorepo/get-and-update-packages.js#L56)
- [`getPublicPackages`](https://github.com/facebook/react-native/blob/2ca7bec0c2a7d821ceaaf39840a6cdc5eceb8678/scripts/releases/set-version/index.js#L19)

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D53607806

fbshipit-source-id: 00ec34edadab863dc8f2f0c7852f6e835a5dddf5
2024-02-12 04:42:27 -08:00

72 lines
2.2 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-local
* @format
*/
const {readdirSync, readFileSync} = require('fs');
const path = require('path');
const REPO_ROOT = path.join(path.dirname(__filename), '..', '..');
const PACKAGES_LOCATION = path.join(REPO_ROOT, 'packages');
const DEFAULT_OPTIONS /*: Options */ = {includeReactNative: false};
/*::
type PackageJSON = {
name: string,
private?: ?boolean,
version: string,
dependencies: {[string]: string},
devDependencies: {[string]: string},
...
};
type Options = {
includeReactNative?: ?boolean,
};
*/
/**
* Function, which returns an array of all directories inside specified location
*/
const getDirectories = (source /*: string */) /*: Array<string> */ =>
readdirSync(source, {withFileTypes: true})
.filter(file => file.isDirectory())
.map(directory => directory.name.toString());
/**
* Iterate through every package inside /packages (ignoring react-native) and call provided callback for each of them
*
* @deprecated Use scripts/releases/utils/monorepo.js#getPackages instead
*/
const forEachPackage = (
callback /*: (string, string, PackageJSON) => void */,
options /*: Options */ = DEFAULT_OPTIONS,
) => {
const {includeReactNative} = options;
// We filter react-native package on purpose, so that no CI's script will be executed for this package in future
// Unless includeReactNative options is provided
const packagesDirectories = getDirectories(PACKAGES_LOCATION).filter(
directoryName =>
directoryName !== 'react-native' || includeReactNative === true,
);
packagesDirectories.forEach(packageDirectory => {
const packageAbsolutePath = path.join(PACKAGES_LOCATION, packageDirectory);
const packageRelativePathFromRoot = path.join('packages', packageDirectory);
const packageManifest = JSON.parse(
readFileSync(path.join(packageAbsolutePath, 'package.json')).toString(),
);
callback(packageAbsolutePath, packageRelativePathFromRoot, packageManifest);
});
};
module.exports = forEachPackage;