Skip to content

Commit eb4b06b

Browse files
committed
fix: validate directory name before skipping the prompt
1 parent 7f5952f commit eb4b06b

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

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

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,33 @@ export async function createQuestions({
189189
// Ignore error
190190
}
191191

192+
const validateDirectory = (input: string) => {
193+
if (!input) {
194+
return 'Cannot be empty';
195+
}
196+
197+
const targetPath = path.join(process.cwd(), input);
198+
199+
if (fs.pathExistsSync(targetPath)) {
200+
const stat = fs.statSync(targetPath);
201+
202+
if (!stat.isDirectory()) {
203+
return 'Path exists and is not a directory';
204+
}
205+
206+
const files = fs.readdirSync(targetPath);
207+
208+
const isEmpty =
209+
files.length === 0 || (files.length === 1 && files[0] === '.git');
210+
211+
if (!isEmpty) {
212+
return 'Directory already exists';
213+
}
214+
}
215+
216+
return true;
217+
};
218+
192219
const questions: Question<keyof PromptAnswers>[] = [
193220
{
194221
type:
@@ -202,7 +229,17 @@ export async function createQuestions({
202229
default: false,
203230
},
204231
{
205-
type: (_, answers) => (name && !(answers.local ?? local) ? null : 'text'),
232+
type: (_, answers) => {
233+
if (
234+
name &&
235+
!(answers.local ?? local) &&
236+
validateDirectory(name) === true
237+
) {
238+
return null;
239+
}
240+
241+
return 'text';
242+
},
206243
name: 'directory',
207244
message: `Where do you want to create the library?`,
208245
initial: (_, answers) => {
@@ -212,32 +249,7 @@ export async function createQuestions({
212249

213250
return name ?? '';
214251
},
215-
validate: (input) => {
216-
if (!input) {
217-
return 'Cannot be empty';
218-
}
219-
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-
}
237-
}
238-
239-
return true;
240-
},
252+
validate: validateDirectory,
241253
default: name,
242254
},
243255
{

packages/create-react-native-library/src/utils/prompt.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,11 @@ export async function prompt<
9595
// @ts-expect-error assume the passed value is correct
9696
defaultAnswers[question.name] = question.default;
9797

98-
// Don't prompt questions with a default value when not interactive
99-
if (!interactive) {
98+
// Don't prompt questions with a valid default value when not interactive
99+
if (
100+
!interactive &&
101+
question.validate?.(String(question.default)) !== false
102+
) {
100103
continue;
101104
}
102105
}

0 commit comments

Comments
 (0)