1import JsonFile from '@expo/json-file';
2import chalk from 'chalk';
3import path from 'path';
4
5import { EXPO_DIR } from '../../Constants';
6import logger from '../../Logger';
7import { Task } from '../../TasksRunner';
8import { Parcel, TaskArgs } from '../types';
9import { selectPackagesToPublish } from './selectPackagesToPublish';
10
11const { magenta, green, gray, cyan } = chalk;
12
13/**
14 * Updates `bundledNativeModules.json` file in `expo` package.
15 * It's used internally by some `expo-cli` commands so we know which package versions are compatible with `expo` version.
16 */
17export const updateBundledNativeModulesFile = new Task<TaskArgs>(
18  {
19    name: 'updateBundledNativeModulesFile',
20    dependsOn: [selectPackagesToPublish],
21    filesToStage: ['packages/expo/bundledNativeModules.json'],
22  },
23  async (parcels: Parcel[]) => {
24    const bundledNativeModulesPath = path.join(EXPO_DIR, 'packages/expo/bundledNativeModules.json');
25    const bundledNativeModules = await JsonFile.readAsync<Record<string, string>>(
26      bundledNativeModulesPath
27    );
28
29    logger.info(`\n✏️  Updating ${magenta.bold('bundledNativeModules.json')} file...`);
30
31    for (const { pkg, state } of parcels) {
32      const currentRange = bundledNativeModules[pkg.packageName];
33      const newRange = `~${state.releaseVersion}`;
34
35      if (!currentRange) {
36        logger.log('  ', green(pkg.packageName), gray('is not defined.'));
37        continue;
38      }
39
40      logger.log(
41        '  ',
42        green(pkg.packageName),
43        `${cyan.bold(currentRange)} -> ${cyan.bold(newRange)}`
44      );
45
46      bundledNativeModules[pkg.packageName] = newRange;
47    }
48
49    await JsonFile.writeAsync(bundledNativeModulesPath, bundledNativeModules);
50  }
51);
52