Skip to content

Commit 69516ec

Browse files
committed
fix: optional component.json + updating path aliases + texts + remove unused imports
1 parent 5a2562c commit 69516ec

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

apps/cli/src/commands/init.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import path from 'path';
77
import { z } from 'zod';
88
import { handleError } from '@/src/utils/handle-error';
99
import { logger } from '@/src/utils/logger';
10-
import { fileURLToPath } from 'url';
1110
import chalk from 'chalk';
1211
import prompts from 'prompts';
1312
import glob from 'fast-glob';
@@ -21,9 +20,6 @@ import {
2120
DEFAULT_LIB,
2221
} from '@/src/utils/get-config';
2322

24-
const filePath = fileURLToPath(import.meta.url);
25-
const fileDir = path.dirname(filePath);
26-
2723
const initOptionsSchema = z.object({
2824
cwd: z.string(),
2925
overwrite: z.boolean(),
@@ -106,41 +102,55 @@ async function promptForConfig(cwd: string) {
106102
initial: true,
107103
});
108104

109-
if (!proceed) {
110-
logger.info('Configuration cancelled.');
111-
process.exit(0);
105+
if (proceed) {
106+
logger.info('');
107+
const spinner = ora(`Writing components.json...`).start();
108+
const targetPath = path.resolve(cwd, 'components.json');
109+
await fs.writeFile(targetPath, JSON.stringify(config, null, 2), 'utf8');
110+
spinner.succeed();
112111
}
113112

114-
logger.info('');
115-
const spinner = ora(`Writing components.json...`).start();
116-
const targetPath = path.resolve(cwd, 'components.json');
117-
await fs.writeFile(targetPath, JSON.stringify(config, null, 2), 'utf8');
118-
spinner.succeed();
119-
120113
return await resolveConfigPaths(cwd, config);
121114
} catch (error) {
122115
logger.error('Failed to configure project.');
123116
process.exit(1);
124117
}
125118
}
126119

120+
const NON_PATH_ALIAS_BASES = ['', '.', '/'];
121+
127122
async function updateTsConfig(cwd: string, config: any, spinner: Ora) {
128123
try {
129-
spinner.text = 'Configuring path aliases...';
130124
const tsconfigPath = path.join(cwd, 'tsconfig.json');
131125
const tsconfig = existsSync(tsconfigPath)
132126
? JSON.parse(await fs.readFile(tsconfigPath, 'utf8'))
133127
: {};
134128

135129
const componentBase = config.aliases.components.split('/')[0];
136130
const libBase = config.aliases.lib.split('/')[0];
137-
const basePath = componentBase === libBase ? componentBase : '@';
131+
132+
if (NON_PATH_ALIAS_BASES.includes(componentBase) || NON_PATH_ALIAS_BASES.includes(libBase)) {
133+
return;
134+
}
135+
136+
const tsconfigPaths = tsconfig.compilerOptions?.paths ?? {};
137+
138+
if (
139+
tsconfigPaths[`${componentBase}/*`]?.[0] === '*' &&
140+
tsconfigPaths[`${libBase}/*`]?.[0] === '*'
141+
) {
142+
spinner.succeed('Path aliases already configured');
143+
return;
144+
}
145+
146+
spinner.text = 'Updating path aliases...';
138147

139148
tsconfig.compilerOptions = {
140149
...tsconfig.compilerOptions,
141150
baseUrl: '.',
142151
paths: {
143-
[`${basePath}/*`]: ['*'],
152+
[`${componentBase}/*`]: ['*'],
153+
[`${libBase}/*`]: ['*'],
144154
...tsconfig.compilerOptions?.paths,
145155
},
146156
};
@@ -192,7 +202,6 @@ async function copyTemplateFile(
192202

193203
async function updateLayoutFile(cwd: string, spinner: Ora) {
194204
try {
195-
spinner.text = 'Updating layout file...';
196205
const layoutFiles = await glob(
197206
['app/_layout.{ts,tsx,js,jsx}', '(app)/_layout.{ts,tsx,js,jsx}'],
198207
{
@@ -202,14 +211,15 @@ async function updateLayoutFile(cwd: string, spinner: Ora) {
202211
);
203212

204213
if (!layoutFiles.length) {
205-
spinner.warn('No _layout file found in app directory');
214+
spinner.warn('Could not find the root _layout file');
206215
return;
207216
}
208217

209218
const layoutPath = path.join(cwd, layoutFiles[0]);
210219
const content = await fs.readFile(layoutPath, 'utf8');
211220

212221
if (!content.includes('import "../global.css"')) {
222+
spinner.text = 'Updating layout file...';
213223
await fs.writeFile(layoutPath, `import "../global.css";\n${content}`);
214224
spinner.succeed(`Updated ${layoutFiles[0]} with global CSS import`);
215225
}
@@ -277,7 +287,7 @@ async function initializeProject(cwd: string, overwrite: boolean) {
277287
await installDependencies(cwd, spinner);
278288
await updateTsConfig(cwd, config, spinner);
279289

280-
spinner.text = 'Copying template files...';
290+
spinner.text = 'Adding config and utility files...';
281291
for (const file of TEMPLATE_FILES) {
282292
await copyTemplateFile(file, templatesDir, cwd, spinner, overwrite);
283293
}
@@ -294,7 +304,7 @@ async function initializeProject(cwd: string, overwrite: boolean) {
294304

295305
export const init = new Command()
296306
.name('init')
297-
.description('Initialize the React Native project with required configuration')
307+
.description('Initialize the required configuration for your React Native project')
298308
.option(
299309
'-c, --cwd <cwd>',
300310
'the working directory. defaults to the current directory.',

0 commit comments

Comments
 (0)