-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (58 loc) · 1.35 KB
/
index.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
let fs = require('fs');
let jener = require('./page');
function readFiles(indir, cb) {
fs.readdir(indir, (err, files) => {
if (err) {
cb(err);
return;
}
let res = [];
files.forEach(file => {
fs.readFile(`${indir}/${file}`, 'utf8', function (err, data) {
if (err) {
console.warn(`Couldn't read ${file} skipping. [${err.message}]`);
}
res.push(file, data);
if (res.length === files.length * 2) {
cb(null, res);
}
});
});
});
}
function writeFiles(pages, outdir, cb) {
let c = 0,
w = 0;
for (let i = 0; i < pages.length; i += 2) {
let filename = pages[i],
content = pages[i + 1];
fs.writeFile(`${outdir}/${filename}`, content, err => {
if (err) {
console.log(`skipping ${filename}, ${err.code}`);
} else {
console.log('✓ ' + filename);
w++;
}
c++;
if (c * 2 === pages.length) {
cb(!w, w);
}
});
}
}
function app(indir, outdir) {
readFiles(indir, (err, defs) => {
if (err) {
console.error(`${err.code}`);
return;
}
writeFiles(jener(defs), outdir, (err, c) => {
if (err) {
console.error(`Couldn't write any files`);
} else {
console.log(`Generated ${c} files.`);
}
});
});
}
module.exports = app;