1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const json_file_1 = __importDefault(require("@expo/json-file"));
|
7 | const child_process_1 = require("child_process");
|
8 | const jest_message_util_1 = require("jest-message-util");
|
9 | const Errors_1 = require("./Errors");
|
10 | const Modules_1 = require("./Modules");
|
11 | const Serialize_1 = require("./Serialize");
|
12 | function isMissingFileCode(code) {
|
13 | return ['ENOENT', 'MODULE_NOT_FOUND', 'ENOTDIR'].includes(code);
|
14 | }
|
15 | function readConfigFile(configFilePath, context) {
|
16 | if (!Modules_1.fileExists(configFilePath))
|
17 | return null;
|
18 | try {
|
19 | return evalConfig(configFilePath, context);
|
20 | }
|
21 | catch (error) {
|
22 |
|
23 | if (!isMissingFileCode(error.code)) {
|
24 | throw error;
|
25 | }
|
26 | }
|
27 | return null;
|
28 | }
|
29 | function 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 | }
|
36 | exports.getDynamicConfig = getDynamicConfig;
|
37 | function 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 | }
|
44 | exports.getStaticConfig = getStaticConfig;
|
45 |
|
46 |
|
47 | function 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 |
|
59 | const lastLog = logs.pop();
|
60 | for (const log of logs) {
|
61 |
|
62 | console.log(log);
|
63 | }
|
64 |
|
65 | const results = JSON.parse(lastLog);
|
66 | return results;
|
67 | }
|
68 | else {
|
69 |
|
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 |
|
\ | No newline at end of file |