-
-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathaddons-setup.ts
More file actions
181 lines (154 loc) · 5.19 KB
/
addons-setup.ts
File metadata and controls
181 lines (154 loc) · 5.19 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
import path from "node:path";
import { Result } from "better-result";
import fs from "fs-extra";
import pc from "picocolors";
import { desktopWebFrontends, type ProjectConfig } from "../../types";
import { addPackageDependency } from "../../utils/add-package-deps";
import { AddonSetupError, UserCancelledError } from "../../utils/errors";
import { cliConsola } from "../../utils/terminal-output";
import { setupFumadocs } from "./fumadocs-setup";
import { setupMcp } from "./mcp-setup";
import { setupOxc } from "./oxc-setup";
import { setupSkills } from "./skills-setup";
import { setupStarlight } from "./starlight-setup";
import { setupTauri } from "./tauri-setup";
import { setupTui } from "./tui-setup";
import { setupUltracite } from "./ultracite-setup";
import { setupWxt } from "./wxt-setup";
// Helper to run setup and handle Result
async function runSetup<T, E extends AddonSetupError | UserCancelledError>(
setupFn: () => Promise<Result<T, E>>,
): Promise<void> {
const result = await setupFn();
if (result.isErr()) {
// Re-throw user cancellation to propagate up
if (UserCancelledError.is(result.error)) {
throw result.error;
}
// Log other errors but don't fail the overall project creation
cliConsola.error(pc.red(result.error.message));
}
}
async function runAddonStep(addon: string, step: () => Promise<void>): Promise<void> {
const result = await Result.tryPromise({
try: async () => step(),
catch: (e) =>
new AddonSetupError({
addon,
message: `Failed to set up ${addon}: ${e instanceof Error ? e.message : String(e)}`,
cause: e,
}),
});
if (result.isErr()) {
cliConsola.error(pc.red(result.error.message));
}
}
export async function setupAddons(config: ProjectConfig) {
const { addons, frontend, projectDir } = config;
const hasWebFrontend = frontend.some((value) =>
(desktopWebFrontends as readonly string[]).includes(value),
);
if (addons.includes("tauri") && hasWebFrontend) {
await runSetup(() => setupTauri(config));
}
const hasUltracite = addons.includes("ultracite");
const hasBiome = addons.includes("biome");
const hasHusky = addons.includes("husky");
const hasLefthook = addons.includes("lefthook");
const hasOxc = addons.includes("oxc");
if (hasUltracite) {
const gitHooks: string[] = [];
if (hasHusky) gitHooks.push("husky");
if (hasLefthook) gitHooks.push("lefthook");
await runSetup(() => setupUltracite(config, gitHooks));
} else {
if (hasBiome) {
await runAddonStep("biome", () => setupBiome(projectDir));
}
if (hasOxc) {
await runSetup(() => setupOxc(projectDir, config.packageManager));
}
if (hasHusky || hasLefthook) {
let linter: "biome" | "oxc" | undefined;
if (hasOxc) {
linter = "oxc";
} else if (hasBiome) {
linter = "biome";
}
if (hasHusky) {
await runAddonStep("husky", () => setupHusky(projectDir, linter));
}
if (hasLefthook) {
await runAddonStep("lefthook", () => setupLefthook(projectDir));
}
}
}
if (addons.includes("starlight")) {
await runSetup(() => setupStarlight(config));
}
if (addons.includes("fumadocs")) {
await runSetup(() => setupFumadocs(config));
}
if (addons.includes("opentui")) {
await runSetup(() => setupTui(config));
}
if (addons.includes("wxt")) {
await runSetup(() => setupWxt(config));
}
if (addons.includes("skills")) {
await runSetup(() => setupSkills(config));
}
if (addons.includes("mcp")) {
await runSetup(() => setupMcp(config));
}
}
export async function setupBiome(projectDir: string) {
await addPackageDependency({
devDependencies: ["@biomejs/biome"],
projectDir,
});
const packageJsonPath = path.join(projectDir, "package.json");
if (await fs.pathExists(packageJsonPath)) {
const packageJson = await fs.readJson(packageJsonPath);
packageJson.scripts = {
...packageJson.scripts,
check: "biome check --write .",
};
await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
}
}
export async function setupHusky(projectDir: string, linter?: "biome" | "oxc") {
await addPackageDependency({
devDependencies: ["husky", "lint-staged"],
projectDir,
});
const packageJsonPath = path.join(projectDir, "package.json");
if (await fs.pathExists(packageJsonPath)) {
const packageJson = await fs.readJson(packageJsonPath);
packageJson.scripts = {
...packageJson.scripts,
prepare: "husky",
};
if (linter === "oxc") {
packageJson["lint-staged"] = {
"*": ["oxlint", "oxfmt --write"],
};
} else if (linter === "biome") {
packageJson["lint-staged"] = {
"*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}": ["biome check --write ."],
};
} else {
packageJson["lint-staged"] = {
"**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx,vue,astro,svelte}": "",
};
}
await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
}
}
export async function setupLefthook(projectDir: string) {
await addPackageDependency({
devDependencies: ["lefthook"],
projectDir,
});
// lefthook.yml is generated by template-generator from templates/addons/lefthook/
}