"README.md" did not exist on "3ee3f35bf0a034b42968dfc0eec46b8d97774dfe"
Newer
Older
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
const path = require('path')
const fs = require('fs').promises
const objectAssignDeep = require(`object-assign-deep`)
const yaml = require('js-yaml')
const pug = require('pug')
const chokidar = require('chokidar')
const default_lang = 'en'
const pugFile = './src/index.pug'
const dataDir = './translation'
const outDir = './www'
async function compile() {
const langs = (await fs.readdir(dataDir))
.filter(f => path.extname(f) === '.yml')
.map(f => path.basename(f, '.yml'))
const langData = {}
await Promise.all(
langs.map(lang =>
fs
.readFile(`${dataDir}/${lang}.yml`, 'utf8')
.then(text => (langData[lang] = yaml.safeLoad(text))),
),
)
for (const lang in langData) {
if (lang === default_lang) continue
langData[lang] = objectAssignDeep(
{},
langData[default_lang],
langData[lang],
)
}
const options = JSON.parse(await fs.readFile('./src/config.json', 'utf-8'))
const pugFct = pug.compileFile(pugFile)
await fs.mkdir(outDir, { recursive: true })
await Promise.all(
Object.entries(langData).map(([lang, data]) => {
options.lang=lang
options.data=data
const html = pugFct(options)
const filename = `${outDir}/index-${lang}.html`
console.log(`write ${filename}`)
return fs.writeFile(filename, html)
}),
)
}
compile().then(() => {
if (!process.argv.includes('-w') && !process.argv.includes('--watch')) return
console.log('watching for changes…')
chokidar.watch([pugFile, dataDir]).on('change', editedFilePath => {
console.log(`\nchanged: ${editedFilePath}`)
compile()
})
})