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 });
6exports.createExampleApp = void 0;
7const spawn_async_1 = __importDefault(require("@expo/spawn-async"));
8const fs_extra_1 = __importDefault(require("fs-extra"));
9const path_1 = __importDefault(require("path"));
10const packageManager_1 = require("./packageManager");
11const utils_1 = require("./utils");
12// These dependencies will be removed from the example app (`expo init` adds them)
13const DEPENDENCIES_TO_REMOVE = ['expo-status-bar', 'expo-splash-screen'];
14/**
15 * Initializes a new Expo project as an example app.
16 */
17async function createExampleApp(data, targetDir, packageManager) {
18    const exampleProjectSlug = `${data.project.slug}-example`;
19    await (0, utils_1.newStep)('Initializing the example app', async (step) => {
20        await (0, spawn_async_1.default)('expo', ['init', exampleProjectSlug, '--template', 'expo-template-blank-typescript'], {
21            cwd: targetDir,
22            stdio: ['ignore', 'ignore', 'inherit'],
23        });
24        step.succeed('Initialized the example app');
25    });
26    // `expo init` creates a new folder with the same name as the project slug
27    const appTmpPath = path_1.default.join(targetDir, exampleProjectSlug);
28    // Path to the target example dir
29    const appTargetPath = path_1.default.join(targetDir, 'example');
30    await (0, utils_1.newStep)('Configuring the example app', async (step) => {
31        // "example" folder already exists and contains template files,
32        // that should replace these created by `expo init`.
33        await moveFiles(appTargetPath, appTmpPath);
34        // Cleanup the "example" dir
35        await fs_extra_1.default.rmdir(appTargetPath);
36        // Move the temporary example app to "example" dir
37        await fs_extra_1.default.rename(appTmpPath, appTargetPath);
38        await addMissingAppConfigFields(appTargetPath, data);
39        step.succeed('Configured the example app');
40    });
41    await prebuildExampleApp(appTargetPath);
42    await modifyPackageJson(appTargetPath);
43    await (0, utils_1.newStep)('Installing dependencies in the example app', async (step) => {
44        await (0, packageManager_1.installDependencies)(packageManager, appTargetPath);
45        await podInstall(appTargetPath);
46        step.succeed('Installed dependencies in the example app');
47    });
48}
49exports.createExampleApp = createExampleApp;
50/**
51 * Copies files from one directory to another.
52 */
53async function moveFiles(fromPath, toPath) {
54    for (const file of await fs_extra_1.default.readdir(fromPath)) {
55        await fs_extra_1.default.move(path_1.default.join(fromPath, file), path_1.default.join(toPath, file), {
56            overwrite: true,
57        });
58    }
59}
60/**
61 * Adds missing configuration that are required to run `expo prebuild`.
62 */
63async function addMissingAppConfigFields(appPath, data) {
64    const appConfigPath = path_1.default.join(appPath, 'app.json');
65    const appConfig = await fs_extra_1.default.readJson(appConfigPath);
66    const appId = `${data.project.package}.example`;
67    // Android package name needs to be added to app.json
68    if (!appConfig.expo.android) {
69        appConfig.expo.android = {};
70    }
71    appConfig.expo.android.package = appId;
72    // Specify iOS bundle identifier
73    if (!appConfig.expo.ios) {
74        appConfig.expo.ios = {};
75    }
76    appConfig.expo.ios.bundleIdentifier = appId;
77    await fs_extra_1.default.writeJson(appConfigPath, appConfig, {
78        spaces: 2,
79    });
80}
81/**
82 * Applies necessary changes to **package.json** of the example app.
83 * It means setting the autolinking config and removing unnecessary dependencies.
84 */
85async function modifyPackageJson(appPath) {
86    const packageJsonPath = path_1.default.join(appPath, 'package.json');
87    const packageJson = await fs_extra_1.default.readJson(packageJsonPath);
88    if (!packageJson.expo) {
89        packageJson.expo = {};
90    }
91    // Set the native modules dir to the root folder,
92    // so that the autolinking can detect and link the module.
93    packageJson.expo.autolinking = {
94        nativeModulesDir: '..',
95    };
96    // Remove unnecessary dependencies
97    for (const dependencyToRemove of DEPENDENCIES_TO_REMOVE) {
98        delete packageJson.dependencies[dependencyToRemove];
99    }
100    await fs_extra_1.default.writeJson(packageJsonPath, packageJson, {
101        spaces: 2,
102    });
103}
104/**
105 * Runs `expo prebuild` in the example app.
106 */
107async function prebuildExampleApp(exampleAppPath) {
108    await (0, utils_1.newStep)('Prebuilding the example app', async (step) => {
109        await (0, spawn_async_1.default)('expo', ['prebuild', '--no-install'], {
110            cwd: exampleAppPath,
111            stdio: ['ignore', 'ignore', 'pipe'],
112        });
113        step.succeed('Prebuilt the example app');
114    });
115}
116/**
117 * Runs `pod install` in the iOS project at the given path.
118 */
119async function podInstall(appPath) {
120    await (0, spawn_async_1.default)('pod', ['install'], {
121        cwd: path_1.default.join(appPath, 'ios'),
122        stdio: ['ignore', 'ignore', 'pipe'],
123    });
124}
125//# sourceMappingURL=createExampleApp.js.map