-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.ts
More file actions
199 lines (193 loc) · 5.92 KB
/
Copy pathindex.ts
File metadata and controls
199 lines (193 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { defineCommand } from "citty";
import { createVanilla } from "./create-vanilla";
import * as p from "@clack/prompts";
import { cancelable, spinnerify } from "@solid-cli/utils/ui";
import { createStart } from "./create-start";
import { getTemplatesList, GIT_IGNORE, isValidTemplate, PROJECT_TYPES, ProjectType } from "./utils/constants";
import { detectPackageManager } from "@solid-cli/utils/package-manager";
import { existsSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { createLibrary } from "./create-library";
import { readFile, writeFile } from "node:fs/promises";
export { createVanilla, createStart, createLibrary };
export const createSolid = (version: string) =>
defineCommand({
meta: {
name: "create-solid",
description: "A CLI for scaffolding new Solid projects",
version: version,
},
args: {
"projectNamePositional": {
type: "positional",
required: false,
description: "Project name",
},
"templatePositional": {
type: "positional",
required: false,
description: "Template name",
},
"project-name": {
type: "string",
required: false,
alias: "p",
description: "Project name",
},
"template": {
type: "string",
required: false,
alias: "t",
description: "Template name",
},
"solidstart": {
type: "boolean",
required: false,
alias: "s",
description: "Create a SolidStart project",
},
"v2": {
type: "boolean",
required: false,
description: "Create a SolidStart v2 project",
},
"library": {
type: "boolean",
required: false,
alias: "l",
description: "Create a Library project",
},
"vanilla": {
type: "boolean",
required: false,
alias: "v",
description: "Create a vanilla (SolidJS + Vite) project",
},
"ts": {
type: "boolean",
required: false,
description: "Use typescript",
},
"js": {
type: "boolean",
required: false,
description: "Use javascript",
},
},
async run({
args: {
projectNamePositional,
templatePositional,
"project-name": projectNameOptional,
"template": templateOptional,
solidstart,
library,
vanilla,
ts,
js,
v2,
},
}) {
// Show prompts for any unknown arguments
let projectName = projectNamePositional ?? projectNameOptional;
let template = templatePositional ?? templateOptional;
let projectType: ProjectType | undefined = solidstart
? "start"
: vanilla
? "vanilla"
: library
? "library"
: undefined;
// False if user has selected ts, true if they have selected js, and undefined if they've done neither
let useJS = ts ? !ts : js ? js : undefined;
projectName ??= await cancelable(
p.text({ message: "Project Name", placeholder: "solid-project", defaultValue: "solid-project" }),
);
projectType ??= await cancelable(
p.select({
message: "What type of project would you like to create?",
initialValue: "start",
options: PROJECT_TYPES.map((t) => ({
value: t,
label: t === "start" ? "SolidStart" : t === "vanilla" ? "SolidJS + Vite" : "Library",
})),
}),
);
if (!projectType) return;
let useV2: string | undefined;
if (projectType === "start") {
useV2 = v2
? "v2"
: await cancelable(
p.select({
message: "Which version of SolidStart?",
initialValue: "v1",
options: [
{ value: "v1", label: "v1 (stable)" },
{ value: "v2", label: "v2 (pre-release)" },
],
}),
);
}
const isV2 = useV2 === "v2";
// Don't offer javascript if `projectType` is library
useJS ??= projectType === "library" ? false : !(await cancelable(p.confirm({ message: "Use Typescript?" })));
if (!projectType) return;
const template_opts = getTemplatesList(projectType, isV2);
template ??= await cancelable(
p.select({
message: "Which template would you like to use?",
initialValue: "ts",
options: template_opts
.filter((s) => (useJS ? s : !s.startsWith("js")))
.map((s: string) => ({ label: s, value: s })),
}),
);
if (!template) return;
// Need to transpile if the user wants Jabascript, but their selected template isn't Javascript
const transpileToJS = useJS && !template.startsWith("js");
if (projectType === "start" && isValidTemplate("start", template, isV2)) {
await spinnerify({
startText: "Creating project",
finishText: "Project created 🎉",
fn: () => createStart({ template, destination: projectName }, transpileToJS, isV2),
});
} else if (projectType === "library" && isValidTemplate("library", template)) {
await spinnerify({
startText: "Creating project",
finishText: "Project created 🎉",
fn: () => createLibrary({ destination: projectName }),
});
} else if (projectType === "vanilla" && isValidTemplate(projectType, template)) {
await spinnerify({
startText: "Creating project",
finishText: "Project created 🎉",
fn: () => createVanilla({ template, destination: projectName }, transpileToJS),
});
} else {
p.log.error(`Template ${template} is not valid for project type ${projectType}`);
process.exit(0);
}
// Add .gitignore
writeFileSync(join(projectName, ".gitignore"), GIT_IGNORE);
// Add "Created with Solid CLI" text to bottom of README
const readmePath = `${projectName}/README.md`;
if (existsSync(readmePath)) {
const contents = (await readFile(readmePath)).toString();
if (!contents.includes("This project was created with the [Solid CLI]"))
await writeFile(
readmePath,
contents +
"\n## This project was created with the [Solid CLI](https://github.com/solidjs-community/solid-cli)\n",
);
}
// Next steps..
const pM = detectPackageManager();
p.note(
(projectName === "." ? "" : `cd ${projectName}\n`) +
`${pM.name} install
${pM.name} ${pM.runScriptCommand("dev")}`,
"To get started, run:",
);
},
});