Update file.js
This commit is contained in:
parent
34c96ae885
commit
a5e1c15c1b
19
src/file.js
19
src/file.js
|
@ -6,6 +6,8 @@ module.exports.write = write
|
||||||
module.exports.resolve = resolve
|
module.exports.resolve = resolve
|
||||||
module.exports.join = join
|
module.exports.join = join
|
||||||
module.exports.dirname = dirname
|
module.exports.dirname = dirname
|
||||||
|
module.exports.templateVariables = templateVariables
|
||||||
|
module.exports.templateFormat = templateFormat
|
||||||
|
|
||||||
function read(filepath) {
|
function read(filepath) {
|
||||||
return fs.readFileSync(path.resolve(filepath), { encoding: 'utf-8' })
|
return fs.readFileSync(path.resolve(filepath), { encoding: 'utf-8' })
|
||||||
|
@ -31,3 +33,20 @@ function join(path1, path2) {
|
||||||
function dirname(filepath) {
|
function dirname(filepath) {
|
||||||
return path.dirname(filepath)
|
return path.dirname(filepath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function templateVariables(template) {
|
||||||
|
const match = template.match(/{[^}]+}/g)
|
||||||
|
|
||||||
|
return Array.isArray(match) ? match.map(s => s.substring(1, s.length - 1)) : []
|
||||||
|
}
|
||||||
|
|
||||||
|
function templateFormat(template, obj) {
|
||||||
|
let output = template
|
||||||
|
for (let key in obj) {
|
||||||
|
const regex = new RegExp(`{${key}}`, 'g')
|
||||||
|
const value = obj[key] || undefined
|
||||||
|
output = output.replace(regex, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue