-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (40 loc) · 1.53 KB
/
index.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
const fs = require('fs')
const functions = require('./helpers/functions')
/**
* Read file and compile it with variables.
*
* @param {string} filePath - Path to file (if you just want to compile a string, pass null).
* @param {object} variables - Variables to compile.
* @param {string} content - File content to compile (if you don't have a file URL).
* @returns {string} Compiled content.
*/
function compile(filePath = null, variables = {}, fileContent = null) {
let content = fileContent
// Read the content of the file
if (filePath) {
content = fs.readFileSync(filePath, 'utf8')
}
// Regex pattern for variables. It can be ${name} or ${f(name)}
const pattern = /\$\{(\w+|\w+\(\w+\))\}/g
// Replace all variables in the content with their corresponding values
const replacedContent = content.replace(pattern, (match, variable) => {
// check if has a function in pattern
const functionMatch = variable.match(/\w+\((\w+)\)/)
if (functionMatch) {
const functionName = variable.split('(')[0]
const functionArg = functionMatch[1]
const functionArgValue = variables[functionArg]
if (functions.hasOwnProperty(functionName) && functionArgValue) {
return functions[functionName](functionArgValue)
} else if (functions.nonArgumentFunctions.hasOwnProperty(functionName)) {
return functions[functionName]()
}
}
if (variables.hasOwnProperty(variable)) {
return variables[variable]
}
return match
})
return replacedContent
}
module.exports = compile