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