/
ae86.js
139 lines (118 loc) · 3.25 KB
/
ae86.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"use strict"
import async from 'async';
import bag from 'bagofcli';
import cpr from 'cpr';
import dateformat from 'dateformat';
import Engine from './engine.js';
import fs from 'fs';
import p from 'path';
import watch from '@cnakazawa/watch';
import wrench from 'wrench';
const DIRNAME = p.dirname(import.meta.url).replace('file://', '');
/**
* class AE86
*/
class AE86 {
constructor(opts) {
opts = opts || {};
this.outDir = opts.outDir || 'out';
}
/**
* Create example AE86 project files in current directory.
*
* @param {Function} cb: standard cb(err, result) callback
*/
init(cb) {
cpr.cpr(p.join(DIRNAME, '../examples'), '.', cb);
}
/**
* Generate website based on the templates and params.
*
* @param {Function} cb: standard cb(err, result) callback
*/
generate(cb) {
const self = this;
function _static(cb) {
// copy static files as-is
cpr.cpr('static', self.outDir, cb);
}
function _pages(cb) {
function _params(cb) {
function prepParams(result) {
const params = result.params;
// add website info
params.sitemap = params.sitemap || {};
params.__genId = dateformat('yyyymmddHHMMss');
cb(null, params);
}
// initialise userland params
const paramsFile = p.join(process.cwd(), 'params.js');
if (fs.existsSync(paramsFile)) {
import(paramsFile)
.then(result => prepParams(result))
.catch(err => cb(err));
} else {
cb(null, null);
}
}
const engine = new Engine(),
tasks = {};
['partials', 'layouts', 'pages'].forEach((dir) => {
tasks[dir] = function (cb) {
engine.compile(dir, cb);
};
});
async.parallel(tasks, (err, results) => {
function prepParams(err, result) {
if (err) {
cb(err);
} else {
engine.merge(self.outDir, results, result || {}, cb);
}
}
_params(prepParams);
});
}
async.parallel([_static, _pages], cb);
}
/**
* Watch for any file changes in AE86 project files.
* A change means the project website will automatically be regenerated.
*/
watch() {
const self = this;
function _listener() {
// no callback because the process shouldn't exit
self.generate();
}
function _watch(file) {
watch.watchTree(file, function (f, curr, prev) {
if (typeof f === "object" && prev === null && curr === null) {
bag.logStepItemSuccess('Watching for file changes at %s...', file)
} else if (prev === null) {
bag.logStepItemSuccess('%s was created', file);
_listener();
} else if (curr.nlink === 0) {
bag.logStepItemSuccess('%s was deleted', file);
self.clean();
_listener();
} else {
bag.logStepItemSuccess('%s was modified', file);
_listener();
}
});
}
['static', 'partials', 'layouts', 'pages', 'params.js'].forEach(_watch);
}
/**
* Remove the generated website.
*
* @param {Function} cb: standard cb(err, result) callback
*/
clean(cb) {
wrench.rmdirRecursive(this.outDir, cb);
}
}
export {
AE86 as default
};