-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpl-compile~extend.js
236 lines (184 loc) · 5.88 KB
/
tpl-compile~extend.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
'use strict';
const path = require('path');
const fs = require('fs-extra');
const glob = require('glob');
const gulp = global.gulp || require('gulp');
const utils = require('fepper-utils');
const yaml = require('js-yaml');
const conf = global.conf;
const pref = global.pref;
const rootDir = global.rootDir;
const dataDir = conf.ui.paths.source.data;
const dataFile = `${dataDir}/_data.json`;
const patternDirPub = conf.ui.paths.public.patterns;
const patternDirSrc = conf.ui.paths.source.patterns;
const tplDir = conf.ui.paths.source.templates;
function tplCompile() {
const globExt = '.json';
const files = glob.sync(`${tplDir}/**/*${globExt}`) || [];
for (let i = 0; i < files.length; i++) {
const file = files[i];
let stat;
try {
stat = fs.statSync(file);
}
catch (err) /* istanbul ignore next */ {
utils.error(err);
continue;
}
// Only process valid files.
/* istanbul ignore if */
if (!stat || !stat.isFile()) {
continue;
}
const fileMinusExt = path.basename(file, globExt);
const pathMinusExt = `${path.dirname(file)}/${fileMinusExt}`;
const ymlFile = `${pathMinusExt}.yml`;
let data = {};
if (fs.existsSync(ymlFile)) {
try {
const yml = fs.readFileSync(ymlFile, conf.enc);
data = yaml.load(yml);
}
catch {} // eslint-disable-line no-empty
}
const tplCompileDir = data.tpl_compile_dir || pref.tpl_compile_dir;
const tplCompileExt = utils.extNormalize(data.tpl_compile_ext || pref.tpl_compile_ext);
/* istanbul ignore if */
if (!tplCompileDir || !tplCompileExt) {
continue;
}
let pubPattern = pathMinusExt.replace(`${patternDirSrc}/`, '');
pubPattern = pubPattern.replace(/\//g, '-');
pubPattern = pubPattern.replace(/~/g, '-');
const pubFile = `${patternDirPub}/${pubPattern}/${pubPattern}.markup-only.html`;
const pubContent = utils.beautifyTemplate(fs.readFileSync(pubFile, conf.enc));
const destFile = `${rootDir}/backend/${tplCompileDir.trim()}/${fileMinusExt}${tplCompileExt}`;
try {
fs.outputFileSync(destFile, pubContent);
}
catch (err) /* istanbul ignore next */ {
utils.error(err);
continue;
}
// Log to console.
utils.log('Template \x1b[36m%s\x1b[0m compiled.', destFile.replace(rootDir, '').replace(/^\//, ''));
}
}
function tplEncode(tplType, argv) {
let ext;
/* istanbul ignore else */
if (!argv || !argv.e) {
ext = `.${tplType}`;
}
else if (argv.e[0] === '.') {
ext = argv.e;
}
else {
ext = `.${argv.e}`;
}
let dataObj = {};
let dataStr = '';
try {
dataObj = fs.readJsonSync(dataFile);
dataStr = fs.readFileSync(dataFile, conf.enc);
}
catch {
// Fail gracefully. A correctly formed dataFile is not crucial for this.
}
const files = glob.sync(`${patternDirSrc}/**/*${ext}`) || [];
for (let i = 0; i < files.length; i++) {
const file = files[i];
let content = fs.readFileSync(file, conf.enc);
// Only Handlebars right now. Perhaps encode for other languages in the future.
switch (tplType) {
case 'hbs':
content = content.replace(/\{\{/g, '{{{<%');
content = content.replace(/(\})?\}\}/g, '$1%>}}}');
content = content.replace(/(\{\{\{<%)/g, '$1}}}');
content = content.replace(/(%>\}\}\})/g, '{{{$1');
break;
}
const regex = new RegExp(`${ext}$`);
let mustacheFile = file.replace(regex, '.mustache');
let jsonFile = file.replace(regex, '.json');
fs.outputFileSync(mustacheFile, content);
// Only Handlebars right now. Perhaps encode for other languages in the future.
switch (tplType) {
case 'hbs':
fs.outputFileSync(jsonFile, '{\n "<%": "{{",\n "%>": "}}"\n}\n');
break;
}
// Crucial part is done. Log to console.
utils.log(
'\x1b[36m%s\x1b[0m encoded to \x1b[36m%s\x1b[0m.', file.replace(rootDir, '').replace(/^\//, ''),
mustacheFile.replace(rootDir, '').replace(/^\//, '')
);
// Clean up.
fs.unlinkSync(file);
// Add key/values to _data.json if they are not there.
// These hide the encoded tags in all views except 03-templates.
if (!dataObj['<%']) {
if (Object.keys(dataObj).length) {
/* istanbul ignore next */
dataStr = dataStr.replace(/\s*\}(\s*)$/, ',\n "<%": "<!--"\n}$1');
}
else {
dataStr = '{\n "<%": "<!--"\n}\n';
}
try {
dataObj = JSON.parse(dataStr);
}
catch (err) /* istanbul ignore next */ {
utils.error(err);
return;
}
fs.outputFileSync(dataFile, dataStr);
}
if (!dataObj['%>']) {
dataStr = dataStr.replace(/\s*\}(\s*)$/, ',\n "%>": "-->"\n}$1');
try {
dataObj = JSON.parse(dataStr);
}
catch (err) /* istanbul ignore next */ {
utils.error(err);
return;
}
fs.outputFileSync(dataFile, dataStr);
}
}
}
// Declare gulp tasks.
gulp.task('tpl-compile:copy', function (cb) {
tplCompile();
cb();
});
gulp.task('tpl-compile', function (cb) {
gulp.runSequence(
'once',
'tpl-compile:copy',
cb
);
});
gulp.task('tpl-encode:hbs', function (cb) {
const argv = require('minimist')(process.argv.slice(2));
tplEncode('hbs', argv);
cb();
});
gulp.task('tpl-compile:help', function (cb) {
let out = `
Fepper Template Compile Extension
Use:
<task> [<additional args>...]
Tasks:
fp tpl-compile Export fully recursed compiled templates to the backend.
fp tpl-compile:copy Like 'fp tpl-compile' but without running 'fp once' first.
fp tpl-encode:hbs Like 'fp tpl-compile' but in reverse. Mainly intended for Handlebars in the backend.
fp tpl-compile:help Print fp-tpl-compile tasks and descriptions.
`;
utils.info(out);
cb();
});
// Export tasks in case users want to run them without gulp.
exports.tplCompile = tplCompile;
exports.tplEncode = tplEncode;