Skip to content

Commit a425a59

Browse files
committed
feat: allow creating library projects
1 parent 37acf10 commit a425a59

File tree

9 files changed

+98
-33
lines changed

9 files changed

+98
-33
lines changed

packages/create/src/create-library.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { downloadRepo, GithubFetcher } from "@begit/core";
2+
3+
export type CreateLibraryArgs = {
4+
destination: string;
5+
};
6+
export const createLibrary = ({ destination }: CreateLibraryArgs) => {
7+
return downloadRepo(
8+
{
9+
repo: { owner: "solidjs-community", name: "solid-lib-starter" },
10+
dest: destination,
11+
},
12+
GithubFetcher,
13+
);
14+
};

packages/create/src/create-start.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ export type CreateStartArgs = {
99
};
1010

1111
export const createStartTS = ({ template, destination }: CreateStartArgs) => {
12-
return downloadRepo({
13-
repo: { owner: "solidjs", name: "solid-start", subdir: `examples/${template}` },
14-
dest: destination,
15-
}, GithubFetcher);
12+
return downloadRepo(
13+
{
14+
repo: { owner: "solidjs", name: "solid-start", subdir: `examples/${template}` },
15+
dest: destination,
16+
},
17+
GithubFetcher,
18+
);
1619
};
1720

