-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e48a33
commit 61e85a1
Showing
5 changed files
with
69 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,44 @@ | ||
const fs = require('fs') | ||
const functions = require('./helpers/functions') | ||
|
||
|
||
/** | ||
* Read file and compile it with variables. | ||
* | ||
* | ||
* @param {string} filePath - Path to file. | ||
* @param {object} variables - Variables to compile. | ||
* @returns {string} Compiled content. | ||
*/ | ||
function compile(filePath, variables = {}) { | ||
// Read the content of the file | ||
const 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) | ||
} | ||
// Read the content of the file | ||
const 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] | ||
} | ||
if (variables.hasOwnProperty(variable)) { | ||
return variables[variable] | ||
} | ||
|
||
return match | ||
}) | ||
|
||
return match | ||
}) | ||
|
||
return replacedContent | ||
} | ||
return replacedContent | ||
} | ||
|
||
module.exports = compile | ||
module.exports = compile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
${time(now)} |