Skip to content

Commit

Permalink
Merge pull request #156 from mkwtys/fix-test
Browse files Browse the repository at this point in the history
fix test
  • Loading branch information
mkwtys authored Dec 16, 2024
2 parents e74bb0c + 0c77c1d commit 0862d27
Show file tree
Hide file tree
Showing 5 changed files with 2,328 additions and 1,337 deletions.
55 changes: 30 additions & 25 deletions lib/index.js
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;
Loading

0 comments on commit 0862d27

Please sign in to comment.