-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate-config.js
76 lines (63 loc) · 2.18 KB
/
generate-config.js
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
import { fileURLToPath } from 'url';
import { dirname, resolve, extname } from 'path';
import { promises as fs } from 'fs';
import { URL } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const parseArray = (arrayString) => {
try {
return arrayString ? JSON.parse(arrayString) : [];
} catch (error) {
console.error(`Error parsing JSON array: ${error.message}`);
return [];
}
};
const extractItemsFromEnv = (prefix) => {
const items = [];
let index = 1;
while (process.env[`${prefix}_${index}`]) {
const itemString = process.env[`${prefix}_${index}`];
const item = parseArray(itemString);
if (item.length > 0) {
items.push(item[0]);
}
index++;
}
return items;
};
const chronosConfig = {
title: process.env.CHRONOS_TITLE || "Chronos Documentation",
logoTitle: process.env.CHRONOS_LOGO_TITLE || "Documentation",
logoUrl: process.env.CHRONOS_LOGO_URL || "/logo.svg",
description:
process.env.CHRONOS_DESCRIPTION ||
"A frontend in Vue.js for the Chronos documentation server.",
baseUrl: process.env.CHRONOS_BASE_URL || "http://localhost:5173",
chronosCollections: extractItemsFromEnv("CHRONOS_COLLECTION"),
extraLinks: extractItemsFromEnv("CHRONOS_EXTRA_LINK") || [
{
url: "https://github.com/vanilla-os/Chronos",
name: "Source Code",
},
],
};
const publicFolderPath = resolve(__dirname, "public");
fs.mkdir(publicFolderPath, { recursive: true }).catch(console.error);
if (process.env.CHRONOS_LOGO_URL && !isWebURL(process.env.CHRONOS_LOGO_URL)) {
const logoExtension = extname(process.env.CHRONOS_LOGO_URL);
const logoFileName = `logo${logoExtension}`;
const logoFilePath = resolve(publicFolderPath, logoFileName);
fs.copyFile(process.env.CHRONOS_LOGO_URL, logoFilePath).catch(console.error);
chronosConfig.logoUrl = `/logo${logoExtension}`;
}
const configFilePath = resolve(publicFolderPath, "chronos.json");
fs.writeFile(configFilePath, JSON.stringify(chronosConfig, null, 2)).catch(console.error);
console.log("chronos.json generated successfully!");
function isWebURL(str) {
try {
new URL(str);
return true;
} catch (error) {
return false;
}
}