forked from gdg-x/hoverboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-utils.js
76 lines (60 loc) · 1.69 KB
/
build-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
65
66
67
68
69
70
71
72
73
74
75
76
/* eslint-env node */
const n = require('nunjucks');
const fs = require('fs');
const production = process.env.NODE_ENV && process.env.NODE_ENV === 'production';
const development = !production;
const buildTarget = process.env.BUILD_ENV
? process.env.BUILD_ENV
: production
? 'production'
: 'development';
const getConfigPath = () => {
const path = `./config/${buildTarget}.json`;
if (!fs.existsSync(path)) {
throw new Error(`
ERROR: Config path '${path}' does not exists.
Please, use production|development.json files or add a configuration file at '${path}'.
`);
}
console.log(`File path ${path} selected as config...`);
return path;
};
const getData = () => {
const settingsFiles = ['./data/resources.json', './data/settings.json', getConfigPath()];
return settingsFiles.reduce((currentData, path) => {
return {
...currentData,
...require(path),
};
}, {});
};
const data = getData();
const nunjucks = n.configure({
tags: {
variableStart: '{$',
variableEnd: '$}',
},
});
const isTemplate = ({ url, contentType }) => {
const templateTypes = [
'application/javascript',
'application/json',
'text/html',
'text/markdown',
'video/mp2t', // TypeScript
];
if (isNodeModule({ url })) {
return false;
}
return templateTypes.some((templateType) => contentType.startsWith(templateType));
};
const compileTemplate = (template) => nunjucks.renderString(template, data);
const compileBufferTemplate = (body) => compileTemplate(body.toString());
const isNodeModule = ({ url }) => url.startsWith('/node_modules/');
module.exports = {
compileBufferTemplate,
compileTemplate,
development,
isTemplate,
production,
};