-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathintegrations.ts
More file actions
226 lines (217 loc) · 6.65 KB
/
integrations.ts
File metadata and controls
226 lines (217 loc) · 6.65 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { insertAfter, insertAtBeginning } from "@solid-cli/utils/fs";
import { writeFile } from "fs/promises";
import { fileExists, manipulateJsonFile, validateFilePath } from "./utils/helpers";
import { $ } from "execa";
import { getRunnerCommand, detectPackageManager } from "@solid-cli/utils/package-manager";
import { createSignal } from "@solid-cli/reactivity";
import * as p from "@clack/prompts";
import color from "picocolors";
import { cancelable } from "@solid-cli/ui";
import { flushQueue } from "@solid-cli/utils/updates";
import { PluginOptions } from "@solid-cli/utils/transform";
// All the integrations/packages that we support
export type Supported = keyof typeof integrations;
export type IntegrationsValue = {
pluginOptions?: PluginOptions;
installs: string[];
installsDev?: string[];
additionalConfig?: () => Promise<void>;
postInstall?: () => Promise<void>;
};
export type Integrations = Record<Supported, IntegrationsValue>;
export const [rootFile, setRootFile] = createSignal<string | undefined>(undefined);
export const integrations: Record<string, IntegrationsValue> = {
tailwind: {
installs: ["tailwindcss", "postcss", "autoprefixer"],
postInstall: async () => {
const pM = detectPackageManager();
await $`${getRunnerCommand(pM)} tailwindcss init -p`;
let tailwindConfig = "tailwind.config.js";
if (!fileExists(tailwindConfig)) {
p.log.error(color.red(`Can't find tailwind config file`));
await cancelable(
p.text({
message: `Type path to tailwind config: `,
validate(value) {
if (!value.length) return `Path can not be empty`;
const path = validateFilePath(value, "tailwind.config");
if (!path) return `Tailwind config at \`${value}\` not found. Please try again`;
else {
tailwindConfig = path;
}
},
}),
);
}
let indexCss = "./src/index.css";
if (!fileExists(indexCss)) {
p.log.error(color.red(`Can't find index.css`));
await cancelable(
p.text({
message: `Type path to index.css: `,
validate(value) {
if (!value.length) return `Path can not be empty`;
const path = validateFilePath(value, "index.css");
if (!path) return `index.css at \`${value}\` not found. Please try again`;
else {
indexCss = path;
}
},
}),
);
}
p.log.info("Updating tailwind config");
await insertAfter(tailwindConfig, "content: [", '"./index.html", "./src/**/*.{js,ts,jsx,tsx}"');
await insertAtBeginning(indexCss, "@tailwind base;\n@tailwind components;\n@tailwind utilities;\n");
// Instantly flush queue
await flushQueue();
},
},
unocss: {
pluginOptions: {
importName: "UnoCss",
importSource: "unocss/vite",
isDefault: true,
options: {},
},
installs: [""],
installsDev: ["unocss"],
additionalConfig: async () => {
const path = rootFile();
if (!path) return;
await insertAtBeginning(path, `import "virtual:uno.css";\n`);
},
},
vitepwa: {
pluginOptions: {
importName: "VitePWA",
importSource: "vite-plugin-pwa",
isDefault: false,
options: {},
},
installs: ["vite-plugin-pwa"],
},
"solid-devtools": {
pluginOptions: {
importName: "devtools",
importSource: "solid-devtools/vite",
isDefault: true,
options: {},
},
installs: ["solid-devtools"],
additionalConfig: async () => {
const path = rootFile();
if (!path) return;
await insertAtBeginning(path, `import "solid-devtools";\n`);
},
},
vitest: {
installs: [],
installsDev: [
"vitest",
"jsdom",
"@solidjs/testing-library",
"@testing-library/user-event",
"@testing-library/jest-dom",
],
additionalConfig: async () => {
try {
p.log.info("Adding test script to package.json");
let hasStart = false;
manipulateJsonFile("package.json", (packageJson) => {
if (!packageJson.scripts) {
packageJson.scripts = {};
}
if (!/\bvitest\b/.test(packageJson.scripts.test || "")) {
packageJson.scripts.test = "vitest";
}
hasStart = packageJson.dependencies["@solidjs/start"];
return packageJson;
});
const hasTs = fileExists("tsconfig.json");
if (hasTs) {
p.log.info("Adding testing types to tsconfig.json");
manipulateJsonFile("tsconfig.json", (tsConfig) => {
if (!tsConfig.compilerOptions) {
tsConfig.compilerOptions = {};
}
tsConfig.compilerOptions.types = [
...new Set([...(tsConfig.compilerOptions.types || []), "vite/client", "@testing-library/jest-dom"]),
];
return tsConfig;
});
}
if (
hasStart &&
["ts", "mjs", "cjs", "js"].every(
(suffix) => !fileExists(`vite.config.${suffix}`) && !fileExists(`vitest.config.${suffix}`),
)
) {
const suffix = hasTs ? "ts" : "mjs";
p.log.info(`Adding vitest.config.${suffix}`);
await writeFile(
`vitest.config.${suffix}`,
`import solid from "vite-plugin-solid";
import { defineConfig } from "vitest/config";
export default defineConfig({
plugins: [solid()],
resolve: {
conditions: ["development", "browser"],
},
});
`,
"utf-8",
);
}
} catch (err) {
console.error(err);
}
},
},
"tauri-v1.x": {
installs: ["@tauri-apps/cli"],
postInstall: async () => {
try {
let name = "";
manipulateJsonFile("package.json", (packageJson) => {
if (!packageJson.scripts) {
packageJson.scripts = {};
}
packageJson.scripts.tauri = "tauri";
name = packageJson.name;
return packageJson;
});
await flushQueue();
const pM = detectPackageManager();
await $`${getRunnerCommand(pM)} tauri init --ci -A ${name} -W ${name} -D ../dist -P http://localhost:3000`;
p.note(`Make sure you have installed all prerequisites: https://tauri.app/v1/guides/getting-started/prerequisites
Start tauri development with ${color.bold(pM.name)} ${color.bold(pM.runScriptCommand("tauri dev"))}`);
} catch (err) {
console.error(err);
}
},
},
"tauri-v2.x": {
installs: ["@tauri-apps/cli@next"],
postInstall: async () => {
try {
let name = "";
manipulateJsonFile("package.json", (packageJson) => {
if (!packageJson.scripts) {
packageJson.scripts = {};
}
packageJson.scripts.tauri = "tauri";
name = packageJson.name;
return packageJson;
});
await flushQueue();
const pM = detectPackageManager();
await $`${getRunnerCommand(pM)} tauri init --ci -A ${name} -W ${name} -D ../dist -P http://localhost:3000`;
p.note(`Make sure you have installed all prerequisites: https://v2.tauri.app/start/prerequisites/
Start tauri development with ${color.bold(pM.name)} ${color.bold(pM.runScriptCommand("tauri dev"))}`);
} catch (err) {
console.error(err);
}
},
},
};