Skip to content

Commit 43f5f75

Browse files
committed
feat(cli): add new project initialization
1 parent 5bcf1ed commit 43f5f75

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

apps/cli/src/commands/init.ts

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { copyFolder } from '@/src/utils/copy-folder';
12
import { getConfig } from '@/src/utils/get-config';
23
import { handleError } from '@/src/utils/handle-error';
34
import { logger } from '@/src/utils/logger';
@@ -76,13 +77,83 @@ async function validateProjectDirectory(cwd: string) {
7677
}
7778

7879
if (!existsSync(path.join(cwd, 'package.json'))) {
79-
logger.error(
80-
'No package.json found. Please run this command in a React Native project directory.'
81-
);
80+
const { proceed } = await prompts({
81+
type: 'confirm',
82+
name: 'proceed',
83+
message: 'No package.json found. Would you like to create a new project?',
84+
initial: false,
85+
});
86+
87+
if (!proceed) {
88+
logger.info('Initialization cancelled.');
89+
process.exit(0);
90+
}
91+
92+
const { projectName } = await prompts({
93+
type: 'text',
94+
name: 'projectName',
95+
message: `What is the name of your project?`,
96+
initial: 'starter-base',
97+
});
98+
99+
const spinner = ora(`Initializing ${projectName}...`).start();
100+
101+
const projectPath = path.join(cwd, projectName);
102+
103+
await copyFolder(path.join(fileDir, '../__generated/starter-base'), projectPath);
104+
105+
await Promise.all([
106+
replaceAllInJsonFile(path.join(cwd, projectName, 'app.json'), 'starter-base', projectName),
107+
replaceAllInJsonFile(
108+
path.join(cwd, projectName, 'package.json'),
109+
'@rnr/starter-base',
110+
projectName
111+
),
112+
]);
113+
114+
spinner.stop();
115+
const { packageManager } = await prompts({
116+
type: 'select',
117+
name: 'packageManager',
118+
message: 'Which package manager would you like to use?',
119+
choices: [
120+
{ title: 'npm', value: 'npm' },
121+
{ title: 'yarn', value: 'yarn' },
122+
{ title: 'pnpm', value: 'pnpm' },
123+
{ title: 'bun', value: 'bun' },
124+
],
125+
});
126+
127+
spinner.start('Installing dependencies...');
128+
await execa(packageManager, ['install'], {
129+
cwd: projectPath,
130+
});
131+
spinner.text = 'Verifying and updating any invalid package versions if needed...';
132+
await execa('npx', ['expo', 'install', '--fix'], {
133+
cwd: projectPath,
134+
});
135+
136+
spinner.succeed('New project initialized successfully!');
82137
process.exit(1);
83138
}
84139
}
85140

141+
async function replaceAllInJsonFile(path: string, searchValue: string, replaceValue: string) {
142+
try {
143+
if (!existsSync(path)) {
144+
logger.error(`The path ${path} does not exist.`);
145+
process.exit(1);
146+
}
147+
148+
const jsonValue = await fs.readFile(path, 'utf8');
149+
const replacedValue = jsonValue.replaceAll(searchValue, replaceValue);
150+
151+
await fs.writeFile(path, replacedValue);
152+
} catch (error) {
153+
handleError(error);
154+
}
155+
}
156+
86157
async function checkGitStatus(cwd: string) {
87158
if (await shouldPromptGitWarning(cwd)) {
88159
const { proceed } = await prompts({

0 commit comments

Comments
 (0)