-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
init.ts
227 lines (185 loc) Β· 5.79 KB
/
init.ts
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
227
import { brightGreen, gray } from "./deps/colors.ts";
import {
checkDenoVersion,
pluginNames,
updateLumeVersion,
writeDenoConfig,
} from "./core/utils.ts";
import { Checkbox, Confirm, Select } from "./deps/cliffy.ts";
import { outdent } from "./deps/outdent.ts";
import { join } from "./deps/path.ts";
import { ensureDir } from "./deps/fs.ts";
import type { DenoConfigResult } from "./core/utils.ts";
checkDenoVersion();
const folder = Deno.args[0] || ".";
/** Init Lume in the current directory */
const configFile = await getConfigFile(folder);
if (!configFile) {
console.log(gray("Lume init cancelled."));
Deno.exit();
}
const plugins = await getPlugins();
const denoConfig: DenoConfigResult = {
config: {},
file: join(folder, "deno.json"),
};
initPlugins(plugins, denoConfig);
// Generate the code for the config file
const code = [`import lume from "lume/mod.ts";`];
plugins.forEach((name) =>
code.push(
`import ${name} from "lume/plugins/${name}.ts";`,
)
);
code.push("");
code.push("const site = lume();");
if (plugins.length) {
code.push("");
plugins.forEach((name) => code.push(`site.use(${name}());`));
}
code.push("");
code.push("export default site;");
code.push("");
// Write the code to the file
await ensureDir(folder);
await Deno.writeTextFile(configFile, code.join("\n"));
console.log();
console.log("Lume configuration file saved:", gray(configFile));
const url = new URL(import.meta.resolve("./"));
updateLumeVersion(url, denoConfig);
writeDenoConfig(denoConfig);
const links = {
serve: brightGreen("deno task serve"),
repo: gray("https://github.com/lumeland/lume"),
website: gray("https://lume.land"),
discord: gray("https://discord.gg/YbTmpACHWB"),
opencollective: gray("https://opencollective.com/lume"),
} as const;
const message = outdent`
π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯
${brightGreen(" Lume configured successfully!")}
BENVIDO - WELCOME! ππ
π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯
Quick start:
Create the index.md file and write some content
Run ${links.serve} to start a local server
See ${links.website} for online documentation
See ${links.discord} to propose new ideas and get help at Discord
See ${links.repo} to view the source code and report issues
See ${links.opencollective} to support Lume development
`;
console.log(message);
/**
* Question to get the list of plugins to install in the config file
* @returns Promise<string[]>
*/
async function getPlugins(): Promise<string[]> {
const usePlugins = await Select.prompt({
message: "Do you want to install some plugins now?",
options: [
{
name: "Yes",
value: "yes",
},
{
name: "Maybe later",
value: "no",
},
],
hint: "See all available plugins at https://lume.land/plugins/",
});
if (usePlugins === "no") {
return [];
}
return Checkbox.prompt({
message: "Select the plugins to install",
options: pluginNames,
hint: "Use Arrow keys and Space to select. Enter to submit",
});
}
/** Question to get the filename of the config file */
async function getConfigFile(folder: string): Promise<string | false> {
const file = await Select.prompt({
message: "Choose the configuration file format",
options: [
{
name: "_config.ts (TypeScript)",
value: "_config.ts",
},
{
name: "_config.js (JavaScript)",
value: "_config.js",
},
],
});
const path = join(folder, file);
try {
await Deno.lstat(path);
const override = await Confirm.prompt({
message: `The file "${path}" already exist. Override?`,
default: false,
});
return override ? path : false;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return path;
}
throw err;
}
}
function initPlugins(plugins: string[], denoConfig: DenoConfigResult) {
// Ensure that jsx and jsx_preact are not used at the same time and are loaded before mdx
if (plugins.includes("mdx")) {
const jsx = plugins.indexOf("jsx");
const jsx_preact = plugins.indexOf("jsx_preact");
if (jsx !== -1 && jsx_preact !== -1) {
throw new Error(
"You can't use both the jsx and jsx_preact plugins at the same time.",
);
}
if (jsx !== -1) {
// Ensure jsx is loaded before mdx
plugins.splice(jsx, 1);
plugins.unshift("jsx");
} else if (jsx_preact !== -1) {
// Ensure jsx_preact is loaded before mdx
plugins.splice(jsx_preact, 1);
plugins.unshift("jsx_preact");
} else {
// Use jsx by default
plugins.unshift("jsx");
}
}
if (plugins.includes("jsx")) {
denoConfig.config.compilerOptions ||= {};
denoConfig.config.compilerOptions.jsx = "react-jsx";
denoConfig.config.compilerOptions.jsxImportSource = "react";
// Add jsx-runtime import to import_map.
denoConfig.importMap ||= { imports: {} };
denoConfig.importMap.imports["react/jsx-runtime"] =
"https://esm.sh/react@18.2.0/jsx-runtime";
}
if (plugins.includes("jsx_preact")) {
denoConfig.config.compilerOptions ||= {};
denoConfig.config.compilerOptions.jsx = "react-jsx";
denoConfig.config.compilerOptions.jsxImportSource = "npm:preact";
}
// Ensure that tailwindcss is loaded before postcss
fixPluginOrder(plugins, "tailwindcss", "postcss");
// Ensure that picture is loaded before imagick
fixPluginOrder(plugins, "picture", "imagick");
}
function fixPluginOrder(plugins: string[], plugin1: string, plugin2: string) {
if (plugins.includes(plugin1)) {
const pos1 = plugins.indexOf(plugin1);
const pos2 = plugins.indexOf(plugin2);
if (pos2 === -1) {
plugins.splice(pos1 + 1, 0, plugin2);
return;
}
if (pos1 > pos2) {
plugins[pos2] = plugin1;
plugins[pos1] = plugin2;
}
}
}