Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let promptOptions = {
type: 'expand',
name: 'answer',
default: false,
message: `${chalk.red('Overwrite')} ${path}?`,
choices: [
{ key: 'y', name: 'Yes, overwrite', value: 'overwrite' },
{ key: 'n', name: 'No, skip', value: 'skip' },
],
};
let outputPathIsFile = false;
try { outputPathIsFile = fs.statSync(this.outputPath).isFile(); } catch (err) { /* ignore */ }
let canDiff = (
!isBinaryFile(this.inputPath) && (
!outputPathIsFile ||
!isBinaryFile(this.outputPath)
)
);
if (canDiff) {
promptOptions.choices.push({ key: 'd', name: 'Diff', value: 'diff' });
if (canEdit()) {
promptOptions.choices.push({ key: 'e', name: 'Edit', value: 'edit' });
}
}
return this.ui.prompt(promptOptions)
.then(response => response.answer);
}
var isBinary = function (buffer) {
return (
// First, check magic numbers to see if we are a possible text file.
//
// _Note_: While a `sync`-named method, there's no actual sync I/O when
// size parameter is provided.
isBinaryFile.sync(buffer, buffer.length) ||
// Then check if we have known non-text file types.
!!fileType(buffer)
);
};
isBinaryFile (relativeFile) {
const file = path.resolve(this.targetDir, relativeFile)
try {
return isBinaryFile.sync(file)
} catch (e) {
// File doesn't exist or is a directory, so it isn't a binary file
if (e.message.includes('ENOENT')) {
return false
}
throw e
}
}
function renderFile (name, data, ejsOptions) {
if (isBinary.sync(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
static readFile(pathstr) {
if (isBinaryFile.sync(pathstr)) {
return fs.readFileSync(pathstr);
}
return fs.readFileSync(pathstr, 'utf8');
}
export function openFile(target: string) {
if (isBinaryFile.sync(target)) {
if (platform.win) {
childProcess.exec(`explorer "${target}"`);
}
if (platform.mac) {
childProcess.exec(`open "${target}"`);
}
} else {
vscode.workspace.openTextDocument(target).then(doc => {
const activeEditor = vscode.window.activeTextEditor;
const column = activeEditor && activeEditor.viewColumn || 1;
vscode.window.showTextDocument(doc, column);
});
}
}
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] = isBinary.sync(name)
? fs.readFileSync(name)
: fs.readFileSync(name, 'utf-8')
}
return normalizeFilePaths(res)
}
_.forOwn(files, function (file, filepath) {
if (utils_1.default.template.isFileSkipped(filepath, filter))
return;
var contents = file.contents;
if (isBinary.sync(contents, contents.length))
return;
var fileSchema = utils_1.default.handlebars.getSchema(contents.toString());
_.extend(filesVariables, fileSchema);
});
return [4 /*yield*/, utils_1.default.loadJSON(path.join(config_1.default.directory, config_1.default.templateConfigName))];
async function render ( files, metalsmith, next ) {
const metadata = metalsmith.metadata (),
render = pify ( consolidate.handlebars.render ),
paths = Object.keys ( files );
for ( let path of paths ) {
const {contents} = files[path];
if ( isBinary.sync ( contents, contents.length ) ) continue;
if ( Utils.template.isFileSkipped ( path, metadata.schema.filter ) ) continue;
const template = contents.toString (),
rendered = await render ( template, metadata.renderVariables );
files[path].contents = new Buffer ( rendered );
}
next ();
}
export function isBinaryFile (src) {
return isBinary.sync(src)
}