UNPKG

3.54 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const json_file_1 = __importDefault(require("@expo/json-file"));
7const child_process_1 = require("child_process");
8const jest_message_util_1 = require("jest-message-util");
9const Errors_1 = require("./Errors");
10const Modules_1 = require("./Modules");
11const Serialize_1 = require("./Serialize");
12function isMissingFileCode(code) {
13 return ['ENOENT', 'MODULE_NOT_FOUND', 'ENOTDIR'].includes(code);
14}
15function readConfigFile(configFilePath, context) {
16 if (!Modules_1.fileExists(configFilePath))
17 return null;
18 try {
19 return evalConfig(configFilePath, context);
20 }
21 catch (error) {
22 // If the file doesn't exist then we should skip it and continue searching.
23 if (!isMissingFileCode(error.code)) {
24 throw error;
25 }
26 }
27 return null;
28}
29function getDynamicConfig(configPath, request) {
30 const config = readConfigFile(configPath, request);
31 if (config) {
32 return Serialize_1.serializeAndEvaluate(config);
33 }
34 throw new Errors_1.ConfigError(`Failed to read config at: ${configPath}`, 'INVALID_CONFIG');
35}
36exports.getDynamicConfig = getDynamicConfig;
37function getStaticConfig(configPath) {
38 const config = json_file_1.default.read(configPath, { json5: true });
39 if (config) {
40 return Serialize_1.serializeAndEvaluate(config);
41 }
42 throw new Errors_1.ConfigError(`Failed to read config at: ${configPath}`, 'INVALID_CONFIG');
43}
44exports.getStaticConfig = getStaticConfig;
45// We cannot use async config resolution right now because Next.js doesn't support async configs.
46// If they don't add support for async Webpack configs then we may need to pull support for Next.js.
47function evalConfig(configFile, request) {
48 try {
49 const spawnResults = child_process_1.spawnSync('node', [
50 require.resolve('@expo/config/build/scripts/read-config.js'),
51 '--colors',
52 configFile,
53 JSON.stringify(Object.assign(Object.assign({}, request), { config: Serialize_1.serializeAndEvaluate(request.config) })),
54 ], {});
55 if (spawnResults.status === 0) {
56 const spawnResultString = spawnResults.stdout.toString('utf8').trim();
57 const logs = spawnResultString.split('\n');
58 // Get the last console log to prevent parsing anything logged in the config.
59 const lastLog = logs.pop();
60 for (const log of logs) {
61 // Log out the logs from the config
62 console.log(log);
63 }
64 // Parse the final log of the script, it's the serialized config and exported object type.
65 const results = JSON.parse(lastLog);
66 return results;
67 }
68 else {
69 // Parse the error data and throw it as expected
70 const errorData = JSON.parse(spawnResults.stderr.toString('utf8'));
71 throw Errors_1.errorFromJSON(errorData);
72 }
73 }
74 catch (error) {
75 if (isMissingFileCode(error.code) || !(error instanceof SyntaxError)) {
76 throw error;
77 }
78 const message = jest_message_util_1.formatExecError(error, { rootDir: request.projectRoot, testMatch: [] }, { noStackTrace: true }, undefined, true);
79 throw new Errors_1.ConfigError(`\n${message}`, 'INVALID_CONFIG');
80 }
81}
82//# sourceMappingURL=getConfig.js.map
\No newline at end of file