-
Notifications
You must be signed in to change notification settings - Fork 0
/
d8e.js
74 lines (58 loc) · 2.06 KB
/
d8e.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
const { existsSync, mkdirSync, readdirSync, rm, statSync } = require('fs');
const { join, extname, relative } = require('path');
const { getConfig } = require('./src/util/config-file-parser');
const { throwAndExit } = require('./src/util/error');
const { log, printSecondsTaken } = require('./src/util/log');
const { copyImages, copyFonts } = require('./src/asset');
const { processHTMLFile } = require('./src/html');
const startTime = performance.now();
const action = process.argv[2];
if (action === 'version') {
log('info', 'd8e VERSION: 0.4.8');
printSecondsTaken(startTime);
return;
}
log('info', 'd8e 0.4.8 starting\n');
if (action !== 'build' && action !== 'b') {
throwAndExit(`Unexpected action: '${ action }', expected 'build' or 'b'`);
}
const { inputDirectory, outputDirectory } = getConfig();
if (!existsSync(inputDirectory)) {
throwAndExit(`The specified input directory does not exist: ${ inputDirectory }`);
}
const cwd = process.cwd();
const outputDir = join(cwd, outputDirectory);
if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
convertHTML();
} else {
rm(outputDir, { recursive: true }, (err) => {
if (err) {
log('error', `Error removing output directory: ${ err }`);
}
convertHTML();
});
}
function convertHTML() {
copyImages(inputDirectory, outputDir);
copyFonts(inputDirectory, outputDir);
const fullInputPath = join(cwd, inputDirectory);
const processedFiles = [];
function processDirectory(dir) {
readdirSync(dir).forEach(file => {
const fullPath = join(dir, file);
if (statSync(fullPath).isDirectory()) {
processDirectory(fullPath);
} else if (extname(file).toLowerCase() === '.html') {
const relativePath = relative(fullInputPath, fullPath);
processHTMLFile(fullPath, join(outputDir, relativePath), outputDirectory);
processedFiles.push(fullPath);
}
});
}
processDirectory(fullInputPath);
for (const processedFile of processedFiles) {
log('info', `processed HTML file ${ processedFile }`);
}
printSecondsTaken(startTime);
}