-
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.
Merge pull request #156 from mkwtys/fix-test
fix test
- Loading branch information
Showing
5 changed files
with
2,328 additions
and
1,337 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,50 @@ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const glob = require('glob'); | ||
const glob2base = require('glob2base'); | ||
const mkdirp = require('mkdirp'); | ||
const path = require('path'); | ||
const fs = require("fs"); | ||
const { globSync } = require("glob"); | ||
const mkdirp = require("mkdirp"); | ||
const path = require("path"); | ||
|
||
const isTemplateLiteral = (str) => { | ||
const start = str.charAt(0); | ||
const end = str.charAt(str.length - 1); | ||
return start === '`' && end === '`'; | ||
return start === "`" && end === "`"; | ||
}; | ||
|
||
const createFunctionBody = (str) => { | ||
if (isTemplateLiteral(str)) { | ||
return 'return ' + str + ';'; | ||
return "return " + str + ";"; | ||
} | ||
|
||
return 'return `' + str + '`;'; | ||
return "return `" + str + "`;"; | ||
}; | ||
|
||
function backtick(src, dest, data, opt) { | ||
async function backtick(src, dest, data, opt) { | ||
const options = opt || {}; | ||
const base = options.base || glob2base(new glob.Glob(src)); | ||
const templates = glob.sync(src, { nodir: true }); | ||
const base = options.base || ''; | ||
const templates = globSync(src, { nodir: true }); | ||
|
||
if (templates.length === 0) { | ||
return Promise.reject(new Error('no match template file')); | ||
return Promise.reject(new Error("no match template file")); | ||
} | ||
|
||
return Promise.all(templates.map((template) => { | ||
return Promise.resolve().then(() => { | ||
const content = fs.readFileSync(path.resolve(template), 'utf8').trim(); | ||
const funcBody = createFunctionBody(content); | ||
const keys = Object.keys(data); | ||
const values = keys.map((key) => { return data[key]; }); | ||
const output = new (Function.prototype.bind.apply(Function, [null].concat(keys, [funcBody])))().apply(undefined, values); | ||
const filePath = path.resolve(dest, template.replace(base, '')); | ||
mkdirp.sync(path.dirname(filePath)); | ||
fs.writeFileSync(filePath, output, 'utf8'); | ||
}); | ||
})); | ||
return Promise.all( | ||
templates.map((template) => { | ||
return Promise.resolve().then(() => { | ||
const content = fs.readFileSync(path.resolve(template), "utf8").trim(); | ||
const funcBody = createFunctionBody(content); | ||
const keys = Object.keys(data); | ||
const values = keys.map((key) => { | ||
return data[key]; | ||
}); | ||
const output = new (Function.prototype.bind.apply( | ||
Function, | ||
[null].concat(keys, [funcBody]) | ||
))().apply(undefined, values); | ||
const filePath = path.resolve(dest, template.replace(base, "")); | ||
mkdirp.sync(path.dirname(filePath)); | ||
fs.writeFileSync(filePath, output, "utf8"); | ||
}); | ||
}) | ||
); | ||
} | ||
|
||
module.exports = backtick; |
Oops, something went wrong.