Skip to content

Commit c410450

Browse files
authored
Merge pull request #300 from mrzachnugent/@zach/299-pnpm-init
fix(#299): add missing files for pnpm + make new project default and prompt for existing
2 parents 724a607 + 0d365db commit c410450

File tree

6 files changed

+140
-65
lines changed

6 files changed

+140
-65
lines changed

apps/cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@react-native-reusables/cli",
3-
"version": "0.3.0",
3+
"version": "0.3.3",
44
"description": "Add react-native-reusables to your project.",
55
"publishConfig": {
66
"access": "public"
@@ -33,7 +33,7 @@
3333
"exports": "./dist/index.js",
3434
"bin": "./dist/index.js",
3535
"scripts": {
36-
"gen": "tsx scripts/generate-source-files.ts",
36+
"gen": "rm -rf __generated && tsx scripts/generate-source-files.ts",
3737
"dev": "pnpm gen && tsup --watch",
3838
"build": "pnpm gen && tsup",
3939
"typecheck": "tsc --noEmit",

apps/cli/scripts/generate-source-files.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { copyFolder } from '../src/utils/copy-folder';
88

99
async function main() {
1010
for (const template of TEMPLATES) {
11-
await copyFolder(template.path, path.join('__generated', template.name));
11+
await copyFolder(template.path, path.join('__generated', template.name), {
12+
ignore: ['.expo', 'node_modules'],
13+
renameTemplateFiles: false,
14+
});
1215
}
1316
for (const comp of COMPONENTS) {
1417
if (Array.isArray(comp.paths)) {

apps/cli/src/commands/init.ts

Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -71,76 +71,90 @@ export const init = new Command()
7171
});
7272

7373
async function validateProjectDirectory(cwd: string) {
74-
if (!existsSync(cwd) || !existsSync(path.join(cwd, 'package.json'))) {
75-
const { proceed } = await prompts({
76-
type: 'confirm',
77-
name: 'proceed',
78-
message: 'No package.json found. Would you like to create a new project?',
74+
if (existsSync(cwd) && existsSync(path.join(cwd, 'package.json'))) {
75+
const { option } = await prompts({
76+
type: 'select',
77+
name: 'option',
78+
message:
79+
'Package.json found. How would you like to proceed? (Selecting "Cancel" will exit the process)',
80+
choices: [
81+
{ title: 'Automatically configure your existing project', value: 'existing-project' },
82+
{ title: 'Init new project', value: 'new-project' },
83+
{ title: 'Cancel', value: 'cancel' },
84+
],
7985
initial: false,
8086
});
8187

82-
if (!proceed) {
83-
logger.info('Initialization cancelled.');
88+
if (option === 'cancel') {
89+
logger.info('Installation cancelled.');
8490
process.exit(0);
8591
}
8692

87-
const { projectName } = await prompts({
88-
type: 'text',
89-
name: 'projectName',
90-
message: `What is the name of your project?`,
91-
initial: 'starter-base',
92-
});
93-
94-
const spinner = ora(`Initializing ${projectName}...`).start();
95-
96-
const projectPath = path.join(cwd, projectName);
97-
if (!existsSync(projectPath)) {
98-
await fs.mkdir(projectPath, { recursive: true });
93+
if (option === 'existing-project') {
94+
return;
9995
}
96+
}
97+
98+
const { projectName } = await prompts({
99+
type: 'text',
100+
name: 'projectName',
101+
message: `What is the name of your project?`,
102+
initial: 'starter-base',
103+
});
100104

101-
await copyFolder(path.join(fileDir, '../__generated/starter-base'), projectPath);
105+
const { packageManager } = await prompts({
106+
type: 'select',
107+
name: 'packageManager',
108+
message: 'Which package manager would you like to use?',
109+
choices: [
110+
{ title: 'npm', value: 'npm' },
111+
{ title: 'yarn', value: 'yarn' },
112+
{ title: 'pnpm', value: 'pnpm' },
113+
{ title: 'bun', value: 'bun' },
114+
],
115+
});
102116

103-
await Promise.all([
104-
replaceAllInJsonFile(path.join(projectPath, 'app.json'), 'starter-base', projectName),
105-
replaceAllInJsonFile(
106-
path.join(projectPath, 'package.json'),
107-
'@rnr/starter-base',
108-
projectName
109-
),
110-
]);
117+
const spinner = ora(`Initializing ${projectName}...`).start();
111118

112-
spinner.stop();
113-
const { packageManager } = await prompts({
114-
type: 'select',
115-
name: 'packageManager',
116-
message: 'Which package manager would you like to use?',
117-
choices: [
118-
{ title: 'npm', value: 'npm' },
119-
{ title: 'yarn', value: 'yarn' },
120-
{ title: 'pnpm', value: 'pnpm' },
121-
{ title: 'bun', value: 'bun' },
122-
],
123-
});
119+
const projectPath = path.join(cwd, projectName);
120+
if (!existsSync(projectPath)) {
121+
await fs.mkdir(projectPath, { recursive: true });
122+
}
124123

125-
spinner.start('Installing dependencies...');
126-
await execa(packageManager, ['install'], {
127-
cwd: projectPath,
128-
});
129-
spinner.text = 'Verifying and updating any invalid package versions if needed...';
130-
await execa('npx', ['expo', 'install', '--fix'], {
131-
cwd: projectPath,
132-
});
124+
const filesToIgnore = [];
133125

134-
spinner.succeed('New project initialized successfully!');
135-
console.log(`\nTo get started, run the following commands:\n`);
136-
console.log(chalk.cyan(`cd ${projectName}`));
137-
console.log(
138-
chalk.cyan(
139-
`${packageManager} ${packageManager === 'npm' || packageManager === 'bun' ? 'run ' : ''}dev`
140-
)
141-
);
142-
process.exit(0);
126+
if (packageManager !== 'pnpm') {
127+
filesToIgnore.push('npmrc-template');
143128
}
129+
130+
await copyFolder(path.join(fileDir, '../__generated/starter-base'), projectPath, {
131+
ignore: filesToIgnore,
132+
renameTemplateFiles: true,
133+
});
134+
135+
await Promise.all([
136+
replaceAllInJsonFile(path.join(projectPath, 'app.json'), 'starter-base', projectName),
137+
replaceAllInJsonFile(path.join(projectPath, 'package.json'), '@rnr/starter-base', projectName),
138+
]);
139+
140+
spinner.start('Installing dependencies...');
141+
await execa(packageManager, ['install'], {
142+
cwd: projectPath,
143+
});
144+
spinner.text = 'Verifying and updating any invalid package versions if needed...';
145+
await execa('npx', ['expo', 'install', '--fix'], {
146+
cwd: projectPath,
147+
});
148+
149+
spinner.succeed('New project initialized successfully!');
150+
console.log(`\nTo get started, run the following commands:\n`);
151+
console.log(chalk.cyan(`cd ${projectName}`));
152+
console.log(
153+
chalk.cyan(
154+
`${packageManager} ${packageManager === 'npm' || packageManager === 'bun' ? 'run ' : ''}dev`
155+
)
156+
);
157+
process.exit(0);
144158
}
145159

146160
async function replaceAllInJsonFile(path: string, searchValue: string, replaceValue: string) {

apps/cli/src/utils/copy-folder.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { existsSync, promises as fs } from 'fs';
22
import path from 'path';
33

4-
export async function copyFolder(src: string, dest: string) {
4+
export async function copyFolder(
5+
src: string,
6+
dest: string,
7+
options: { ignore: string[]; renameTemplateFiles: boolean } = {
8+
ignore: [],
9+
renameTemplateFiles: false,
10+
}
11+
) {
512
if (!existsSync(src)) {
613
throw new Error(`Source folder does not exist: ${src}`);
714
}
@@ -16,12 +23,20 @@ export async function copyFolder(src: string, dest: string) {
1623
const srcPath = path.join(src, entry.name);
1724
const destPath = path.join(dest, entry.name);
1825

26+
if (options.ignore.includes(entry.name)) {
27+
continue;
28+
}
29+
1930
if (entry.isDirectory()) {
20-
// Recursively copy subdirectories
21-
await copyFolder(srcPath, destPath);
31+
await copyFolder(srcPath, destPath, options);
2232
} else if (entry.isFile()) {
23-
// Copy files
24-
await fs.copyFile(srcPath, destPath);
33+
if (options.renameTemplateFiles && entry.name === 'gitignore-template') {
34+
await fs.copyFile(srcPath, path.join(dest, '.gitignore'));
35+
} else if (options.renameTemplateFiles && entry.name === 'npmrc-template') {
36+
await fs.copyFile(srcPath, path.join(dest, '.npmrc'));
37+
} else {
38+
await fs.copyFile(srcPath, destPath);
39+
}
2540
}
2641
}
2742
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files
2+
3+
# dependencies
4+
node_modules/
5+
6+
# Expo
7+
.expo/
8+
dist/
9+
web-build/
10+
11+
# Native
12+
*.orig.*
13+
*.jks
14+
*.p8
15+
*.p12
16+
*.key
17+
*.mobileprovision
18+
19+
# Metro
20+
.metro-health-check*
21+
22+
# debug
23+
npm-debug.*
24+
yarn-debug.*
25+
yarn-error.*
26+
27+
# macOS
28+
.DS_Store
29+
*.pem
30+
31+
# local env files
32+
.env*.local
33+
34+
# typescript
35+
*.tsbuildinfo
36+
37+
# @generated expo-cli sync-2b81b286409207a5da26e14c78851eb30d8ccbdb
38+
# The following patterns were generated by expo-cli
39+
40+
expo-env.d.ts
41+
# @end expo-cli
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node-linker=hoisted
2+
enable-pre-post-scripts=true

0 commit comments

Comments
 (0)