-
Notifications
You must be signed in to change notification settings - Fork 0
/
magpiler-make.js
101 lines (88 loc) · 3.09 KB
/
magpiler-make.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
92
93
94
95
96
97
98
99
100
101
const commandLineArgs = require('command-line-args');
const getUsage = require('command-line-usage');
const path = require('path');
const Path = path;
const fs = require('fs');
const { renderPage, getFilesRecursively, parseOptions, loadData } = require('./magpiler-core');
const optionDefinitions = [
{ name: 'input', alias: 'i', defaultOption: true, type: String, description: "input folder to process"},
{ name: 'args', alias: 'a', type: String, description: "k1:v1,k2:v2 strings to add to options" },
{ name: 'help', alias: 'h', type: Boolean, description: "print this usage help and exit"}
];
const sections = [
{
header: 'magpiler-make',
content: 'Xpiler for a Markdown- and JS-based website.\nGenerate entire static site.'
},
{
header: 'Options',
optionList: [
{
name: 'input',
typeLabel: '{underline folder}',
description: '(Default with no flags.) The input folder to process (e.g. {underline /path/to/src}).'
},
{
name: 'args',
typeLabel: '{underline k1:v1,k2:v2,...}',
description: "key value pairs to add to options object, available to config.js and global.js"
},
{
name: 'help',
description: 'Print this usage guide.'
}
]
}
];
function mkdirNoCrash(path) {
fs.mkdirSync(path, { recursive: true });
}
function makeMain(options) {
options = parseOptions(sections, options);
if (!options) {
return;
}
loadData(options);
console.log('output folder at', options.out);
// mkdir out/ folder if it doesn't exist (but do not crash if it does exist)
mkdirNoCrash(options.out);
// TODO delete anything from out/ that is not present in static/ or render/
// (or user can just manage this themselves and blow away the out/ folder if
// they know they are renaming or removing things)
// copy everything from static/ to out/ (if different size in bytes)
getFilesRecursively(Path.join(options.src, "static")).forEach(file => {
if (file.indexOf('/.') >= 0) { // skip .xyz files
return;
}
let justFile = file.replace(options.src, '').replace('static/', '');
if (justFile.startsWith('/')) {
justFile = justFile.slice(1);
}
if (justFile.indexOf('/') >= 0) {
let folderToEnsure = Path.join(options.out, Path.dirname(justFile));
//console.log('mkdir -P', folderToEnsure);
mkdirNoCrash(folderToEnsure);
}
let to = Path.join(options.out, justFile);
fs.copyFileSync(file, to);
});
// render each page in render array as a page in out/
options.render.forEach(ob => {
if (ob.file.indexOf('/') >= 0) {
let folderToEnsure = Path.join(options.out, Path.dirname(ob.file));
//console.log('mkdir -P', folderToEnsure);
mkdirNoCrash(folderToEnsure);
}
renderPage(ob.file, Path.join(options.out, ob.file), options);
});
// We are done when all the promises finish. Good night and enjoy your baked-out website!
}
function main() {
if (process.argv.length <= 2) {
console.log(getUsage(sections));
return;
}
let options = commandLineArgs(optionDefinitions);
makeMain(options);
}
main();