1821
export const createStartJS = async ({ template, destination }: CreateStartArgs) => {

packages/create/src/create-vanilla.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export const createVanilla = (args: CreateVanillaArgs, transpile?: boolean) => {
1515
return createVanillaTS(args);
1616
};
1717
export const createVanillaTS = async ({ template, destination }: CreateVanillaArgs) => {
18-
return await downloadRepo({ repo: { owner: "solidjs", name: "templates", subdir: template }, dest: destination }, GithubFetcher);
18+
return await downloadRepo(
19+
{ repo: { owner: "solidjs", name: "templates", subdir: template }, dest: destination },
20+
GithubFetcher,
21+
);
1922
};
2023

2124
export const createVanillaJS = async ({ template, destination }: CreateVanillaArgs) => {

packages/create/src/index.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ 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 { getTemplatesList, GIT_IGNORE, StartTemplate, VanillaTemplate } from "./utils/constants";
6+
import {
7+
getTemplatesList,
8+
GIT_IGNORE,
9+
PROJECT_TYPES,
10+
ProjectType,
11+
StartTemplate,
12+
VanillaTemplate,
13+
} from "./utils/constants";
714
import { detectPackageManager } from "@solid-cli/utils/package-manager";
815
import { insertAtEnd } from "@solid-cli/utils/fs";
916
import { existsSync, writeFileSync } from "node:fs";
1017
import { join } from "node:path";
18+
import { createLibrary } from "./create-library";
1119
export { createVanilla, createStart };
20+
1221
export const createSolid = (version: string) =>
1322
defineCommand({
1423
meta: {
@@ -46,12 +55,21 @@ export const createSolid = (version: string) =>
4655
// Show prompts for any unknown arguments
4756
let projectName: string = projectNamePositional ?? projectNameOptional;
4857
let template: string = templatePositional;
49-
let isStart: boolean = solidstart;
58+
let projectType: ProjectType | undefined = solidstart ? "start" : undefined;
5059
projectName ??= await cancelable(
5160
p.text({ message: "Project Name", placeholder: "solid-project", defaultValue: "solid-project" }),
5261
);
53-
isStart ??= await cancelable(p.confirm({ message: "Is this a SolidStart project?" }));
54-
const template_opts = await getTemplatesList(isStart);
62+
projectType ??= await cancelable(
63+
p.select({
64+
message: "What type of project would you like to create?",
65+
initialValue: "start",
66+
options: PROJECT_TYPES.map((t) => ({
67+
value: t,
68+
label: t === "start" ? "SolidStart" : t === "vanilla" ? "SolidJS + Vite" : "Library",
69+
})),
70+
}),
71+
);
72+
const template_opts = getTemplatesList(projectType);
5573
template ??= await cancelable(
5674
p.select({
5775
message: "Which template would you like to use?",
@@ -61,14 +79,25 @@ export const createSolid = (version: string) =>
6179
);
6280

6381
// Don't transpile project if it's already javascript!
64-
const transpileToJS = template.startsWith("js") ? false : !(await cancelable(p.confirm({ message: "Use Typescript?" })));
82+
const transpileToJS =
83+
projectType === "library"
84+
? false
85+
: template.startsWith("js")
86+
? false
87+
: !(await cancelable(p.confirm({ message: "Use Typescript?" })));
6588

66-
if (isStart) {
89+
if (projectType === "start") {
6790
await spinnerify({
6891
startText: "Creating project",
6992
finishText: "Project created 🎉",
7093
fn: () => createStart({ template: template as StartTemplate, destination: projectName }, transpileToJS),
7194
});
95+
} else if (projectType === "library") {
96+
await spinnerify({
97+
startText: "Creating project",
98+
finishText: "Project created 🎉",
99+
fn: () => createLibrary({ destination: projectName }),
100+
});
72101
} else {
73102
await spinnerify({
74103
startText: "Creating project",

packages/create/src/utils/constants.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const VANILLA_TEMPLATES = [
5555
"js",
5656
"js-vitest",
5757
"js-tailwindcss",
58-
] as const;
58+
] as const satisfies string[];
5959
export type VanillaTemplate = (typeof VANILLA_TEMPLATES)[number];
6060

6161
const START_TEMPLATES = [
@@ -77,14 +77,26 @@ const START_TEMPLATES = [
7777
"with-unocss",
7878
"with-vitest",
7979
"experiments",
80-
] as const;
80+
] as const satisfies string[];
8181
export type StartTemplate = (typeof START_TEMPLATES)[number];
8282

83-
export const getTemplatesList = async (isStart: boolean) => {
84-
if (isStart) {
85-
return START_TEMPLATES;
83+
export const LIBRARY_TEMPLATES = ["solid-lib-starter"] as const satisfies string[];
84+
export type LibraryTemplate = (typeof LIBRARY_TEMPLATES)[number];
85+
86+
export const PROJECT_TYPES = ["start", "vanilla", "library"] as const satisfies string[];
87+
export type ProjectType = (typeof PROJECT_TYPES)[number];
88+
89+
export function getTemplatesList(projectType: "vanilla"): StartTemplate[];
90+
export function getTemplatesList(projectType: "start"): VanillaTemplate[];
91+
export function getTemplatesList(projectType: "library"): VanillaTemplate[];
92+
export function getTemplatesList(projectType: ProjectType): VanillaTemplate[] | StartTemplate[] | LibraryTemplate[];
93+
export function getTemplatesList(projectType: ProjectType) {
94+
if (projectType === "start") {
95+
return START_TEMPLATES as StartTemplate[];
96+
} else if (projectType === "library") {
97+
return LIBRARY_TEMPLATES as LibraryTemplate[];
8698
}
87-
return VANILLA_TEMPLATES;
88-
};
99+
return VANILLA_TEMPLATES as VanillaTemplate[];
100+
}
89101

90102
//

packages/full-solid/src/bin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ intro(`\n${color.bgCyan(color.black(` Solid CLI v${packageJson.version}`))}`);
1313

1414
const main = defineCommand({
1515
meta: {
16-
description: "The full Solid CLI"
16+
description: "The full Solid CLI",
1717
},
1818
subCommands: {
1919
create: createSolid(packageJson.version),
@@ -23,7 +23,7 @@ const main = defineCommand({
2323
meta: { description: "Open the Solid Docs in your browser" },
2424
async run() {
2525
openInBrowser("https://docs.solidjs.com");
26-
p.log.success("Opened successfully")
26+
p.log.success("Opened successfully");
2727
},
2828
}),
2929
},

packages/full-solid/src/start/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { createApi, createRoute } from "@solid-cli/utils";
22
import { defineCommand } from "citty";
33
import * as p from "@clack/prompts";
44
import { green } from "picocolors";
5-
import { cancelable } from "@solid-cli/utils/ui"
5+
import { cancelable } from "@solid-cli/utils/ui";
66
export const startCommands = defineCommand({
7-
meta: { description: "Start-specific commands" }, subCommands: {
7+
meta: { description: "Start-specific commands" },
8+
subCommands: {
89
route: defineCommand({
910
meta: { description: "Creates a new route" },
1011
args: {
@@ -17,22 +18,25 @@ export const startCommands = defineCommand({
1718
type: "boolean",
1819
required: false,
1920
default: false,
20-
alias: "a"
21-
}
21+
alias: "a",
22+
},
2223
},
2324
async run({ args: { path, api } }) {
24-
path ||= await cancelable(p.text({
25-
message: "Route path", validate(value) {
26-
if (value.length === 0) return "A route path is required"
27-
},
28-
}))
25+
path ||= await cancelable(
26+
p.text({
27+
message: "Route path",
28+
validate(value) {
29+
if (value.length === 0) return "A route path is required";
30+
},
31+
}),
32+
);
2933
if (api) {
3034
await createApi(path as string);
3135
} else {
3236
await createRoute(path as string);
3337
}
34-
p.log.success(`Route ${green(path as string)} successfully created!`)
38+
p.log.success(`Route ${green(path as string)} successfully created!`);
3539
},
3640
}),
37-
}
41+
},
3842
});

packages/utils/src/ui/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { cancelable } from "./cancelable";
22
import { spinnerify } from "./spinnerify";
3-
export { cancelable, spinnerify }
3+
export { cancelable, spinnerify };

packages/utils/src/ui/spinnerify.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ export async function spinnerify<T>(spinners: SpinnerItem<any>[] | SpinnerItem<T
1616
s.stop(finishText);
1717
}
1818
return results.length === 1 ? results[0] : results;
19-
}
19+
}

0 commit comments

Comments
 (0)