-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
init.ts
100 lines (84 loc) · 2.35 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
import { brightGreen, cyan, dim, red } from "../deps/colors.ts";
import { pluginNames } from "./utils.ts";
import importMap from "./import_map.ts";
/** Generate a _config.js file */
export default function () {
return init();
}
export async function init() {
await initConfig();
await importMap();
}
/** (Re)configure lume config file */
async function initConfig() {
const configFile = await getConfigFile();
if (!configFile) {
console.log();
console.log("No config file created");
return;
}
// Generate the code for the config file
const code = [`import lume from "lume/mod.ts";`];
const plugins = getPlugins();
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(brightGreen("Lume configuration file saved:"), configFile);
}
/** Question to get the list of plugins to install in the config file */
function getPlugins() {
const message = `
${cyan("Do you want to use plugins?")}
Type the plugins separated by comma or space.
All available options:
${
pluginNames.map((plugin) =>
`- ${dim(plugin)} https://lume.land/plugins/${plugin}/`
).join("\n")
}
Example: ${dim(`postcss terser base_path`)}
`;
const choice = prompt(message);
const plugins = choice ? choice.split(/[\s,]+/) : [];
// Validate the plugins
return plugins.filter((plugin) => {
if (pluginNames.includes(plugin)) {
return true;
}
console.log(red(`Ignored not valid plugin ${plugin}.`));
return false;
});
}
/** Question to get the filename of the config file */
async function getConfigFile(): Promise<string | false> {
const configFile = confirm(cyan("Use TypeScript for the configuration file?"))
? "_config.ts"
: "_config.js";
try {
await Deno.lstat(configFile);
return confirm(
cyan(`The file "${configFile}" already exist. Override?`),
)
? configFile
: false;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return configFile;
}
throw err;
}
}