Skip to content

Commit 86a9a40

Browse files
committed
feat: check that template is valid before attempting to create it
1 parent 2f2ce23 commit 86a9a40

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

packages/create/src/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ import { createStart } from "./create-start";
66
import {
77
getTemplatesList,
88
GIT_IGNORE,
9+
isValidTemplate,
910
PROJECT_TYPES,
1011
ProjectType,
11-
StartTemplate,
12-
VanillaTemplate,
1312
} from "./utils/constants";
1413
import { detectPackageManager } from "@solid-cli/utils/package-manager";
1514
import { existsSync, writeFileSync } from "node:fs";
@@ -111,25 +110,29 @@ export const createSolid = (version: string) =>
111110

112111
// Need to transpile if the user wants Jabascript, but their selected template isn't Javascript
113112
const transpileToJS = useJS && !template.startsWith("js");
114-
if (projectType === "start") {
113+
if (projectType === "start" && isValidTemplate("start", template)) {
115114
await spinnerify({
116115
startText: "Creating project",
117116
finishText: "Project created 🎉",
118-
fn: () => createStart({ template: template as StartTemplate, destination: projectName }, transpileToJS),
117+
fn: () => createStart({ template, destination: projectName }, transpileToJS),
119118
});
120-
} else if (projectType === "library") {
119+
} else if (projectType === "library" && isValidTemplate("library", template)) {
121120
await spinnerify({
122121
startText: "Creating project",
123122
finishText: "Project created 🎉",
124123
fn: () => createLibrary({ destination: projectName }),
125124
});
126-
} else {
125+
} else if (projectType === "vanilla" && isValidTemplate(projectType, template)) {
127126
await spinnerify({
128127
startText: "Creating project",
129128
finishText: "Project created 🎉",
130-
fn: () => createVanilla({ template: template as VanillaTemplate, destination: projectName }, transpileToJS),
129+
fn: () => createVanilla({ template, destination: projectName }, transpileToJS),
131130
});
132131
}
132+
else {
133+
p.log.error(`Template ${template} is not valid for project type ${projectType}`);
134+
process.exit(0);
135+
}
133136
// Add .gitignore
134137
writeFileSync(join(projectName, ".gitignore"), GIT_IGNORE);
135138
// Add "Created with Solid CLI" text to bottom of README

packages/create/src/utils/constants.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ export function getTemplatesList(projectType: ProjectType) {
112112
* @param maybe_template the template string to test
113113
* @returns the template string if it is valid, undefined if not
114114
*/
115-
export function isValidTemplate(type: "vanilla", maybe_template: string): VanillaTemplate;
116-
export function isValidTemplate(type: "start", maybe_template: string): StartTemplate;
117-
export function isValidTemplate(type: "library", maybe_template: string): LibraryTemplate;
115+
export function isValidTemplate(type: "vanilla", maybe_template: string): maybe_template is VanillaTemplate;
116+
export function isValidTemplate(type: "start", maybe_template: string): maybe_template is StartTemplate;
117+
export function isValidTemplate(type: "library", maybe_template: string): maybe_template is LibraryTemplate;
118118
export function isValidTemplate(type: ProjectType, maybe_template: string) {
119119
const templates = getTemplatesList(type);
120-
return templates.find(t => t === maybe_template)
120+
return templates.find(t => t === maybe_template) !== undefined
121121
}

0 commit comments

Comments
 (0)