Skip to content

Commit a933e02

Browse files
authored
fix: allow project creation in directories with only .git folder (#866)
<!-- Please provide enough information so that others can review your pull request. --> <!-- Keep pull requests small and focused on a single change. --> ### Summary #### Problem Currently, the library validation prevents creating projects in directories that contain any files, including `.git` folders. This creates a frustrating user experience when: 1. Users clone an empty GitHub repository 2. The directory contains only a `.git` folder 3. The validation fails with "Directory already exists" error This is a common workflow where developers create a GitHub repo first, then clone it locally to set up their React Native library. <img width="838" alt="image" src="https://github.com/user-attachments/assets/85813b62-6954-4775-9794-c065de446f79" /> #### Solution - Treat directories as "empty" if they contain no files OR only a `.git` folder - Allow project creation in such directories This follows the same pattern used by Vite's `create-vite` tool: - [Vite source code](https://github.com/vitejs/vite/blob/b135918b91e8381c50bd2d076d40e9a65fe68bfe/packages/create-vite/src/index.ts#L589) ### Test plan 1. yarn install 2. cd packages/create-react-native-library 3. yarn prepare 4. run `./bin/create-react-native-library` Tested scenarios: - [x] Empty directory - works as before - [x] Directory with only `.git` - now works ✅ - [x] Directory with other files - still blocked as expected - [x] Non-existent directory - works as before <!-- List the steps with which we can test this change. Provide screenshots if this changes anything visual. -->
1 parent 05ece3e commit a933e02

File tree

1 file changed

+17
-2
lines changed
  • packages/create-react-native-library/src

1 file changed

+17
-2
lines changed

packages/create-react-native-library/src/input.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,23 @@ export async function createQuestions({
217217
return 'Cannot be empty';
218218
}
219219

220-
if (fs.pathExistsSync(path.join(process.cwd(), input))) {
221-
return 'Directory already exists';
220+
const targetPath = path.join(process.cwd(), input);
221+
222+
if (fs.pathExistsSync(targetPath)) {
223+
const stat = fs.statSync(targetPath);
224+
225+
if (!stat.isDirectory()) {
226+
return 'Path exists and is not a directory';
227+
}
228+
229+
const files = fs.readdirSync(targetPath);
230+
231+
const isEmpty =
232+
files.length === 0 || (files.length === 1 && files[0] === '.git');
233+
234+
if (!isEmpty) {
235+
return 'Directory already exists';
236+
}
222237
}
223238

224239
return true;

0 commit comments

Comments
 (0)