|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + */ |
| 9 | + |
| 10 | +const path = require('path'); |
| 11 | +const {readdirSync, readFileSync} = require('fs'); |
| 12 | + |
| 13 | +const ROOT_LOCATION = path.join(__dirname, '..', '..'); |
| 14 | +const PACKAGES_LOCATION = path.join(ROOT_LOCATION, 'packages'); |
| 15 | + |
| 16 | +const DEFAULT_OPTIONS = {includeReactNative: false}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Function, which returns an array of all directories inside specified location |
| 20 | + * |
| 21 | + * @param {string} source Path to directory, where this should be executed |
| 22 | + * @returns {string[]} List of directories names |
| 23 | + */ |
| 24 | +const getDirectories = source => |
| 25 | + readdirSync(source, {withFileTypes: true}) |
| 26 | + .filter(file => file.isDirectory()) |
| 27 | + .map(directory => directory.name); |
| 28 | + |
| 29 | +/** |
| 30 | + * @callback forEachPackageCallback |
| 31 | + * @param {string} packageAbsolutePath |
| 32 | + * @param {string} packageRelativePathFromRoot |
| 33 | + * @param {Object} packageManifest |
| 34 | + */ |
| 35 | + |
| 36 | +/** |
| 37 | + * Iterate through every package inside /packages (ignoring react-native) and call provided callback for each of them |
| 38 | + * |
| 39 | + * @param {forEachPackageCallback} callback The callback which will be called for each package |
| 40 | + * @param {{includeReactNative: (boolean|undefined)}} [options={}] description |
| 41 | + */ |
| 42 | +const forEachPackage = (callback, options = DEFAULT_OPTIONS) => { |
| 43 | + const {includeReactNative} = options; |
| 44 | + |
| 45 | + // We filter react-native package on purpose, so that no CI's script will be executed for this package in future |
| 46 | + // Unless includeReactNative options is provided |
| 47 | + const packagesDirectories = getDirectories(PACKAGES_LOCATION).filter( |
| 48 | + directoryName => directoryName !== 'react-native' || includeReactNative, |
| 49 | + ); |
| 50 | + |
| 51 | + packagesDirectories.forEach(packageDirectory => { |
| 52 | + const packageAbsolutePath = path.join(PACKAGES_LOCATION, packageDirectory); |
| 53 | + const packageRelativePathFromRoot = path.join('packages', packageDirectory); |
| 54 | + |
| 55 | + const packageManifest = JSON.parse( |
| 56 | + readFileSync(path.join(packageAbsolutePath, 'package.json')), |
| 57 | + ); |
| 58 | + |
| 59 | + callback(packageAbsolutePath, packageRelativePathFromRoot, packageManifest); |
| 60 | + }); |
| 61 | +}; |
| 62 | + |
| 63 | +module.exports = forEachPackage; |
0 commit comments