-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
98 lines (83 loc) · 3.68 KB
/
mod.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
// Lume plugin https://lume.land/docs/advanced/plugins/
// Generates language data for website multilanguage websites.
// Puts the generated data in <srcdir>/<langdir>/_data/lang.yaml.
// Also generates a list of the site's languages in <srcdir>/languages.json.
// How this plugin is useful:
// ▶︎ This data can be used in layouts. For example a layout file can contain "<!DOCTYPE html><html lang='{{ lang.code }}'> ..."
// ▶︎ /index.html can shunt the user to his/her preferred language with a JavaScript code such as fetch('/languages.json').then((response) => response.json()).then((siteLanguages) => { /*...*/ });
// external dependencies
import {path, YAML, date, Lang} from './deps.ts';
export default
function () {
return (site: Lume.Site) => {
// console.info(`ℹ️ langdata: site: ${JSON.stringify(site)}`);
const
languages : string[] = [],
currentWorkingDirectoryAbs: string = site.options.cwd,
projectSourceDirectory : string = site.options.src,
projectSourceDirectoryAbs : string = path.resolve(currentWorkingDirectoryAbs, projectSourceDirectory);
// console.info(`ℹ️ langdata: currentWorkingDirectoryAbs: ${currentWorkingDirectoryAbs}`);
// console.info(`ℹ️ langdata: projectSourceDirectoryAbs: ${projectSourceDirectoryAbs}`);
for (const dirEntry of Deno.readDirSync(projectSourceDirectoryAbs)) {
if(!(dirEntry.isDirectory && Lang.getLanguageInfo(dirEntry.name)))continue;
// directory serves the site in this language: dirEntry.name
const
langCode: string = dirEntry.name,
absLangDirname: string = path.resolve(projectSourceDirectoryAbs, langCode),
absLangDataDirname: string = path.resolve(absLangDirname, '_data');
// ensure _data dir exists
let absLangDataDirInfo: Deno.FileInfo;
try {
absLangDataDirInfo = Deno.statSync(absLangDataDirname);
} catch (_error) {
// _data does not exist
Deno.mkdirSync(absLangDataDirname);
absLangDataDirInfo = Deno.statSync(absLangDataDirname);
}
if(!absLangDataDirInfo.isDirectory)continue;
// console.log(` data dir: ${absLangDataDirname}`);
// ensure lang.yaml file does not exist
const langYamlFilenameAbs = path.resolve(absLangDataDirname, 'lang.yaml');
try {
const langYamlInfo = Deno.statSync(langYamlFilenameAbs);
// lang.yaml exists
if(!langYamlInfo.isFile)continue;
Deno.removeSync(langYamlFilenameAbs);
} catch (_error) {
// lang.yaml does not exist
}
// write lang.yaml
try {
const
now = new Date(),
timestamp = date.format(now, "yyyy-MM-dd"),
langData = {code: langCode },
yaml = YAML.stringify(langData);
if(!yaml)throw new Error('');
Deno.writeTextFileSync(
langYamlFilenameAbs,
`# This file was automatically generated at build time
# on ${timestamp} by the "lume_langdata" Lume plugin.
${yaml}`);
} catch (_error) {
// should never happen
}
languages.push(langCode);
}
// write <srcdir>/languages.json
const langsFilenameAbs : string = path.resolve(projectSourceDirectoryAbs, 'languages.json');
try {
const langsFilenameInfo: Deno.FileInfo = Deno.statSync(langsFilenameAbs);
// <srcdir>/languages.json exists
if(!langsFilenameInfo.isFile)return;
Deno.removeSync(langsFilenameAbs);
} catch (_error) {
// <srcdir>/languages.json does not exist
}
try {
Deno.writeTextFileSync(langsFilenameAbs, JSON.stringify(languages));
} catch (_error) {
console.error(`Error when writing languages file: ${langsFilenameAbs}`);
}
};
}