-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathadd.ts
More file actions
225 lines (192 loc) · 6.93 KB
/
add.ts
File metadata and controls
225 lines (192 loc) · 6.93 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
import { autocomplete } from "@solid-cli/ui";
import { S_BAR, cancelable, spinnerify } from "@solid-cli/ui";
import { Integrations, Supported, integrations, setRootFile } from "../lib/integrations";
import * as p from "@clack/prompts";
import color from "picocolors";
import { primitives, loadPrimitives } from "@solid-cli/utils/primitives";
import { isSolidStart, t } from "@solid-cli/utils";
import { fileExists, getRootFile, getConfigFile, validateFilePath } from "../lib/utils/helpers";
import { writeFile, readFile } from "@solid-cli/utils/fs";
import { transformPlugins, type PluginOptions } from "@solid-cli/utils/transform";
import {
UPDATESQUEUE,
clearQueue,
flushCommandUpdates,
flushFileUpdates,
flushPackageUpdates,
queueUpdate,
summarizeUpdates,
} from "@solid-cli/utils/updates";
const handleAutocompleteAdd = async () => {
const supportedIntegrations = (Object.keys(integrations) as Supported[]).map((value) => ({ label: value, value }));
const opts = () => [...supportedIntegrations, ...primitives()];
loadPrimitives().catch((e) => p.log.error(e));
const a = await cancelable(
autocomplete({
message: t.ADD_PACKAGES,
options: opts,
}),
);
if (a.length === 0) {
p.log.warn(t.NOTHING_SELECTED);
return;
}
const shouldInstall = await cancelable<unknown>(
p.select({
options: [
{ label: t.YES, value: true },
{ label: t.NO, value: false },
{ label: t.YES_FORCE, value: [true, "force"] },
],
message: `${t.CONFIRM_INSTALL(a.length)} \n${color.red(S_BAR)} \n${color.red(S_BAR)} ${
" " + color.yellow(a.map((opt) => opt.label).join(" ")) + " "
} \n${color.red(S_BAR)} `,
}),
);
if (!shouldInstall) return;
let forceTransform = false;
if (Array.isArray(shouldInstall) && shouldInstall[1] === "force") {
forceTransform = true;
}
const packages = a.map((opt) => opt.value as Supported);
return { packages, forceTransform };
};
const isIntegration = (str: string) => {
if (Object.keys(integrations).includes(str)) return true;
return false;
};
/**
* Transforms a list containing primitives, either by name or full package name, and returns the corresponding primitive objects
*/
const transformPrimitives = async (ps: string[]) => {
if (!ps.length) return [];
if (!primitives().length) {
await spinnerify({
startText: t.LOADING_PRIMITIVES,
finishText: t.PRIMITIVES_LOADED,
fn: loadPrimitives,
});
}
const mappedInput = ps.map((p) => p.replace("@solid-primitives/", ""));
return primitives().filter((p) => mappedInput.includes(p.value.replace("@solid-primitives/", "")));
};
type Configs = Integrations[keyof Integrations][];
export const handleAdd = async (packages?: string[], forceTransform: boolean = false) => {
if (!packages?.length) {
const autocompleted = await handleAutocompleteAdd();
if (!autocompleted) return;
packages = autocompleted.packages;
forceTransform = autocompleted.forceTransform;
}
const possiblePrimitives: string[] = [];
const configs: Configs = packages
.map((n) => {
if (!n) return;
if (!isIntegration(n)) {
possiblePrimitives.push(n);
return;
}
const res = integrations[n as Supported];
if (!res) {
p.log.error(t.NO_SUPPORT(n));
return;
}
return res;
})
.filter((p) => p) as Configs;
const configType = (await isSolidStart()) ? "app" : "vite";
const configFile = await getConfigFile(configType);
for (let i = 0; i < configs.length; i++) {
const config = configs[i];
config.installs.forEach((p) => queueUpdate({ type: "package", name: p, dev: false }));
config.installsDev?.forEach((p) => queueUpdate({ type: "package", name: p, dev: true }));
}
// Queue primitives
for (const primitive of await transformPrimitives(possiblePrimitives)) {
queueUpdate({ type: "package", name: primitive.value, dev: false });
}
if (!configs.length) return;
const pluginOptions = configs.map((c) => c.pluginOptions).filter(Boolean) as PluginOptions[];
if (pluginOptions.length) {
await spinnerify({
startText: "Processing config",
finishText: "Config processed",
fn: async () => {
const code = await transformPlugins(
configs.map((c) => c.pluginOptions).filter(Boolean) as PluginOptions[],
{ type: configType, name: configFile, contents: (await readFile(configFile)).toString() },
forceTransform,
undefined,
);
await writeFile(configFile, code);
},
});
}
p.log.info("Preparing post install steps for integrations");
let projectRoot = await getRootFile();
setRootFile(projectRoot);
if (!fileExists(projectRoot)) {
p.log.error(color.red(`Can't find root file \`${projectRoot.split("/")[1]}\`.`));
await cancelable(
p.text({
message: `Type path to root: `,
validate(value) {
if (!value.length) return `Path can not be empty`;
const path = validateFilePath(value, ["app.tsx", "index.tsx"]);
if (!path) return `File at \`${value}\` not found. Please try again`;
else setRootFile(path);
},
}),
);
}
// Run our additional configs
await spinnerify({
startText: "Running additional config steps",
finishText: "Additional config steps complete",
fn: async () => {
for (const cfg of configs) {
await cfg.additionalConfig?.();
}
},
});
if (UPDATESQUEUE.length === 0) return;
const { fileUpdates, packageUpdates, commandUpdates } = summarizeUpdates();
// Inspired by Qwik's CLI
if (fileUpdates.length) p.log.message([`${color.cyan("Modify")}`, ...fileUpdates.map((f) => ` - ${f}`)].join("\n"));
if (packageUpdates.length)
p.log.message(
[`${color.cyan("Install")}`, ...packageUpdates.map((p) => ` - ${p.name}` + (p.dev ? " (dev)" : ""))].join("\n"),
);
if (commandUpdates.length)
p.log.message([`${color.cyan("Run commands")}`, ...commandUpdates.map((p) => ` - ${p}`)].join("\n"));
const confirmed = await p.confirm({ message: "Do you wish to continue?" });
if (!confirmed || p.isCancel(confirmed)) return;
await spinnerify({ startText: "Writing files...", finishText: "Updates written", fn: flushFileUpdates });
await spinnerify({ startText: "Installing packages...", finishText: "Packages installed", fn: flushPackageUpdates });
await spinnerify({ startText: "Running setup commands", finishText: "Setup commands ran", fn: flushCommandUpdates });
clearQueue();
const postInstalls = configs.filter((c) => c.postInstall);
if (postInstalls.length === 0) return;
p.log.message(
`${postInstalls.length} ${
postInstalls.length === 1 ? "package has" : "packages have"
} post install steps that need to run.`,
);
const pInstallConfirmed = await p.confirm({ message: "Do you wish to continue?" });
if (!pInstallConfirmed || p.isCancel(pInstallConfirmed)) return;
p.log.info("Running post installs");
// Run postInstalls
for (const cfg of configs) {
await cfg.postInstall?.();
}
p.log.success("Post install steps complete!");
// await spinnerify({
// startText: t.POST_INSTALL,
// finishText: t.POST_INSTALL_COMPLETE,
// fn: async () => {
// for (const cfg of configs) {
// await cfg.postInstall?.();
// }
// },
// });
};