-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
64 lines (49 loc) · 2.14 KB
/
utils.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
const fs = require('fs');
const path = require('path');
function loadUserConfig() {
const file = path.resolve(process.cwd(), 'handlebars.config.js');
const flavors = [
file, // handlebars.config.js
file.replace('.js', '.json'), // handlebars.config.json
file.replace('handlebars.', 'hbs.'), // hbs.config.js
file.replace('handlebars.', 'hbs.').replace('.js', '.json'), // hbs.config.json
];
if (fs.existsSync(flavors[0])) { // eslint-disable-line no-sync
return require(flavors[0]); // eslint-disable-line global-require
}
if (fs.existsSync(flavors[1])) { // eslint-disable-line no-sync
return JSON.parse(fs.readFileSync(flavors[1], { encoding: 'utf-8' })); // eslint-disable-line no-sync
}
if (fs.existsSync(flavors[2])) { // eslint-disable-line no-sync
return require(flavors[2]); // eslint-disable-line global-require
}
if (fs.existsSync(flavors[3])) { // eslint-disable-line no-sync
return JSON.parse(fs.readFileSync(flavors[3], { encoding: 'utf-8' })); // eslint-disable-line no-sync
}
return {};
}
const parseSimpleLayout = (str, opts) => {
const layoutPattern = /{{!<\s+([A-Za-z0-9._\-/]+)\s*}}/;
const matches = str.match(layoutPattern);
if (matches) {
let layout = matches[1];
if (opts.layouts && layout[0] !== '.') {
layout = path.resolve(opts.layouts, layout);
}
const hbsLayout = path.resolve(process.cwd(), `${layout}.hbs`);
if (fs.existsSync(hbsLayout)) { // eslint-disable-line no-sync
const content = fs.readFileSync(hbsLayout, { encoding: 'utf-8' }); // eslint-disable-line no-sync
return content.replace('{{{body}}}', str);
}
const handlebarsLayout = hbsLayout.replace('.hbs', '.handlebars');
if (fs.existsSync(handlebarsLayout)) { // eslint-disable-line no-sync
const content = fs.readFileSync(handlebarsLayout, { encoding: 'utf-8' }); // eslint-disable-line no-sync
return content.replace('{{{body}}}', str);
}
}
return str;
};
module.exports = {
loadUserConfig,
parseSimpleLayout,
};