How to use hexo-fs - 10 common examples

To help you get started, we’ve selected a few hexo-fs 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 hexojs / hexo / test / scripts / console / generate.js View on Github external
it('write file if not exist', () => {
    const src = pathFn.join(hexo.source_dir, 'test.txt');
    const dest = pathFn.join(hexo.public_dir, 'test.txt');
    const content = 'test';

    // Add some source files
    return fs.writeFile(src, content).then(() => // First generation
      generate()).then(() => // Delete generated files
      fs.unlink(dest)).then(() => // Second generation
      generate()).then(() => fs.readFile(dest)).then(result => {
      result.should.eql(content);

      // Remove source files and generated files
      return Promise.all([
        fs.unlink(src),
        fs.unlink(dest)
      ]);
    });
  });
github hexojs / hexo / test / scripts / hexo / multi_config_path.js View on Github external
it('write multiconfig to specified path', () => {
    const outputPath = osFn.tmpdir();
    const combinedPath = pathFn.join(outputPath, '_multiconfig.yml');

    mcp(base, 'test1.yml', outputPath).should.not.eql(combinedPath);
    mcp(base, 'test1.yml,test2.yml', outputPath).should.eql(combinedPath);
    mcp(base, 'test1.yml,test1.json', outputPath).should.eql(combinedPath);
    mcp(base, 'test1.json,test2.json', outputPath).should.eql(combinedPath);
    mcp(base, 'notafile.yml,test1.json', outputPath).should.eql(combinedPath);
    mcp(base, 'notafile.yml,alsonotafile.json', outputPath).should.not.eql(combinedPath);

    // delete /tmp/_multiconfig.yml
    fs.unlinkSync(combinedPath);

    hexo.log.reader[1].type.should.eql('debug');
    hexo.log.reader[1].msg.should.eql(`Writing _multiconfig.yml to ${combinedPath}`);
    hexo.log.reader[2].type.should.eql('info');
    hexo.log.reader[2].msg.should.eql('Config based on 2 files');
    hexo.log.reader[6].type.should.eql('warning');
    hexo.log.reader[6].msg.should.eql('Config file notafile.yml not found.');
    hexo.log.reader[7].type.should.eql('info');
    hexo.log.reader[7].msg.should.eql('Config based on 1 files');
    hexo.log.reader[11].type.should.eql('error');
    hexo.log.reader[11].msg.should.eql('No config files found.'
                                     + ' Using _config.yml.');
  });
});
github hexojs / hexo / test / scripts / console / generate.js View on Github external
generate()).then(() => // Change the generated file
      fs.writeFile(dest, newContent)).then(() => // Second generation
      generate()).then(() => // Read the generated file
github hexojs / hexo-deployer-git / test / deployer.js View on Github external
return spawn('git', ['clone', fakeRemote, validateDir, '--branch', branch]).then(() => {
      // Check the branch name
      return fs.readFile(pathFn.join(validateDir, '.git', 'HEAD'));
    }).then(content => {
      content.trim().should.eql('ref: refs/heads/' + branch);
github hexojs / hexo / lib / plugins / tag / include_code.js View on Github external
}

  let title = '';
  let path = '';
  if (rCaptionTitleFile.test(arg)) {
    const match = arg.match(rCaptionTitleFile);
    title = match[1];
    path = match[3];
  }

  // Exit if path is not defined
  if (!path) return;

  const src = join(ctx.source_dir, codeDir, path);

  return fs.exists(src).then(exist => {
    if (exist) return fs.readFile(src);
  }).then(code => {
    if (!code) return;

    code = stripIndent(code).trim();

    if (!config.enable) {
      return `<pre><code>${code}</code></pre>`;
    }

    // If the title is not defined, use file name instead
    title = title || basename(path);

    // If the language is not defined, use file extension instead
    lang = lang || extname(path).substring(1);
github jaredly / hexo-admin / update.js View on Github external
fs.writeFile(full_source, raw, function(err){
      if (err) return callback(err);

      if (full_source !== prev_full) {
        fs.unlinkSync(prev_full)
        // move asset dir
        var assetPrev = removeExtname(prev_full);
        var assetDest = removeExtname(full_source);
        fs.exists(assetPrev).then(function(exist) {
          if (exist) {
            fs.copyDir(assetPrev, assetDest).then(function() {
              fs.rmdir(assetPrev);
            });
          }
        });
      }
      hexo.source.process([post.source]).then(function () {
  //      console.log(post.full_source, post.source)
        callback(null, hexo.model(model).get(id));
      });
    });
  });
github Troy-Yang / hexo-lazyload-image / lib / process.js View on Github external
function lazyProcess(htmlContent) {
    let defaultImagePath = __dirname + '/default-image.json';
    let loadingImage = this.config.lazyload.loadingImg;

    if (!loadingImage) {
        loadingImage = JSON.parse(fs.readFileSync(defaultImagePath)).default;
    }

    return htmlContent.replace(//gi, function (str, p1, p2) {
        // might be duplicate
        if(/data-original/gi.test(str)){
            return str;
        }
        if(/src="data:image(.*?)/gi.test(str)) {
            return str;
        }
        if(/no-lazy/gi.test(str)) {
            return str;
        }
        return str.replace(p2, loadingImage + '" data-original="' + p2);
    });
}
github hexojs / hexo / lib / hexo / post.js View on Github external
if (data.layout === 'draft') data.layout = 'post';

  const ctx = this.context;
  const { config } = ctx;
  const draftDir = join(ctx.source_dir, '_drafts');
  const slug = slugize(data.slug.toString(), {transform: config.filename_case});
  data.slug = slug;
  const regex = new RegExp(`^${escapeRegExp(slug)}(?:[^\\/\\\\]+)`);
  let src = '';
  const result = {};

  data.layout = (data.layout || config.default_layout).toLowerCase();

  // Find the draft
  return fs.listDir(draftDir).then(list => {
    return list.find(item => regex.test(item));
  }).then(item => {
    if (!item) throw new Error(`Draft "${slug}" does not exist.`);

    // Read the content
    src = join(draftDir, item);
    return fs.readFile(src);
  }).then(content => {
    // Create post
    Object.assign(data, yfm(content));
    data.content = data._content;
    delete data._content;

    return this.create(data, replace).then(post => {
      result.path = post.path;
      result.content = post.content;
github mimming / snippets / hexo-sheets-plugin / blog / scripts / hexo-posts-from-csv.js View on Github external
entry.should.have.property('imageurl');
      entry.should.have.property('slug');
      entry.should.not.have.property('localimage');

      // TODO: deal with urls that do not parse
      const imageUrl = url.parse(entry.imageurl);
      const imageFilename = imageUrl.pathname.split('/').pop();

      // TODO: figure out if this is the safest way to construct local image paths
      // Construct the local image path
      const localPath = `${hexo.config.source_dir}/images/${entry.slug}_${imageFilename}`;
      const relativeUrl = `/images/${entry.slug}_${imageFilename}`;

      // stat each image as a jpg, then as png, see if it exists
      try {
        fs.statSync(localPath);
        // if exists, add localimage to entry
        entry['localimage'] = relativeUrl;
      } catch (error) {
        // Do nothing!  Pass the entry through
      }
    });
    resolve(entries);
github jaredly / hexo-admin / update.js View on Github external
fs.writeFile(full_source, raw, function(err){
      if (err) return callback(err);

      if (full_source !== prev_full) {
        fs.unlinkSync(prev_full)
        // move asset dir
        var assetPrev = removeExtname(prev_full);
        var assetDest = removeExtname(full_source);
        fs.exists(assetPrev).then(function(exist) {
          if (exist) {
            fs.copyDir(assetPrev, assetDest).then(function() {
              fs.rmdir(assetPrev);
            });
          }
        });
      }
      hexo.source.process([post.source]).then(function () {
  //      console.log(post.full_source, post.source)
        callback(null, hexo.model(model).get(id));
      });
    });