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