1ca5a2fa2STomasz Sapeta"use strict";
2ca5a2fa2STomasz Sapetavar __importDefault = (this && this.__importDefault) || function (mod) {
3ca5a2fa2STomasz Sapeta    return (mod && mod.__esModule) ? mod : { "default": mod };
4ca5a2fa2STomasz Sapeta};
5ca5a2fa2STomasz SapetaObject.defineProperty(exports, "__esModule", { value: true });
6ca5a2fa2STomasz Sapetaexports.createExampleApp = void 0;
7ca5a2fa2STomasz Sapetaconst spawn_async_1 = __importDefault(require("@expo/spawn-async"));
8ca5a2fa2STomasz Sapetaconst fs_extra_1 = __importDefault(require("fs-extra"));
9c4c29f98SBrent Vatneconst getenv_1 = __importDefault(require("getenv"));
107a6db19cSTomasz Sapetaconst os_1 = __importDefault(require("os"));
11ca5a2fa2STomasz Sapetaconst path_1 = __importDefault(require("path"));
12ca5a2fa2STomasz Sapetaconst packageManager_1 = require("./packageManager");
13234d009cSTomasz Sapetaconst utils_1 = require("./utils");
14c4c29f98SBrent Vatneconst debug = require('debug')('create-expo-module:createExampleApp');
15ca5a2fa2STomasz Sapeta// These dependencies will be removed from the example app (`expo init` adds them)
16ca5a2fa2STomasz Sapetaconst DEPENDENCIES_TO_REMOVE = ['expo-status-bar', 'expo-splash-screen'];
17c4c29f98SBrent Vatneconst EXPO_BETA = getenv_1.default.boolish('EXPO_BETA', false);
18ca5a2fa2STomasz Sapeta/**
19ca5a2fa2STomasz Sapeta * Initializes a new Expo project as an example app.
20ca5a2fa2STomasz Sapeta */
21ca5a2fa2STomasz Sapetaasync function createExampleApp(data, targetDir, packageManager) {
2250ef9c7cSTomasz Sapeta    // Package name for the example app
23ca5a2fa2STomasz Sapeta    const exampleProjectSlug = `${data.project.slug}-example`;
2450ef9c7cSTomasz Sapeta    // `expo init` creates a new folder with the same name as the project slug
2550ef9c7cSTomasz Sapeta    const appTmpPath = path_1.default.join(targetDir, exampleProjectSlug);
2650ef9c7cSTomasz Sapeta    // Path to the target example dir
2750ef9c7cSTomasz Sapeta    const appTargetPath = path_1.default.join(targetDir, 'example');
2850ef9c7cSTomasz Sapeta    if (!(await fs_extra_1.default.pathExists(appTargetPath))) {
2950ef9c7cSTomasz Sapeta        // The template doesn't include the example app, so just skip this phase
3050ef9c7cSTomasz Sapeta        return;
3150ef9c7cSTomasz Sapeta    }
32234d009cSTomasz Sapeta    await (0, utils_1.newStep)('Initializing the example app', async (step) => {
33c4c29f98SBrent Vatne        const templateVersion = EXPO_BETA ? 'next' : 'latest';
34c4c29f98SBrent Vatne        const template = `expo-template-blank-typescript@${templateVersion}`;
35c4c29f98SBrent Vatne        debug(`Using example template: ${template}`);
36c4c29f98SBrent Vatne        await (0, spawn_async_1.default)(packageManager, ['create', 'expo-app', '--', exampleProjectSlug, '--template', template, '--yes'], {
37ca5a2fa2STomasz Sapeta            cwd: targetDir,
38a2ce1fb6STomasz Sapeta            stdio: 'ignore',
39ca5a2fa2STomasz Sapeta        });
40234d009cSTomasz Sapeta        step.succeed('Initialized the example app');
41234d009cSTomasz Sapeta    });
42234d009cSTomasz Sapeta    await (0, utils_1.newStep)('Configuring the example app', async (step) => {
43ca5a2fa2STomasz Sapeta        // "example" folder already exists and contains template files,
44ca5a2fa2STomasz Sapeta        // that should replace these created by `expo init`.
45ca5a2fa2STomasz Sapeta        await moveFiles(appTargetPath, appTmpPath);
46ca5a2fa2STomasz Sapeta        // Cleanup the "example" dir
47ca5a2fa2STomasz Sapeta        await fs_extra_1.default.rmdir(appTargetPath);
4818522c87SCedric van Putten        // Clean up the ".git" from example app
4918522c87SCedric van Putten        // note, this directory has contents, rmdir will throw
5018522c87SCedric van Putten        await fs_extra_1.default.remove(path_1.default.join(appTmpPath, '.git'));
51ca5a2fa2STomasz Sapeta        // Move the temporary example app to "example" dir
52ca5a2fa2STomasz Sapeta        await fs_extra_1.default.rename(appTmpPath, appTargetPath);
53ca5a2fa2STomasz Sapeta        await addMissingAppConfigFields(appTargetPath, data);
54234d009cSTomasz Sapeta        step.succeed('Configured the example app');
55234d009cSTomasz Sapeta    });
56ca5a2fa2STomasz Sapeta    await prebuildExampleApp(appTargetPath);
57ca5a2fa2STomasz Sapeta    await modifyPackageJson(appTargetPath);
58234d009cSTomasz Sapeta    await (0, utils_1.newStep)('Installing dependencies in the example app', async (step) => {
59ca5a2fa2STomasz Sapeta        await (0, packageManager_1.installDependencies)(packageManager, appTargetPath);
607a6db19cSTomasz Sapeta        if (os_1.default.platform() === 'darwin') {
61ca5a2fa2STomasz Sapeta            await podInstall(appTargetPath);
62234d009cSTomasz Sapeta            step.succeed('Installed dependencies in the example app');
637a6db19cSTomasz Sapeta        }
647a6db19cSTomasz Sapeta        else {
657a6db19cSTomasz Sapeta            step.succeed('Installed dependencies in the example app (skipped installing CocoaPods)');
667a6db19cSTomasz Sapeta        }
67234d009cSTomasz Sapeta    });
68ca5a2fa2STomasz Sapeta}
69ca5a2fa2STomasz Sapetaexports.createExampleApp = createExampleApp;
70ca5a2fa2STomasz Sapeta/**
71ca5a2fa2STomasz Sapeta * Copies files from one directory to another.
72ca5a2fa2STomasz Sapeta */
73ca5a2fa2STomasz Sapetaasync function moveFiles(fromPath, toPath) {
74ca5a2fa2STomasz Sapeta    for (const file of await fs_extra_1.default.readdir(fromPath)) {
75ca5a2fa2STomasz Sapeta        await fs_extra_1.default.move(path_1.default.join(fromPath, file), path_1.default.join(toPath, file), {
76ca5a2fa2STomasz Sapeta            overwrite: true,
77ca5a2fa2STomasz Sapeta        });
78ca5a2fa2STomasz Sapeta    }
79ca5a2fa2STomasz Sapeta}
80ca5a2fa2STomasz Sapeta/**
81*384598e2SBrent Vatne * Adds missing configuration that are required to run `npx expo prebuild`.
82ca5a2fa2STomasz Sapeta */
83ca5a2fa2STomasz Sapetaasync function addMissingAppConfigFields(appPath, data) {
84ca5a2fa2STomasz Sapeta    const appConfigPath = path_1.default.join(appPath, 'app.json');
85ca5a2fa2STomasz Sapeta    const appConfig = await fs_extra_1.default.readJson(appConfigPath);
86ca5a2fa2STomasz Sapeta    const appId = `${data.project.package}.example`;
87ca5a2fa2STomasz Sapeta    // Android package name needs to be added to app.json
88ca5a2fa2STomasz Sapeta    if (!appConfig.expo.android) {
89ca5a2fa2STomasz Sapeta        appConfig.expo.android = {};
90ca5a2fa2STomasz Sapeta    }
91ca5a2fa2STomasz Sapeta    appConfig.expo.android.package = appId;
92ca5a2fa2STomasz Sapeta    // Specify iOS bundle identifier
93ca5a2fa2STomasz Sapeta    if (!appConfig.expo.ios) {
94ca5a2fa2STomasz Sapeta        appConfig.expo.ios = {};
95ca5a2fa2STomasz Sapeta    }
96ca5a2fa2STomasz Sapeta    appConfig.expo.ios.bundleIdentifier = appId;
97ca5a2fa2STomasz Sapeta    await fs_extra_1.default.writeJson(appConfigPath, appConfig, {
98ca5a2fa2STomasz Sapeta        spaces: 2,
99ca5a2fa2STomasz Sapeta    });
100ca5a2fa2STomasz Sapeta}
101ca5a2fa2STomasz Sapeta/**
102ca5a2fa2STomasz Sapeta * Applies necessary changes to **package.json** of the example app.
103ca5a2fa2STomasz Sapeta * It means setting the autolinking config and removing unnecessary dependencies.
104ca5a2fa2STomasz Sapeta */
105ca5a2fa2STomasz Sapetaasync function modifyPackageJson(appPath) {
106ca5a2fa2STomasz Sapeta    const packageJsonPath = path_1.default.join(appPath, 'package.json');
107ca5a2fa2STomasz Sapeta    const packageJson = await fs_extra_1.default.readJson(packageJsonPath);
108ca5a2fa2STomasz Sapeta    if (!packageJson.expo) {
109ca5a2fa2STomasz Sapeta        packageJson.expo = {};
110ca5a2fa2STomasz Sapeta    }
111ca5a2fa2STomasz Sapeta    // Set the native modules dir to the root folder,
112ca5a2fa2STomasz Sapeta    // so that the autolinking can detect and link the module.
113ca5a2fa2STomasz Sapeta    packageJson.expo.autolinking = {
114ca5a2fa2STomasz Sapeta        nativeModulesDir: '..',
115ca5a2fa2STomasz Sapeta    };
116ca5a2fa2STomasz Sapeta    // Remove unnecessary dependencies
117ca5a2fa2STomasz Sapeta    for (const dependencyToRemove of DEPENDENCIES_TO_REMOVE) {
118ca5a2fa2STomasz Sapeta        delete packageJson.dependencies[dependencyToRemove];
119ca5a2fa2STomasz Sapeta    }
120ca5a2fa2STomasz Sapeta    await fs_extra_1.default.writeJson(packageJsonPath, packageJson, {
121ca5a2fa2STomasz Sapeta        spaces: 2,
122ca5a2fa2STomasz Sapeta    });
123ca5a2fa2STomasz Sapeta}
124ca5a2fa2STomasz Sapeta/**
125fb08e93cSBartosz Kaszubowski * Runs `npx expo prebuild` in the example app.
126ca5a2fa2STomasz Sapeta */
127ca5a2fa2STomasz Sapetaasync function prebuildExampleApp(exampleAppPath) {
128234d009cSTomasz Sapeta    await (0, utils_1.newStep)('Prebuilding the example app', async (step) => {
129fb08e93cSBartosz Kaszubowski        await (0, spawn_async_1.default)('npx', ['expo', 'prebuild', '--no-install'], {
130ca5a2fa2STomasz Sapeta            cwd: exampleAppPath,
131ca5a2fa2STomasz Sapeta            stdio: ['ignore', 'ignore', 'pipe'],
132ca5a2fa2STomasz Sapeta        });
133234d009cSTomasz Sapeta        step.succeed('Prebuilt the example app');
134234d009cSTomasz Sapeta    });
135ca5a2fa2STomasz Sapeta}
136ca5a2fa2STomasz Sapeta/**
137ca5a2fa2STomasz Sapeta * Runs `pod install` in the iOS project at the given path.
138ca5a2fa2STomasz Sapeta */
139ca5a2fa2STomasz Sapetaasync function podInstall(appPath) {
140ca5a2fa2STomasz Sapeta    await (0, spawn_async_1.default)('pod', ['install'], {
141ca5a2fa2STomasz Sapeta        cwd: path_1.default.join(appPath, 'ios'),
142ca5a2fa2STomasz Sapeta        stdio: ['ignore', 'ignore', 'pipe'],
143ca5a2fa2STomasz Sapeta    });
144ca5a2fa2STomasz Sapeta}
145ca5a2fa2STomasz Sapeta//# sourceMappingURL=createExampleApp.js.map