1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.getLocales = getLocales;
7exports.getResolvedLocalesAsync = getResolvedLocalesAsync;
8exports.setLocalesAsync = setLocalesAsync;
9exports.withLocales = void 0;
10
11function _jsonFile() {
12  const data = _interopRequireDefault(require("@expo/json-file"));
13
14  _jsonFile = function () {
15    return data;
16  };
17
18  return data;
19}
20
21function _fs() {
22  const data = _interopRequireDefault(require("fs"));
23
24  _fs = function () {
25    return data;
26  };
27
28  return data;
29}
30
31function _path() {
32  const data = require("path");
33
34  _path = function () {
35    return data;
36  };
37
38  return data;
39}
40
41function _iosPlugins() {
42  const data = require("../plugins/ios-plugins");
43
44  _iosPlugins = function () {
45    return data;
46  };
47
48  return data;
49}
50
51function _warnings() {
52  const data = require("../utils/warnings");
53
54  _warnings = function () {
55    return data;
56  };
57
58  return data;
59}
60
61function _Xcodeproj() {
62  const data = require("./utils/Xcodeproj");
63
64  _Xcodeproj = function () {
65    return data;
66  };
67
68  return data;
69}
70
71function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
72
73const withLocales = config => {
74  return (0, _iosPlugins().withXcodeProject)(config, async config => {
75    config.modResults = await setLocalesAsync(config, {
76      projectRoot: config.modRequest.projectRoot,
77      project: config.modResults
78    });
79    return config;
80  });
81};
82
83exports.withLocales = withLocales;
84
85function getLocales(config) {
86  var _config$locales;
87
88  return (_config$locales = config.locales) !== null && _config$locales !== void 0 ? _config$locales : null;
89}
90
91async function setLocalesAsync(config, {
92  projectRoot,
93  project
94}) {
95  const locales = getLocales(config);
96
97  if (!locales) {
98    return project;
99  } // possibly validate CFBundleAllowMixedLocalizations is enabled
100
101
102  const localesMap = await getResolvedLocalesAsync(projectRoot, locales);
103  const projectName = (0, _Xcodeproj().getProjectName)(projectRoot);
104  const supportingDirectory = (0, _path().join)(projectRoot, 'ios', projectName, 'Supporting'); // TODO: Should we delete all before running? Revisit after we land on a lock file.
105
106  const stringName = 'InfoPlist.strings';
107
108  for (const [lang, localizationObj] of Object.entries(localesMap)) {
109    const dir = (0, _path().join)(supportingDirectory, `${lang}.lproj`); // await fs.ensureDir(dir);
110
111    await _fs().default.promises.mkdir(dir, {
112      recursive: true
113    });
114    const strings = (0, _path().join)(dir, stringName);
115    const buffer = [];
116
117    for (const [plistKey, localVersion] of Object.entries(localizationObj)) {
118      buffer.push(`${plistKey} = "${localVersion}";`);
119    } // Write the file to the file system.
120
121
122    await _fs().default.promises.writeFile(strings, buffer.join('\n'));
123    const groupName = `${projectName}/Supporting/${lang}.lproj`; // deep find the correct folder
124
125    const group = (0, _Xcodeproj().ensureGroupRecursively)(project, groupName); // Ensure the file doesn't already exist
126
127    if (!(group !== null && group !== void 0 && group.children.some(({
128      comment
129    }) => comment === stringName))) {
130      // Only write the file if it doesn't already exist.
131      project = (0, _Xcodeproj().addResourceFileToGroup)({
132        filepath: (0, _path().relative)(supportingDirectory, strings),
133        groupName,
134        project,
135        isBuildFile: true,
136        verbose: true
137      });
138    }
139  }
140
141  return project;
142}
143
144async function getResolvedLocalesAsync(projectRoot, input) {
145  const locales = {};
146
147  for (const [lang, localeJsonPath] of Object.entries(input)) {
148    if (typeof localeJsonPath === 'string') {
149      try {
150        locales[lang] = await _jsonFile().default.readAsync((0, _path().join)(projectRoot, localeJsonPath));
151      } catch {
152        // Add a warning when a json file cannot be parsed.
153        (0, _warnings().addWarningIOS)(`locales.${lang}`, `Failed to parse JSON of locale file for language: ${lang}`, 'https://docs.expo.dev/distribution/app-stores/#localizing-your-ios-app');
154      }
155    } else {
156      // In the off chance that someone defined the locales json in the config, pass it directly to the object.
157      // We do this to make the types more elegant.
158      locales[lang] = localeJsonPath;
159    }
160  }
161
162  return locales;
163}
164//# sourceMappingURL=Locales.js.map