( data: SubstitutionData, targetDir: string, packageManager: PackageManagerName )
| 20 | * Initializes a new Expo project as an example app. |
| 21 | */ |
| 22 | export async function createExampleApp( |
| 23 | data: SubstitutionData, |
| 24 | targetDir: string, |
| 25 | packageManager: PackageManagerName |
| 26 | ): Promise<void> { |
| 27 | // Package name for the example app |
| 28 | const exampleProjectSlug = `${data.project.slug}-example`; |
| 29 | |
| 30 | // `expo init` creates a new folder with the same name as the project slug |
| 31 | const appTmpPath = path.join(targetDir, exampleProjectSlug); |
| 32 | |
| 33 | // Path to the target example dir |
| 34 | const appTargetPath = path.join(targetDir, 'example'); |
| 35 | |
| 36 | if (!fs.existsSync(appTargetPath)) { |
| 37 | // The template doesn't include the example app, so just skip this phase |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | await newStep('Initializing the example app', async (step) => { |
| 42 | // Pin the example template to the same SDK as the module template (derived from the CLI's own |
| 43 | // version), so `create-expo-module@sdk-XX` scaffolds an SDK XX example rather than always using |
| 44 | // `latest`. Fall back to `latest` when the SDK can't be determined or its template isn't published. |
| 45 | const distTag = env.EXPO_BETA ? 'next' : getTemplateDistTag(require('../package.json').version); |
| 46 | |
| 47 | try { |
| 48 | await initExampleApp(packageManager, exampleProjectSlug, targetDir, distTag); |
| 49 | } catch (error: any) { |
| 50 | if (env.EXPO_BETA || distTag === 'latest') { |
| 51 | throw error; |
| 52 | } |
| 53 | |
| 54 | console.warn( |
| 55 | chalk.yellow(`Failed to use the "${distTag}" example template, falling back to "latest".`) |
| 56 | ); |
| 57 | |
| 58 | await initExampleApp(packageManager, exampleProjectSlug, targetDir, 'latest'); |
| 59 | } |
| 60 | |
| 61 | step.succeed('Initialized the example app'); |
| 62 | }); |
| 63 | |
| 64 | await newStep('Configuring the example app', async (step) => { |
| 65 | // "example" folder already exists and contains template files, |
| 66 | // that should replace these created by `expo init`. |
| 67 | await moveFiles(appTargetPath, appTmpPath); |
| 68 | |
| 69 | // Cleanup the "example" dir |
| 70 | await fs.promises.rm(appTargetPath, { recursive: true, force: true }); |
| 71 | |
| 72 | // Clean up the ".git" from example app |
| 73 | // note, this directory has contents, rmdir will throw |
| 74 | await fs.promises.rm(path.join(appTmpPath, '.git'), { recursive: true, force: true }); |
| 75 | |
| 76 | // Move the temporary example app to "example" dir |
| 77 | await fs.promises.rename(appTmpPath, appTargetPath); |
| 78 | |
| 79 | await addMissingAppConfigFields(appTargetPath, data); |
no test coverage detected
searching dependent graphs…