Skip to content

Commit 28cb5dd

Browse files
atilafassinaTommypop2
authored andcommitted
add support for v2 CLI flag
1 parent e2e2f7d commit 28cb5dd

3 files changed

Lines changed: 91 additions & 31 deletions

File tree

packages/create/src/create-start.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,36 @@ import { downloadRepo, GithubFetcher } from "@begit/core";
22
import { join } from "path";
33
import { writeFileSync } from "fs";
44
import { handleTSConversion } from "./utils/ts-conversion";
5-
import { GIT_IGNORE, StartTemplate } from "./utils/constants";
5+
import { GIT_IGNORE, StartTemplate, StartTemplateV2 } from "./utils/constants";
66
export type CreateStartArgs = {
7-
template: StartTemplate;
7+
template: StartTemplate | StartTemplateV2;
88
destination: string;
99
};
1010

11-
export const createStartTS = ({ template, destination }: CreateStartArgs) => {
11+
export const createStartTS = ({ template, destination }: CreateStartArgs, v2?: boolean) => {
12+
const subdir = v2 ? `solid-start/v2/${template}` : `solid-start/${template}`;
1213
return downloadRepo(
1314
{
14-
repo: { owner: "solidjs", name: "templates", subdir: `solid-start/${template}` },
15+
repo: { owner: "solidjs", name: "templates", subdir },
1516
dest: destination,
1617
},
1718
GithubFetcher,
1819
);
1920
};
2021

21-
export const createStartJS = async ({ template, destination }: CreateStartArgs) => {
22+
export const createStartJS = async ({ template, destination }: CreateStartArgs, v2?: boolean) => {
2223
// Create typescript project in `<destination>/.project`
2324
// then transpile this to javascript and clean up
2425
const tempDir = join(destination, ".project");
25-
await createStartTS({ template, destination: tempDir });
26+
await createStartTS({ template, destination: tempDir }, v2);
2627
await handleTSConversion(tempDir, destination);
2728
// Add .gitignore
2829
writeFileSync(join(destination, ".gitignore"), GIT_IGNORE);
2930
};
3031

31-
export const createStart = (args: CreateStartArgs, transpile?: boolean) => {
32+
export const createStart = (args: CreateStartArgs, transpile?: boolean, v2?: boolean) => {
3233
if (transpile) {
33-
return createStartJS(args);
34+
return createStartJS(args, v2);
3435
}
35-
return createStartTS(args);
36+
return createStartTS(args, v2);
3637
};

packages/create/src/index.ts

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,7 @@ import { createVanilla } from "./create-vanilla";
33
import * as p from "@clack/prompts";
44
import { cancelable, spinnerify } from "@solid-cli/utils/ui";
55
import { createStart } from "./create-start";
6-
import {
7-
getTemplatesList,
8-
GIT_IGNORE,
9-
isValidTemplate,
10-
PROJECT_TYPES,
11-
ProjectType,
12-
} from "./utils/constants";
6+
import { getTemplatesList, GIT_IGNORE, isValidTemplate, PROJECT_TYPES, ProjectType } from "./utils/constants";
137
import { detectPackageManager } from "@solid-cli/utils/package-manager";
148
import { existsSync, writeFileSync } from "node:fs";
159
import { join } from "node:path";
@@ -53,6 +47,11 @@ export const createSolid = (version: string) =>
5347
alias: "s",
5448
description: "Create a SolidStart project",
5549
},
50+
"v2": {
51+
type: "boolean",
52+
required: false,
53+
description: "Create a SolidStart v2 project",
54+
},
5655
"library": {
5756
type: "boolean",
5857
required: false,
@@ -87,12 +86,19 @@ export const createSolid = (version: string) =>
8786
vanilla,
8887
ts,
8988
js,
89+
v2,
9090
},
9191
}) {
9292
// Show prompts for any unknown arguments
9393
let projectName = projectNamePositional ?? projectNameOptional;
9494
let template = templatePositional ?? templateOptional;
95-
let projectType: ProjectType | undefined = solidstart ? "start" : (vanilla ? "vanilla" : (library ? "library" : undefined));
95+
let projectType: ProjectType | undefined = solidstart
96+
? "start"
97+
: vanilla
98+
? "vanilla"
99+
: library
100+
? "library"
101+
: undefined;
96102
// False if user has selected ts, true if they have selected js, and undefined if they've done neither
97103
let useJS = ts ? !ts : js ? js : undefined;
98104
projectName ??= await cancelable(
@@ -108,10 +114,30 @@ export const createSolid = (version: string) =>
108114
})),
109115
}),
110116
);
117+
if (!projectType) return;
118+
119+
let useV2: string | undefined;
120+
if (projectType === "start") {
121+
useV2 = v2
122+
? "v2"
123+
: await cancelable(
124+
p.select({
125+
message: "Which version of SolidStart?",
126+
initialValue: "v2",
127+
options: [
128+
{ value: "v2", label: "v2 (pre-release, recommended)" },
129+
{ value: "v1", label: "v1 (stable)" },
130+
],
131+
}),
132+
);
133+
}
134+
const isV2 = useV2 === "v2";
135+
111136
// Don't offer javascript if `projectType` is library
112137
useJS ??= projectType === "library" ? false : !(await cancelable(p.confirm({ message: "Use Typescript?" })));
113138

114-
const template_opts = getTemplatesList(projectType);
139+
if (!projectType) return;
140+
const template_opts = getTemplatesList(projectType, isV2);
115141
template ??= await cancelable(
116142
p.select({
117143
message: "Which template would you like to use?",
@@ -122,13 +148,15 @@ export const createSolid = (version: string) =>
122148
}),
123149
);
124150

