Skip to content

Commit

Permalink
Add Prettier to expo-setup script
Browse files Browse the repository at this point in the history
  • Loading branch information
ProchaLu committed Jan 24, 2025
1 parent aac3141 commit d3caa1d
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions bin/expo-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
// - https://archive.ph/MG03E
//
// TODO: Remove when Expo enables New Architecture and new Metro resolver by default
import { execSync } from 'node:child_process';
import { readFile, unlink, writeFile } from 'node:fs/promises';
import isPlainObject from 'is-plain-obj';

execSync('pnpm add --save-dev prettier', { stdio: 'inherit' });

const { format } = await import('prettier');

const appFilePath = 'app.json';
const appJson = JSON.parse(await readFile(appFilePath, 'utf8'));

Expand All @@ -22,19 +27,18 @@ if (!isPlainObject(appJson) || !isPlainObject(appJson.expo)) {
);
}

const expoConfig =
`import { type ExpoConfig } from "expo/config";
const expoConfig = `import { type ExpoConfig } from 'expo/config';
const config: ExpoConfig = ${JSON.stringify(appJson.expo, null, 2)};
const config: ExpoConfig = ${JSON.stringify(appJson.expo, null, 2)
.replace(/"([^"]+)":/g, '$1:')
.replace(/"(.*?)"/g, `'$1'`)
.replace(/([}\]])(\s*[}\]])/g, '$1,$2')
.replace(/}(\s+\])/g, '},$1')
.replace(/(?<!,)(\n\s*[}\]])/g, ',$1')};
export default config;`.trim();

export default config;`.trim() + '\n';
const formattedConfig = await format(expoConfig, {
parser: 'typescript',
singleQuote: true,
});

await writeFile('app.config.ts', expoConfig, 'utf8');
await writeFile('app.config.ts', formattedConfig, 'utf8');
console.log('✅ Converted and formatted app.json to app.config.ts');

await unlink(appFilePath);
Expand Down

0 comments on commit d3caa1d

Please sign in to comment.