-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.js
69 lines (53 loc) · 1.88 KB
/
compile.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
const Handlebars = require('handlebars')
const fs = require('fs')
const yaml = require('js-yaml')
const path = require('path')
/* ------------------------ CONSTANTS ----------------------- */
const SOURCE_DIR = 'source'
const OUTPUT_DIR = 'docs'
/* ------------------------- HELPERS ------------------------ */
Handlebars.registerHelper('isMissing', function (value, options) {
if (value === null || value === '' || value === undefined)
return options.fn(this)
else return options.inverse(this)
})
Handlebars.registerHelper('sourceExists', function (value, options) {
if (!value) return options.inverse(this)
const filePath = path.join(OUTPUT_DIR, value)
if (fs.existsSync(filePath)) return options.fn(this)
else return options.inverse(this)
})
/* ----------------------- LOAD SHARED ---------------------- */
const sharedPath = path.join(SOURCE_DIR, 'shared.yml')
const sharedStr = fs.readFileSync(sharedPath, 'utf8')
const sharedObj = yaml.load(sharedStr)
/* ----------------- LOOP THROUGH LANGUAGES ----------------- */
const languages = [
{
inputName: 'english.yml',
outputName: 'en.html',
},
{
inputName: 'swedish.yml',
outputName: 'se.html',
},
]
for (const language of languages) {
compileLanguage(language)
}
/* --------------------- COMPILE LANGUAGE -------------------- */
function compileLanguage(lang) {
const templateSourcePath = path.join(SOURCE_DIR, 'template.html')
const templateSource = fs.readFileSync(templateSourcePath, 'utf8')
const contentPath = path.join(SOURCE_DIR, lang.inputName)
const contentStr = fs.readFileSync(contentPath, 'utf8')
const contentObj = yaml.load(contentStr)
const context = {
...sharedObj,
...contentObj,
}
const template = Handlebars.compile(templateSource)
const result = template(context)
const outputPath = path.join(OUTPUT_DIR, lang.outputName)
fs.writeFileSync(outputPath, result)
}