How to use the istextorbinary.isTextSync function in istextorbinary

To help you get started, we’ve selected a few istextorbinary examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github x-orpheus / nei-toolkit / lib / nei / template.js View on Github external
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;
github bevry-archive / feedr / source / plugins / string.js View on Github external
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())
	}
}
github eggjs / egg-init / lib / init_command.js View on Github external
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;
github stefanpenner / async-disk-cache / index.js View on Github external
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);
  };
}
github hawkeyesec / scanner-cli / lib / util.js View on Github external
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) {

istextorbinary

Determine if a filename and/or buffer is text or binary. Smarter detection than the other solutions.

Artistic-2.0
Latest version published 12 months ago

Package Health Score

79 / 100
Full package analysis