Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function converts(text: string | Buffer, to: string) {
if (Buffer.isBuffer(text)) {
if (isBinaryFileSync(text)) return text; // don't touch binary files
newLines.forEach((newLine) => {
// $FlowFixMe text is Buffer here
if (newLine !== to) text = replaceBuffer(text, newLine, to);
});
return text;
}
return text.toString().replace(newline, to);
}
function renderFile (name, data, ejsOptions) {
if (isBinaryFileSync(name)) {
return fs.readFileSync(name) // return buffer
}
const template = fs.readFileSync(name, 'utf-8')
// custom template inheritance via yaml front matter.
// ---
// extend: 'source-file'
// replace: !!js/regexp /some-regex/
// OR
// replace:
// - !!js/regexp /foo/
// - !!js/regexp /bar/
// ---
const yaml = require('yaml-front-matter')
const parsed = yaml.loadFront(template)
const content = parsed.__content
function render(contents, filename, context, tplSettings) {
let result;
const contentsBuffer = Buffer.from(contents, 'binary');
if (isBinaryFileSync(contentsBuffer, contentsBuffer.length)) {
result = contentsBuffer;
} else {
result = ejs.render(
contents.toString(),
context,
// Setting filename by default allow including partials.
extend({filename: filename}, tplSettings)
);
}
return result;
}
file =>
INCLUDED_PATTERNS.some(pattern => pattern.test(file)) &&
!IGNORED_PATTERNS.some(pattern => pattern.test(file)) &&
!isDirectory(file) &&
!isBinaryFileSync(file) &&
needsCopyrightHeader(file)
);
utils.contentType = function(p) {
const type = mimeTypes.lookup(p);
if (overrideMimeTypes[type]) return overrideMimeTypes[type];
if (type) {
const charset = mimeTypes.charsets.lookup(type);
return type + (charset ? "; charset=" + charset : "");
} else {
try {
return isbinaryfile.isBinaryFileSync(p) ? "application/octet-stream" : "text/plain";
} catch (err) {
return "application/octet-stream";
}
}
};
function copyWithReplace(src, dest, substitutions) {
mkdirp(path.dirname(dest));
if (fs.lstatSync(src).isDirectory()) {
const files = fs.readdirSync(src)
files.forEach((relativeChildSrc) => {
const childSrc = path.join(src, relativeChildSrc);
const childDest = path.join(dest, path.basename(childSrc));
copyWithReplace(childSrc, childDest, substitutions);
});
} else if (!isBinary(src)) {
let content = fs.readFileSync(src, {encoding: 'utf-8'});
substitutions.forEach(r => {
const [regexp, newvalue] = r;
content = content.replace(regexp, newvalue);
});
fs.writeFileSync(dest, content);
} else {
fs.copyFileSync(src, dest);
}
}
module.exports = async function readFiles (context) {
const files = await globby(['**'], {
cwd: context,
onlyFiles: true,
gitignore: true,
ignore: ['**/node_modules/**', '**/.git/**'],
dot: true
})
const res = {}
for (const file of files) {
const name = path.resolve(context, file)
res[file] = isBinaryFileSync(name)
? fs.readFileSync(name)
: fs.readFileSync(name, 'utf-8')
}
return normalizeFilePaths(res)
}
files.forEach((file) => {
if (ignoreFiles.includes(file)) {
return;
}
const fullPath = path.join(dirPath, file);
stats = fs.lstatSync(fullPath);
if (stats.isDirectory()) {
list = list.concat(recursiveReaddirSync(fullPath, rootDir));
} else if (!isBinaryFileSync(fullPath)) {
list.push([path.relative(rootDir, fullPath), fullPath]);
}
});
if (answer.action === 'overwriteAll') {
overwrite = 'overwriteAll'
}
else if (answer.action === 'skipAll') {
overwrite = 'skipAll'
continue
}
else if (answer.action === 'skip') {
continue
}
}
}
fs.ensureFileSync(targetPath)
if (rawCopy || isBinary(sourcePath)) {
fs.copyFileSync(sourcePath, targetPath)
}
else {
const rawContent = fs.readFileSync(sourcePath, 'utf-8')
const template = compileTemplate(rawContent, { 'interpolate': /<%=([\s\S]+?)%>/g })
fs.writeFileSync(targetPath, template(scope), 'utf-8')
}
}
}