Skip to content

Commit b49a661

Browse files
committed
fix: reprompt on invalid folder names
1 parent 0bc2a88 commit b49a661

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/engine/TemplateEngine.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ export abstract class TemplateEngine extends QuickAddEngine {
8787
}
8888

8989
const selection = await this.promptUntilAllowed(context);
90-
if (selection.isEmpty) return "";
91-
92-
await this.ensureFolderExists(selection);
93-
return selection.resolved;
90+
return selection.isEmpty ? "" : selection.resolved;
9491
}
9592

9693
private buildFolderSelectionContext(
@@ -219,6 +216,16 @@ export abstract class TemplateEngine extends QuickAddEngine {
219216
continue;
220217
}
221218

219+
try {
220+
await this.ensureFolderExists(selection);
221+
} catch (error) {
222+
if (this.isInvalidPathError(error)) {
223+
this.showInvalidFolderNotice(error);
224+
continue;
225+
}
226+
throw error;
227+
}
228+
222229
return selection;
223230
}
224231
}
@@ -240,14 +247,35 @@ export abstract class TemplateEngine extends QuickAddEngine {
240247
return "";
241248
}
242249

243-
await this.ensureFolderExists(selection);
250+
try {
251+
await this.ensureFolderExists(selection);
252+
} catch (error) {
253+
if (this.isInvalidPathError(error)) {
254+
this.showInvalidFolderNotice(error);
255+
return "";
256+
}
257+
throw error;
258+
}
244259
return selection.resolved;
245260
}
246261

247262
private normalizeFolderPath(path: string): string {
248263
return path.trim().replace(/^\/+/, "").replace(/\/+$/, "");
249264
}
250265

266+
private isInvalidPathError(error: unknown): error is Error {
267+
if (!(error instanceof Error)) return false;
268+
return (
269+
error.message.includes("File name cannot contain") ||
270+
error.message.includes("File name cannot be empty") ||
271+
error.message.includes("File name cannot start with")
272+
);
273+
}
274+
275+
private showInvalidFolderNotice(error: Error): void {
276+
new Notice(error.message);
277+
}
278+
251279
private isPathAllowed(path: string, roots: string[]): boolean {
252280
const normalizedPath = this.normalizeFolderPath(path);
253281
for (const root of roots) {

0 commit comments

Comments
 (0)