-
Notifications
You must be signed in to change notification settings - Fork 5
/
build-utils.js
49 lines (38 loc) · 1.2 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
/* eslint-env node */
import n from 'nunjucks';
import fs from 'fs';
const { BUILD_ENV, NODE_ENV } = process.env;
export const production = NODE_ENV === 'production';
const buildTarget = BUILD_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()];
const combineSettings = (currentData, path) => {
return {
...currentData,
...require(path),
};
};
return settingsFiles.reduce(combineSettings, { NODE_ENV });
};
const data = getData();
const nunjucks = n.configure({
tags: {
variableStart: '{$',
variableEnd: '$}',
},
});
export const compileTemplate = (template) => {
return nunjucks.renderString(template, data);
};
export const compileBufferTemplate = (body) => compileTemplate(body.toString());