Skip to content

Commit e84bf3f

Browse files
unisol1020mrzachnugent
authored andcommitted
refactor init command to improve error handling and user prompts during project initialization
1 parent eb8ad60 commit e84bf3f

File tree

1 file changed

+64
-43
lines changed

1 file changed

+64
-43
lines changed

apps/cli/src/commands/init.ts

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -62,53 +62,66 @@ async function installDependencies(cwd: string, spinner: Ora) {
6262
cwd,
6363
stdio: 'inherit',
6464
});
65+
spinner.text = 'Dependencies installed successfully';
6566
} catch (error) {
6667
spinner.fail('Failed to install dependencies');
6768
handleError(error);
69+
process.exit(1);
6870
}
6971
}
7072

7173
async function promptForConfig(cwd: string) {
7274
const highlight = (text: string) => chalk.cyan(text);
7375

74-
const options = await prompts([
75-
{
76-
type: 'text',
77-
name: 'components',
78-
message: `Configure the import alias for ${highlight('components')}:`,
79-
initial: DEFAULT_COMPONENTS,
80-
},
81-
{
82-
type: 'text',
83-
name: 'lib',
84-
message: `Configure the import alias for ${highlight('lib')}:`,
85-
initial: DEFAULT_LIB,
86-
},
87-
]);
88-
89-
const config = rawConfigSchema.parse({
90-
aliases: {
91-
lib: options.lib,
92-
components: options.components,
93-
},
94-
});
76+
try {
77+
const options = await prompts([
78+
{
79+
type: 'text',
80+
name: 'components',
81+
message: `Configure the import alias for ${highlight('components')}:`,
82+
initial: DEFAULT_COMPONENTS,
83+
},
84+
{
85+
type: 'text',
86+
name: 'lib',
87+
message: `Configure the import alias for ${highlight('lib')}:`,
88+
initial: DEFAULT_LIB,
89+
},
90+
]);
9591

96-
const { proceed } = await prompts({
97-
type: 'confirm',
98-
name: 'proceed',
99-
message: `Write configuration to ${highlight('components.json')}. Proceed?`,
100-
initial: true,
101-
});
92+
const components = options.components || DEFAULT_COMPONENTS;
93+
const lib = options.lib || DEFAULT_LIB;
94+
95+
const config = rawConfigSchema.parse({
96+
aliases: {
97+
components,
98+
lib,
99+
},
100+
});
101+
102+
const { proceed } = await prompts({
103+
type: 'confirm',
104+
name: 'proceed',
105+
message: `Write configuration to ${highlight('components.json')}. Proceed?`,
106+
initial: true,
107+
});
108+
109+
if (!proceed) {
110+
logger.info('Configuration cancelled.');
111+
process.exit(0);
112+
}
102113

103-
if (proceed) {
104114
logger.info('');
105115
const spinner = ora(`Writing components.json...`).start();
106116
const targetPath = path.resolve(cwd, 'components.json');
107117
await fs.writeFile(targetPath, JSON.stringify(config, null, 2), 'utf8');
108118
spinner.succeed();
109-
}
110119

111-
return await resolveConfigPaths(cwd, config);
120+
return await resolveConfigPaths(cwd, config);
121+
} catch (error) {
122+
logger.error('Failed to configure project.');
123+
process.exit(1);
124+
}
112125
}
113126

114127
async function updateTsConfig(cwd: string, config: any, spinner: Ora) {
@@ -250,25 +263,33 @@ async function checkGitStatus(cwd: string) {
250263
async function initializeProject(cwd: string, overwrite: boolean) {
251264
const spinner = ora(`Initializing project...`).start();
252265

253-
let config = await getConfig(cwd);
266+
try {
267+
let config = await getConfig(cwd);
254268

255-
if (!config) {
256-
config = await promptForConfig(cwd);
257-
}
269+
if (!config) {
270+
spinner.stop();
271+
config = await promptForConfig(cwd);
272+
spinner.start();
273+
}
258274

259-
const templatesDir = path.dirname(createRequire(import.meta.url).resolve('@rnr/starter-base'));
275+
const templatesDir = path.dirname(createRequire(import.meta.url).resolve('@rnr/starter-base'));
260276

261-
await installDependencies(cwd, spinner);
262-
await updateTsConfig(cwd, config, spinner);
277+
await installDependencies(cwd, spinner);
278+
await updateTsConfig(cwd, config, spinner);
263279

264-
spinner.text = 'Copying template files...';
265-
for (const file of TEMPLATE_FILES) {
266-
await copyTemplateFile(file, templatesDir, cwd, spinner, overwrite);
267-
}
280+
spinner.text = 'Copying template files...';
281+
for (const file of TEMPLATE_FILES) {
282+
await copyTemplateFile(file, templatesDir, cwd, spinner, overwrite);
283+
}
268284

269-
await updateLayoutFile(cwd, spinner);
285+
await updateLayoutFile(cwd, spinner);
270286

271-
spinner.succeed('Initialization completed successfully!');
287+
spinner.succeed('Initialization completed successfully!');
288+
} catch (error) {
289+
spinner.fail('Initialization failed');
290+
handleError(error);
291+
process.exit(1);
292+
}
272293
}
273294

274295
export const init = new Command()

0 commit comments

Comments
 (0)