1"use strict";
2/**
3 * A helper script to load the Expo config and loaded plugins from a project
4 */
5var __importDefault = (this && this.__importDefault) || function (mod) {
6    return (mod && mod.__esModule) ? mod : { "default": mod };
7};
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.getExpoConfigLoaderPath = void 0;
10const promises_1 = __importDefault(require("fs/promises"));
11const module_1 = __importDefault(require("module"));
12const path_1 = __importDefault(require("path"));
13const resolve_from_1 = __importDefault(require("resolve-from"));
14const Options_1 = require("../Options");
15const Path_1 = require("../utils/Path");
16async function runAsync(programName, args = []) {
17    if (args.length < 1) {
18        console.log(`Usage: ${programName} <projectRoot> [ignoredFile]`);
19        return;
20    }
21    const projectRoot = path_1.default.resolve(args[0]);
22    const ignoredFile = args[1] ? path_1.default.resolve(args[1]) : null;
23    // @ts-expect-error: module internal _cache
24    const loadedModulesBefore = new Set(Object.keys(module_1.default._cache));
25    const { getConfig } = require((0, resolve_from_1.default)(path_1.default.resolve(projectRoot), 'expo/config'));
26    const config = await getConfig(projectRoot, { skipSDKVersionRequirement: true });
27    // @ts-expect-error: module internal _cache
28    const loadedModules = Object.keys(module_1.default._cache)
29        .filter((modulePath) => !loadedModulesBefore.has(modulePath))
30        .map((modulePath) => path_1.default.relative(projectRoot, modulePath));
31    const ignoredPaths = await loadIgnoredPathsAsync(ignoredFile);
32    const filteredLoadedModules = loadedModules.filter((modulePath) => !(0, Path_1.isIgnoredPath)(modulePath, ignoredPaths));
33    console.log(JSON.stringify({ config, loadedModules: filteredLoadedModules }));
34}
35// If running from the command line
36if (require.main?.filename === __filename) {
37    (async () => {
38        const programIndex = process.argv.findIndex((arg) => arg === __filename);
39        try {
40            await runAsync(process.argv[programIndex], process.argv.slice(programIndex + 1));
41        }
42        catch (e) {
43            console.error('Uncaught Error', e);
44            process.exit(1);
45        }
46    })();
47}
48/**
49 * Load the generated ignored paths file from caller and remove the file after loading
50 */
51async function loadIgnoredPathsAsync(ignoredFile) {
52    if (!ignoredFile) {
53        return Options_1.DEFAULT_IGNORE_PATHS;
54    }
55    const ignorePaths = [];
56    try {
57        const fingerprintIgnore = await promises_1.default.readFile(ignoredFile, 'utf8');
58        const fingerprintIgnoreLines = fingerprintIgnore.split('\n');
59        for (const line of fingerprintIgnoreLines) {
60            const trimmedLine = line.trim();
61            if (trimmedLine) {
62                ignorePaths.push(trimmedLine);
63            }
64        }
65    }
66    catch { }
67    try {
68        await promises_1.default.rm(ignoredFile);
69    }
70    catch { }
71    return ignorePaths;
72}
73/**
74 * Get the path to the ExpoConfigLoader file.
75 */
76function getExpoConfigLoaderPath() {
77    return path_1.default.join(__dirname, 'ExpoConfigLoader.js');
78}
79exports.getExpoConfigLoaderPath = getExpoConfigLoaderPath;
80//# sourceMappingURL=ExpoConfigLoader.js.map