1import JsonFile, { JSONObject } from '@expo/json-file'; 2import fs from 'fs'; 3import path from 'path'; 4 5/** Create a set of functions for managing a file in the project's `.expo` directory. */ 6export function createTemporaryProjectFile<T extends JSONObject>(fileName: string, defaults: T) { 7 function getFile(projectRoot: string): JsonFile<T> { 8 const dotExpoDir = ensureDotExpoProjectDirectoryInitialized(projectRoot); 9 return new JsonFile<T>(path.join(dotExpoDir, fileName)); 10 } 11 12 async function readAsync(projectRoot: string): Promise<T> { 13 let projectSettings; 14 try { 15 projectSettings = await getFile(projectRoot).readAsync(); 16 } catch { 17 projectSettings = await getFile(projectRoot).writeAsync(defaults); 18 } 19 // Set defaults for any missing fields 20 return { ...defaults, ...projectSettings }; 21 } 22 23 async function setAsync(projectRoot: string, json: Partial<T>): Promise<T> { 24 try { 25 return await getFile(projectRoot).mergeAsync(json, { 26 cantReadFileDefault: defaults, 27 }); 28 } catch { 29 return await getFile(projectRoot).writeAsync({ 30 ...defaults, 31 ...json, 32 }); 33 } 34 } 35 36 return { 37 getFile, 38 readAsync, 39 setAsync, 40 }; 41} 42 43function getDotExpoProjectDirectory(projectRoot: string): string { 44 return path.join(projectRoot, '.expo'); 45} 46 47export function ensureDotExpoProjectDirectoryInitialized(projectRoot: string): string { 48 const dirPath = getDotExpoProjectDirectory(projectRoot); 49 fs.mkdirSync(dirPath, { recursive: true }); 50 51 const readmeFilePath = path.resolve(dirPath, 'README.md'); 52 if (!fs.existsSync(readmeFilePath)) { 53 fs.writeFileSync( 54 readmeFilePath, 55 `> Why do I have a folder named ".expo" in my project? 56The ".expo" folder is created when an Expo project is started using "expo start" command. 57> What do the files contain? 58- "devices.json": contains information about devices that have recently opened this project. This is used to populate the "Development sessions" list in your development builds. 59- "settings.json": contains the server configuration that is used to serve the application manifest. 60> Should I commit the ".expo" folder? 61No, you should not share the ".expo" folder. It does not contain any information that is relevant for other developers working on the project, it is specific to your machine. 62Upon project creation, the ".expo" folder is already added to your ".gitignore" file. 63` 64 ); 65 } 66 return dirPath; 67} 68