Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
files.forEach(file =>{
let fileName = path.join(this.output, path.relative(this.path,file));
const buffer = fs.readFileSync(file);
let shouldCompile = true;
if(!isTextOrBinary.isTextSync(fileName, buffer)){//如果测试为binary文件,就不解析content,当然文件名还是得解析的
logger.log("debug", {message: `文件${fileName}为二进制文件,不解析文件内容,直接复制`});
shouldCompile = false;
}
//解析文件名
try {
// 使用strict模式,会提示未定义field错误
fileName = Handlebars.compile(fileName, {strict: true})(this.data);
} catch (e) {
logger.log('error', {message: `生成文件${fileName} 时出现错误: ${e}`});
fileName = Handlebars.compile(fileName)(this.data); // 非严格模式下重新编译
}
let fileContent;
if(shouldCompile) {
if (fs.existsSync(fileName) && !this.overwrite) {
logger.log("debug", {message: `${fileName} 已存在,将保留该文件内容`});
return;
module.exports.parse = function parseString({ feed, data }, next) {
// Detect
const isText = require('istextorbinary').isTextSync(feed.basename, data)
if (!isText) {
next()
} else {
// Parse
next(null, data.toString())
}
}
files.forEach(file => {
const from = path.join(src, file);
const to = path.join(targetDir, this.replaceTemplate(this.fileMapping[file] || file, locals));
const content = fs.readFileSync(from);
this.log('write to %s', to);
// check if content is a text file
const result = isTextOrBinary.isTextSync(from, content)
? this.replaceTemplate(content.toString('utf8'), locals)
: content;
mkdirp.sync(path.dirname(to));
fs.writeFileSync(to, result);
});
return files;
return async (fileStream) => {
let value = await decompress(fileStream);
// is Buffer or string? >:D
if (!this.supportBuffer || require('istextorbinary').isTextSync(false, value)) {
debug('convert to string');
value = value.toString();
} else {
debug('keep data as Buffer');
}
return new CacheEntry(true, filePath, value);
};
}
readFileSync: function (absolute) {
const stat = fs.statSync(absolute)
if (stat.size > 1000000) {
console.warn(('[warn] File which exceeds 1MB limited detected: ' + absolute).yellow)
return ''
}
const buffer = fs.readFileSync(absolute)
if (!istext.isTextSync(absolute, buffer)) {
console.warn(('[warn] Binary file detected when expected text: ' + absolute).yellow)
return ''
}
const contents = buffer.toString().trim()
return contents
},
readFile: function (absolute, done) {