Skip to content

Commit 77dec78

Browse files
unisol1020mrzachnugent
authored andcommitted
add configuration prompts for components and library aliases in init command
1 parent ae55f80 commit 77dec78

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

apps/cli/src/commands/init.ts

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import prompts from 'prompts';
1313
import glob from 'fast-glob';
1414
import { createRequire } from 'module';
1515
import { execSync } from 'child_process';
16+
import {
17+
getConfig,
18+
rawConfigSchema,
19+
resolveConfigPaths,
20+
DEFAULT_COMPONENTS,
21+
DEFAULT_LIB,
22+
} from '@/src/utils/get-config';
1623

1724
const filePath = fileURLToPath(import.meta.url);
1825
const fileDir = path.dirname(filePath);
@@ -61,19 +68,66 @@ async function installDependencies(cwd: string, spinner: Ora) {
6168
}
6269
}
6370

64-
async function updateTsConfig(cwd: string, spinner: Ora) {
71+
async function promptForConfig(cwd: string) {
72+
const highlight = (text: string) => chalk.cyan(text);
73+
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+
});
95+
96+
const { proceed } = await prompts({
97+
type: 'confirm',
98+
name: 'proceed',
99+
message: `Write configuration to ${highlight('components.json')}. Proceed?`,
100+
initial: true,
101+
});
102+
103+
if (proceed) {
104+
logger.info('');
105+
const spinner = ora(`Writing components.json...`).start();
106+
const targetPath = path.resolve(cwd, 'components.json');
107+
await fs.writeFile(targetPath, JSON.stringify(config, null, 2), 'utf8');
108+
spinner.succeed();
109+
}
110+
111+
return await resolveConfigPaths(cwd, config);
112+
}
113+
114+
async function updateTsConfig(cwd: string, config: any, spinner: Ora) {
65115
try {
66116
spinner.text = 'Configuring path aliases...';
67117
const tsconfigPath = path.join(cwd, 'tsconfig.json');
68118
const tsconfig = existsSync(tsconfigPath)
69119
? JSON.parse(await fs.readFile(tsconfigPath, 'utf8'))
70120
: {};
71121

122+
const componentBase = config.aliases.components.split('/')[0];
123+
const libBase = config.aliases.lib.split('/')[0];
124+
const basePath = componentBase === libBase ? componentBase : '@';
125+
72126
tsconfig.compilerOptions = {
73127
...tsconfig.compilerOptions,
74128
baseUrl: '.',
75129
paths: {
76-
'~/*': ['*'],
130+
[`${basePath}/*`]: ['*'],
77131
...tsconfig.compilerOptions?.paths,
78132
},
79133
};
@@ -195,7 +249,9 @@ async function validateProjectDirectory(cwd: string) {
195249
}
196250

197251
if (!existsSync(path.join(cwd, 'package.json'))) {
198-
logger.error('No package.json found. Please run this command in a React Native project directory.');
252+
logger.error(
253+
'No package.json found. Please run this command in a React Native project directory.'
254+
);
199255
process.exit(1);
200256
}
201257
}
@@ -205,7 +261,8 @@ async function checkGitStatus(cwd: string) {
205261
const { proceed } = await prompts({
206262
type: 'confirm',
207263
name: 'proceed',
208-
message: 'The Git repository is dirty (uncommitted changes). It is recommended to commit your changes before proceeding. Do you want to continue?',
264+
message:
265+
'The Git repository is dirty (uncommitted changes). It is recommended to commit your changes before proceeding. Do you want to continue?',
209266
initial: false,
210267
});
211268

@@ -218,12 +275,17 @@ async function checkGitStatus(cwd: string) {
218275

219276
async function initializeProject(cwd: string, overwrite: boolean) {
220277
const spinner = ora(`Initializing project...`).start();
221-
const templatesDir = path.dirname(
222-
createRequire(import.meta.url).resolve('@rnr/starter-base')
223-
);
278+
279+
let config = await getConfig(cwd);
280+
281+
if (!config) {
282+
config = await promptForConfig(cwd);
283+
}
284+
285+
const templatesDir = path.dirname(createRequire(import.meta.url).resolve('@rnr/starter-base'));
224286

225287
await installDependencies(cwd, spinner);
226-
await updateTsConfig(cwd, spinner);
288+
await updateTsConfig(cwd, config, spinner);
227289

228290
spinner.text = 'Copying template files...';
229291
for (const file of TEMPLATE_FILES) {

0 commit comments

Comments
 (0)