Skip to content

Commit 176a363

Browse files
authored
Merge branch 'main' into @satya164/configure-tools
2 parents a7009be + eb4b06b commit 176a363

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
@@ -196,6 +196,33 @@ export async function createQuestions({
196196
// Ignore error
197197
}
198198

199+
const validateDirectory = (input: string) => {
200+
if (!input) {
201+
return 'Cannot be empty';
202+
}
203+
204+
const targetPath = path.join(process.cwd(), input);
205+
206+
if (fs.pathExistsSync(targetPath)) {
207+
const stat = fs.statSync(targetPath);
208+
209+
if (!stat.isDirectory()) {
210+
return 'Path exists and is not a directory';
211+
}
212+
213+
const files = fs.readdirSync(targetPath);
214+
215+
const isEmpty =
216+
files.length === 0 || (files.length === 1 && files[0] === '.git');
217+
218+
if (!isEmpty) {
219+
return 'Directory already exists';
220+
}
221+
}
222+
223+
return true;
224+
};
225+
199226
const questions: Question<keyof PromptAnswers>[] = [
200227
{
201228
type:
@@ -209,7 +236,17 @@ export async function createQuestions({
209236
default: false,
210237
},
211238
{
212-
type: (_, answers) => (name && !(answers.local ?? local) ? null : 'text'),
239+
type: (_, answers) => {
240+
if (
241+
name &&
242+
!(answers.local ?? local) &&
243+
validateDirectory(name) === true
244+
) {
245+
return null;
246+
}
247+
248+
return 'text';
249+
},
213250
name: 'directory',
214251
message: `Where do you want to create the library?`,
215252
initial: (_, answers) => {
@@ -219,32 +256,7 @@ export async function createQuestions({
219256

220257
return name ?? '';
221258
},
222-
validate: (input) => {
223-
if (!input) {
224-
return 'Cannot be empty';
225-
}
226-
227-
const targetPath = path.join(process.cwd(), input);
228-
229-
if (fs.pathExistsSync(targetPath)) {
230-
const stat = fs.statSync(targetPath);
231-
232-
if (!stat.isDirectory()) {
233-
return 'Path exists and is not a directory';
234-
}
235-
236-
const files = fs.readdirSync(targetPath);
237-
238-
const isEmpty =
239-
files.length === 0 || (files.length === 1 && files[0] === '.git');
240-
241-
if (!isEmpty) {
242-
return 'Directory already exists';
243-
}
244-
}
245-
246-
return true;
247-
},
259+
validate: validateDirectory,
248260
default: name,
249261
},
250262
{

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)