-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathantima.js
91 lines (77 loc) · 2.23 KB
/
antima.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
// -- node modules --
const fs = require('fs');
const del = require('del');
const pug = require('pug');
const { argv } = require('yargs');
const yaml = require('yamljs');
// -- check parameters --
if (!argv.template || argv.template === '') {
console.log('Error, you must pass a --template variable to node process.');
process.exit(1);
}
// -- variables --
const { template } = argv;
const yamlPath = `./${template}/source`;
const htmlPath = `./${template}`;
const templatePath = `./${template}/template`;
// -- check if folder with json files exists --
if (!fs.existsSync(yamlPath)) {
console.log('Error, json folder does not exist.');
process.exit(1);
}
// -- reading data from json files --
let yamls = fs.readdirSync(yamlPath);
if (!yamls || yamls.length === 0) {
console.log('Error, you have zero json files for data loading.');
process.exit(1);
}
// -- removing old HTML files --
del.sync([`${htmlPath}/*.html`]);
// -- array for sitemap links --
const sitemap = [];
let sitemapConfig;
if (fs.existsSync(`${templatePath}/sitemap.config.json`)) {
sitemapConfig = fs.readFileSync(
`${templatePath}/sitemap.config.json`,
'utf-8'
);
sitemapConfig = JSON.parse(sitemapConfig).keys;
}
// -- generating sitemap --
yamls = yamls.filter((y) => {
return y.indexOf('.yaml') !== -1;
});
yamls.forEach((file) => {
const yamlSource = yaml.load(`${yamlPath}/${file}`);
const sitemapItem = {
path: `${htmlPath}/${file.replace('.yaml', '.html')}`,
relativePath: `./${file.replace('.yaml', '.html')}`,
fileName: `${file.replace('.yaml', '.html')}`,
};
if (sitemapConfig) {
sitemapConfig.forEach((k) => {
sitemapItem[k] = yamlSource[k];
});
}
sitemap.push(sitemapItem);
});
// -- writing sitemap into a pug file --
fs.writeFileSync(
`${templatePath}/sitemap.pug`,
`- var sitemap = ${JSON.stringify(sitemap)}`,
'utf-8'
);
// -- build html pages from pug template --
yamls.forEach((file) => {
const yamlSource = yaml.load(`${yamlPath}/${file}`);
const html = pug.renderFile(`${templatePath}/${template}.pug`, {
data: yamlSource,
fileName: file.replace('.yaml', ''),
});
fs.writeFileSync(
`${htmlPath}/${file.replace('.yaml', '.html')}`,
html,
'utf-8'
);
});