1import assert from 'assert'; 2 3export type LanguageOptions = { 4 isTS: boolean; 5 isModern: boolean; 6 isReact: boolean; 7}; 8 9export function getExtensions( 10 platforms: string[], 11 extensions: string[], 12 workflows: string[] 13): string[] { 14 // In the past we used spread operators to collect the values so now we enforce type safety on them. 15 assert(Array.isArray(platforms), 'Expected: `platforms: string[]`'); 16 assert(Array.isArray(extensions), 'Expected: `extensions: string[]`'); 17 assert(Array.isArray(workflows), 'Expected: `workflows: string[]`'); 18 19 const fileExtensions = []; 20 // support .expo files 21 for (const workflow of [...workflows, '']) { 22 // Ensure order is correct: [platformA.js, platformB.js, js] 23 for (const platform of [...platforms, '']) { 24 // Support both TypeScript and JavaScript 25 for (const extension of extensions) { 26 fileExtensions.push([platform, workflow, extension].filter(Boolean).join('.')); 27 } 28 } 29 } 30 return fileExtensions; 31} 32 33export function getLanguageExtensionsInOrder({ 34 isTS, 35 isModern, 36 isReact, 37}: LanguageOptions): string[] { 38 // @ts-ignore: filter removes false type 39 const addLanguage = (lang: string): string[] => [lang, isReact && `${lang}x`].filter(Boolean); 40 41 // Support JavaScript 42 let extensions = addLanguage('js'); 43 44 if (isModern) { 45 extensions.unshift('mjs'); 46 } 47 if (isTS) { 48 extensions = [...addLanguage('ts'), ...extensions]; 49 } 50 51 return extensions; 52} 53 54export function getManagedExtensions( 55 platforms: string[], 56 languageOptions: LanguageOptions = { isTS: true, isModern: true, isReact: true } 57): string[] { 58 const fileExtensions = getExtensions(platforms, getLanguageExtensionsInOrder(languageOptions), [ 59 'expo', 60 ]); 61 // Always add these last 62 _addMiscellaneousExtensions(platforms, fileExtensions); 63 return fileExtensions; 64} 65 66export function getBareExtensions( 67 platforms: string[], 68 languageOptions: LanguageOptions = { isTS: true, isModern: true, isReact: true } 69): string[] { 70 const fileExtensions = getExtensions( 71 platforms, 72 getLanguageExtensionsInOrder(languageOptions), 73 [] 74 ); 75 // Always add these last 76 _addMiscellaneousExtensions(platforms, fileExtensions); 77 return fileExtensions; 78} 79 80function _addMiscellaneousExtensions(platforms: string[], fileExtensions: string[]): string[] { 81 // Always add these with no platform extension 82 // In the future we may want to add platform and workspace extensions to json. 83 fileExtensions.push('json'); 84 // Native doesn't currently support web assembly. 85 if (platforms.includes('web')) { 86 fileExtensions.push('wasm'); 87 } 88 return fileExtensions; 89} 90