forked from nunocoracao/blowfish
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenLangLinks.js
More file actions
71 lines (59 loc) · 1.58 KB
/
genLangLinks.js
File metadata and controls
71 lines (59 loc) · 1.58 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
const fs = require("fs/promises");
const configDir = "./exampleSite/config/_default";
const contentDir = "./exampleSite/content";
const defaultLang = "en";
const targetLangs = [];
async function readConfigs() {
const files = await fs.readdir(configDir);
for (const file of files) {
console.log(file);
if (file.indexOf("languages.") > -1) {
const lang = file.split(".")[1];
console.log(lang);
if (lang != defaultLang) {
targetLangs.push(lang);
}
}
}
}
async function processFile(filePath, file) {
if (filePath.indexOf("index.md") > -1) {
console.log("processing", filePath);
for (const targetLang of targetLangs) {
const targetFilePath = filePath.replace(".md", "." + targetLang + ".md");
let exists = true;
try {
await fs.access(targetFilePath);
} catch {
exists = false;
}
if (exists) {
console.log("file already exists", targetFilePath);
} else {
console.log("creating file", targetFilePath);
await fs.copyFile(filePath, targetFilePath);
}
}
} else {
return;
}
}
async function processFolder(folderPath) {
const files = await fs.readdir(folderPath);
for (const file of files) {
const filePath = `${folderPath}/${file}`;
const isDir = (await fs.lstat(filePath)).isDirectory();
if (isDir) {
await processFolder(filePath);
} else {
await processFile(filePath, file);
}
}
}
async function createLinks() {
await processFolder(contentDir);
}
(async () => {
await readConfigs();
await createLinks();
})();