151+
if (!template) return;
152+
125153
// Need to transpile if the user wants Jabascript, but their selected template isn't Javascript
126154
const transpileToJS = useJS && !template.startsWith("js");
127-
if (projectType === "start" && isValidTemplate("start", template)) {
155+
if (projectType === "start" && isValidTemplate("start", template, isV2)) {
128156
await spinnerify({
129157
startText: "Creating project",
130158
finishText: "Project created 🎉",
131-
fn: () => createStart({ template, destination: projectName }, transpileToJS),
159+
fn: () => createStart({ template, destination: projectName }, transpileToJS, isV2),
132160
});
133161
} else if (projectType === "library" && isValidTemplate("library", template)) {
134162
await spinnerify({
@@ -142,8 +170,7 @@ export const createSolid = (version: string) =>
142170
finishText: "Project created 🎉",
143171
fn: () => createVanilla({ template, destination: projectName }, transpileToJS),
144172
});
145-
}
146-
else {
173+
} else {
147174
p.log.error(`Template ${template} is not valid for project type ${projectType}`);
148175
process.exit(0);
149176
}
@@ -154,7 +181,11 @@ export const createSolid = (version: string) =>
154181
if (existsSync(readmePath)) {
155182
const contents = (await readFile(readmePath)).toString();
156183
if (!contents.includes("This project was created with the [Solid CLI]"))
157-
await writeFile(readmePath, contents + "\n## This project was created with the [Solid CLI](https://github.com/solidjs-community/solid-cli)\n")
184+
await writeFile(
185+
readmePath,
186+
contents +
187+
"\n## This project was created with the [Solid CLI](https://github.com/solidjs-community/solid-cli)\n",
188+
);
158189
}
159190
// Next steps..
160191
const pM = detectPackageManager();

packages/create/src/utils/constants.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ const START_TEMPLATES = [
8383

8484
export type StartTemplate = (typeof START_TEMPLATES)[number];
8585

86+
const START_TEMPLATES_V2 = [
87+
"basic",
88+
"bare",
89+
"hackernews",
90+
"notes",
91+
"with-auth",
92+
"with-drizzle",
93+
"with-mdx",
94+
"with-prisma",
95+
"with-solid-styled",
96+
"with-tailwindcss",
97+
"with-trpc",
98+
"with-unocss",
99+
"with-vitest",
100+
] as const satisfies string[];
101+
102+
export type StartTemplateV2 = (typeof START_TEMPLATES_V2)[number];
103+
86104
/**Supported Library Templates */
87105
export const LIBRARY_TEMPLATES = ["solid-lib-starter"] as const satisfies string[];
88106
export type LibraryTemplate = (typeof LIBRARY_TEMPLATES)[number];
@@ -94,12 +112,18 @@ export type ProjectType = (typeof PROJECT_TYPES)[number];
94112
* Fetches the template list for the project type given
95113
* @param projectType type of project
96114
*/
97-
export function getTemplatesList(projectType: "vanilla"): StartTemplate[];
98-
export function getTemplatesList(projectType: "start"): VanillaTemplate[];
99-
export function getTemplatesList(projectType: "library"): VanillaTemplate[];
100-
export function getTemplatesList(projectType: ProjectType): VanillaTemplate[] | StartTemplate[] | LibraryTemplate[];
101-
export function getTemplatesList(projectType: ProjectType) {
115+
export function getTemplatesList(projectType: "vanilla", v2?: boolean): VanillaTemplate[];
116+
export function getTemplatesList(projectType: "start", v2?: boolean): StartTemplate[] | StartTemplateV2[];
117+
export function getTemplatesList(projectType: "library", v2?: boolean): LibraryTemplate[];
118+
export function getTemplatesList(
119+
projectType: ProjectType,
120+
v2?: boolean,
121+
): VanillaTemplate[] | StartTemplate[] | StartTemplateV2[] | LibraryTemplate[];
122+
export function getTemplatesList(projectType: ProjectType, v2?: boolean) {
102123
if (projectType === "start") {
124+
if (v2) {
125+
return START_TEMPLATES_V2 as unknown as StartTemplateV2[];
126+
}
103127
return START_TEMPLATES as StartTemplate[];
104128
} else if (projectType === "library") {
105129
return LIBRARY_TEMPLATES as LibraryTemplate[];
@@ -114,9 +138,13 @@ export function getTemplatesList(projectType: ProjectType) {
114138
* @returns the template string if it is valid, undefined if not
115139
*/
116140
export function isValidTemplate(type: "vanilla", maybe_template: string): maybe_template is VanillaTemplate;
117-
export function isValidTemplate(type: "start", maybe_template: string): maybe_template is StartTemplate;
141+
export function isValidTemplate(
142+
type: "start",
143+
maybe_template: string,
144+
v2?: boolean,
145+
): maybe_template is StartTemplate | StartTemplateV2;
118146
export function isValidTemplate(type: "library", maybe_template: string): maybe_template is LibraryTemplate;
119-
export function isValidTemplate(type: ProjectType, maybe_template: string) {
120-
const templates = getTemplatesList(type);
147+
export function isValidTemplate(type: ProjectType, maybe_template: string, v2?: boolean) {
148+
const templates = getTemplatesList(type, v2);
121149
return templates.find((t) => t === maybe_template) !== undefined;
122150
}

0 commit comments

Comments
 (0)