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