-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
init.ts
152 lines (126 loc) Β· 3.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
import { brightGreen, gray } from "../deps/colors.ts";
import { pluginNames } from "../core/utils.ts";
import importMap from "./import_map.ts";
import { Checkbox, Confirm, Select } from "../deps/cliffy.ts";
import { outdent } from "../deps/outdent.ts";
/** Generate a _config.js file */
export default function (): Promise<void> {
return init();
}
export async function init() {
const plugins = await initConfig();
if (!plugins) return;
await importMap({ plugins });
welcome();
}
/** (Re)configure lume config file */
async function initConfig(): Promise<string[] | undefined> {
const configFile = await getConfigFile();
if (!configFile) {
console.log(gray("Lume init cancelled."));
return;
}
const plugins = await getPlugins();
// 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 Deno.writeTextFile(configFile, code.join("\n"));
console.log();
console.log("Lume configuration file saved:", gray(configFile));
return plugins;
}
/**
* 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(): 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",
},
],
});
try {
await Deno.lstat(file);
const override = await Confirm.prompt({
message: `The file "${file}" already exist. Override?`,
default: false,
});
return override ? file : false;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return file;
}
throw err;
}
}
/** Show a welcome message */
function welcome() {
const links = {
page: brightGreen("echo 'Hello world' > index.md"),
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! ππ
π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯π₯
Run ${links.page} to create your first page
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);
}