1import { ExpoConfig } from '@expo/config';
2import JsonFile, { JSONObject } from '@expo/json-file';
3import path from 'path';
4
5import { CommandError } from '../utils/errors';
6
7type LocaleMap = Record<string, JSONObject>;
8
9// Similar to how we resolve locales in `@expo/config-plugins`
10export async function getResolvedLocalesAsync(
11  projectRoot: string,
12  exp: Pick<ExpoConfig, 'locales'>
13): Promise<LocaleMap> {
14  if (!exp.locales) {
15    return {};
16  }
17
18  const locales: LocaleMap = {};
19  for (const [lang, localeJsonPath] of Object.entries(exp.locales)) {
20    if (typeof localeJsonPath === 'string') {
21      try {
22        locales[lang] = await JsonFile.readAsync(path.join(projectRoot, localeJsonPath));
23      } catch (error: any) {
24        throw new CommandError('EXPO_CONFIG', JSON.stringify(error));
25      }
26    } else {
27      // In the off chance that someone defined the locales json in the config, pass it directly to the object.
28      // We do this to make the types more elegant.
29      locales[lang] = localeJsonPath;
30    }
31  }
32  return locales;
33}
34