Skip to content

Commit

Permalink
refactor(hexo/index): destructure modules (#3953)
Browse files Browse the repository at this point in the history
* refactor(hexo/index): destructure modules
* style: object-shorthand
  • Loading branch information
curbengh authored and SukkaW committed Dec 14, 2019
1 parent e4a5292 commit 9cb8258
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const { sep, join, dirname } = require('path');
const tildify = require('tildify');
const Database = require('warehouse');
const _ = require('lodash');
const chalk = require('chalk');
const { magenta, underline } = require('chalk');
const { EventEmitter } = require('events');
const fs = require('hexo-fs');
const { readFile } = require('hexo-fs');
const Module = require('module');
const vm = require('vm');
const pkg = require('../../package.json');
const { runInThisContext } = require('vm');
const { version } = require('../../package.json');
const logger = require('hexo-log');
const extend = require('../extend');
const { Console, Deployer, Filter, Generator, Helper, Migrator, Processor, Renderer, Tag } = require('../extend');
const Render = require('./render');
const registerModels = require('./register_models');
const Post = require('./post');
Expand All @@ -24,7 +24,7 @@ const Locals = require('./locals');
const defaultConfig = require('./default_config');
const loadDatabase = require('./load_database');
const multiConfigPath = require('./multi_config_path');
const resolve = require('resolve');
const { sync } = require('resolve');
const full_url_for = require('../plugins/helper/full_url_for');
const { inherits } = require('util');

Expand Down Expand Up @@ -56,18 +56,18 @@ const createLoadThemeRoute = function(generatorResult, locals, ctx) {
const view = theme.getView(name);

if (view) {
log.debug(`Rendering HTML ${name}: ${chalk.magenta(path)}`);
log.debug(`Rendering HTML ${name}: ${magenta(path)}`);
return view.render(locals).tap(result => {
if (useCache) {
routeCache.set(generatorResult, result);
}
}).tapCatch(err => {
log.error({ err }, `Render HTML failed: ${chalk.magenta(path)}`);
log.error({ err }, `Render HTML failed: ${magenta(path)}`);
});
}
}

log.warn(`No layout: ${chalk.magenta(path)}`);
log.warn(`No layout: ${magenta(path)}`);
};
};

Expand Down Expand Up @@ -104,21 +104,21 @@ class Hexo {
safe: Boolean(args.safe),
silent: Boolean(args.silent),
env: process.env.NODE_ENV || 'development',
version: pkg.version,
version,
cmd: args._ ? args._[0] : '',
init: false
};

this.extend = {
console: new extend.Console(),
deployer: new extend.Deployer(),
filter: new extend.Filter(),
generator: new extend.Generator(),
helper: new extend.Helper(),
migrator: new extend.Migrator(),
processor: new extend.Processor(),
renderer: new extend.Renderer(),
tag: new extend.Tag()
console: new Console(),
deployer: new Deployer(),
filter: new Filter(),
generator: new Generator(),
helper: new Helper(),
migrator: new Migrator(),
processor: new Processor(),
renderer: new Renderer(),
tag: new Tag()
};

this.config = Object.assign({}, defaultConfig);
Expand Down Expand Up @@ -211,8 +211,8 @@ class Hexo {
}

init() {
this.log.debug('Hexo version: %s', chalk.magenta(this.version));
this.log.debug('Working directory: %s', chalk.magenta(tildify(this.base_dir)));
this.log.debug('Hexo version: %s', magenta(this.version));
this.log.debug('Working directory: %s', magenta(tildify(this.base_dir)));

// Load internal plugins
require('../plugins/console')(this);
Expand Down Expand Up @@ -260,7 +260,7 @@ class Hexo {

try {
// Try to resolve the plugin with the resolve.sync.
return resolve.sync(name, { basedir: baseDir });
return sync(name, { basedir: baseDir });
} catch (err) {
// There was an error (likely the plugin wasn't found), so return a possibly
// non-existing path that a later part of the resolution process will check.
Expand All @@ -269,7 +269,7 @@ class Hexo {
}

loadPlugin(path, callback) {
return fs.readFile(path).then(script => {
return readFile(path).then(script => {
// Based on: https://github.com/joyent/node/blob/v0.10.33/src/node.js#L516
const module = new Module(path);
module.filename = path;
Expand All @@ -287,7 +287,7 @@ class Hexo {

script = `(function(exports, require, module, __filename, __dirname, hexo){${script}});`;

const fn = vm.runInThisContext(script, path);
const fn = runInThisContext(script, path);

return fn(module.exports, require, module, path, dirname(path), this);
}).asCallback(callback);
Expand Down Expand Up @@ -378,7 +378,7 @@ class Hexo {
const generator = generators[key];

return Reflect.apply(generator, this, [siteLocals]).then(data => {
log.debug('Generator: %s', chalk.magenta(key));
log.debug('Generator: %s', magenta(key));
return data;
});
}).reduce((result, data) => {
Expand Down Expand Up @@ -443,7 +443,7 @@ class Hexo {
this.log.fatal(
{err},
'Something\'s wrong. Maybe you can find the solution here: %s',
chalk.underline('https://hexo.io/docs/troubleshooting.html')
underline('https://hexo.io/docs/troubleshooting.html')
);
}

Expand All @@ -469,7 +469,7 @@ Hexo.prototype.lib_dir = libDir + sep;
Hexo.core_dir = dirname(libDir) + sep;
Hexo.prototype.core_dir = dirname(libDir) + sep;

Hexo.version = pkg.version;
Hexo.prototype.version = pkg.version;
Hexo.version = version;
Hexo.prototype.version = version;

module.exports = Hexo;

0 comments on commit 9cb8258

Please sign in to comment.