Skip to content

Commit

Permalink
refactor: destructure modules (#3992)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Dec 20, 2019
1 parent 91e5c32 commit 9b49688
Show file tree
Hide file tree
Showing 25 changed files with 90 additions and 90 deletions.
4 changes: 2 additions & 2 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const stripIndent = require('strip-indent');
const chalk = require('chalk');
const { cyan } = require('chalk');
const nunjucks = require('nunjucks');
const { inherits } = require('util');
const Promise = require('bluebird');
Expand Down Expand Up @@ -56,7 +56,7 @@ function getContextLineNums(min, max, center, amplitude) {

const LINES_OF_CONTEXT = 5;
function getContext(lines, errLine, location, type) {
const colorize = chalk.cyan;
const colorize = cyan;
const message = [
location + ' ' + type,
colorize(' ===== Context Dump ====='),
Expand Down
4 changes: 2 additions & 2 deletions lib/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tildify = require('tildify');
const Theme = require('../theme');
const Source = require('./source');
const fs = require('hexo-fs');
const chalk = require('chalk');
const { magenta } = require('chalk');
const { deepMerge } = require('hexo-util');

module.exports = ctx => {
Expand All @@ -24,7 +24,7 @@ module.exports = ctx => {
}).then(config => {
if (!config || typeof config !== 'object') return;

ctx.log.debug('Config loaded: %s', chalk.magenta(tildify(configPath)));
ctx.log.debug('Config loaded: %s', magenta(tildify(configPath)));

ctx.config = deepMerge(ctx.config, config);
config = ctx.config;
Expand Down
6 changes: 3 additions & 3 deletions lib/hexo/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { EventEmitter } = require('events');
const Promise = require('bluebird');
const Stream = require('stream');
const util = require('util');
const { inherits } = require('util');
const { Readable } = Stream;

function Router() {
Expand All @@ -12,7 +12,7 @@ function Router() {
this.routes = {};
}

util.inherits(Router, EventEmitter);
inherits(Router, EventEmitter);

const format = path => {
path = path || '';
Expand Down Expand Up @@ -108,7 +108,7 @@ function RouteStream(data) {
this.modified = data.modified;
}

util.inherits(RouteStream, Readable);
inherits(RouteStream, Readable);

RouteStream.prototype._read = function() {
const data = this._data;
Expand Down
4 changes: 2 additions & 2 deletions lib/hexo/source.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

const Box = require('../box');
const util = require('util');
const { inherits } = require('util');

function Source(ctx) {
Reflect.apply(Box, this, [ctx, ctx.source_dir]);

this.processors = ctx.extend.processor.list();
}

util.inherits(Source, Box);
inherits(Source, Box);

module.exports = Source;
10 changes: 5 additions & 5 deletions lib/plugins/console/deploy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const fs = require('hexo-fs');
const chalk = require('chalk');
const { underline, magenta } = require('chalk');
const Promise = require('bluebird');

function deployConsole(args) {
Expand All @@ -14,7 +14,7 @@ function deployConsole(args) {
help += 'You should configure deployment settings in _config.yml first!\n\n';
help += 'Available deployer plugins:\n';
help += ` ${Object.keys(deployers).join(', ')}\n\n`;
help += `For more help, you can check the online docs: ${chalk.underline('https://hexo.io/')}`;
help += `For more help, you can check the online docs: ${underline('https://hexo.io/')}`;

console.log(help);
return;
Expand Down Expand Up @@ -44,14 +44,14 @@ function deployConsole(args) {
const { type } = item;

if (!deployers[type]) {
this.log.error('Deployer not found: %s', chalk.magenta(type));
this.log.error('Deployer not found: %s', magenta(type));
return;
}

this.log.info('Deploying: %s', chalk.magenta(type));
this.log.info('Deploying: %s', magenta(type));

return Reflect.apply(deployers[type], this, [Object.assign({}, item, args)]).then(() => {
this.log.info('Deploy done: %s', chalk.magenta(type));
this.log.info('Deploy done: %s', magenta(type));
});
}).then(() => {
this.emit('deployAfter');
Expand Down
12 changes: 6 additions & 6 deletions lib/plugins/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('hexo-fs');
const { join } = require('path');
const Promise = require('bluebird');
const prettyHrtime = require('pretty-hrtime');
const chalk = require('chalk');
const { cyan, magenta } = require('chalk');
const tildify = require('tildify');
const { Transform, PassThrough } = require('stream');
const { HashStream } = require('hexo-util');
Expand Down Expand Up @@ -60,7 +60,7 @@ function generateConsole(args = {}) {
hash
}).then(() => // Write cache data to public folder
fs.writeFile(dest, cacheStream.getCache())).then(() => {
log.info('Generated: %s', chalk.magenta(path));
log.info('Generated: %s', magenta(path));
return true;
});
}).finally(() => {
Expand All @@ -73,7 +73,7 @@ function generateConsole(args = {}) {
const dest = join(publicDir, path);

return fs.unlink(dest).then(() => {
log.info('Deleted: %s', chalk.magenta(path));
log.info('Deleted: %s', magenta(path));
}, err => {
// Skip ENOENT errors (file was deleted)
if (err.cause && err.cause.code === 'ENOENT') return;
Expand All @@ -100,15 +100,15 @@ function generateConsole(args = {}) {
function firstGenerate() {
// Show the loading time
const interval = prettyHrtime(process.hrtime(start));
log.info('Files loaded in %s', chalk.cyan(interval));
log.info('Files loaded in %s', cyan(interval));

// Reset the timer for later usage
start = process.hrtime();

// Check the public folder
return fs.stat(publicDir).then(stats => {
if (!stats.isDirectory()) {
throw new Error('%s is not a directory', chalk.magenta(tildify(publicDir)));
throw new Error('%s is not a directory', magenta(tildify(publicDir)));
}
}).catch(err => {
// Create public folder if not exists
Expand All @@ -133,7 +133,7 @@ function generateConsole(args = {}) {
const interval = prettyHrtime(process.hrtime(start));
const count = result.filter(Boolean).length;

log.info('%d files generated in %s', count, chalk.cyan(interval));
log.info('%d files generated in %s', count, cyan(interval));
});
}

Expand Down
8 changes: 4 additions & 4 deletions lib/plugins/console/list/category.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict';

const chalk = require('chalk');
const { underline } = require('chalk');
const table = require('text-table');
const common = require('./common');
const { stringLength } = require('./common');

function listCategory() {
const categories = this.model('Category');

const data = categories.sort({name: 1}).map(cate => [cate.name, String(cate.length)]);

// Table header
const header = ['Name', 'Posts'].map(str => chalk.underline(str));
const header = ['Name', 'Posts'].map(str => underline(str));

data.unshift(header);

const t = table(data, {
align: ['l', 'r'],
stringLength: common.stringLength
stringLength
});

console.log(t);
Expand Down
10 changes: 5 additions & 5 deletions lib/plugins/console/list/page.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
'use strict';

const chalk = require('chalk');
const { magenta, underline, gray } = require('chalk');
const table = require('text-table');
const common = require('./common');
const { stringLength } = require('./common');

function listPage() {
const Page = this.model('Page');

const data = Page.sort({date: 1}).map(page => {
const date = page.date.format('YYYY-MM-DD');
return [chalk.gray(date), page.title, chalk.magenta(page.source)];
return [gray(date), page.title, magenta(page.source)];
});

// Table header
const header = ['Date', 'Title', 'Path'].map(str => chalk.underline(str));
const header = ['Date', 'Title', 'Path'].map(str => underline(str));

data.unshift(header);

const t = table(data, {
stringLength: common.stringLength
stringLength
});

console.log(t);
Expand Down
12 changes: 6 additions & 6 deletions lib/plugins/console/list/post.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const chalk = require('chalk');
const { gray, magenta, underline } = require('chalk');
const table = require('text-table');
const common = require('./common');
const { stringLength } = require('./common');

function mapName(item) {
return item.name;
Expand All @@ -17,21 +17,21 @@ function listPost() {
const categories = post.categories.map(mapName);

return [
chalk.gray(date),
gray(date),
post.title,
chalk.magenta(post.source),
magenta(post.source),
categories.join(', '),
tags.join(', ')
];
});

// Table header
const header = ['Date', 'Title', 'Path', 'Category', 'Tags'].map(str => chalk.underline(str));
const header = ['Date', 'Title', 'Path', 'Category', 'Tags'].map(str => underline(str));

data.unshift(header);

const t = table(data, {
stringLength: common.stringLength
stringLength
});

console.log(t);
Expand Down
10 changes: 5 additions & 5 deletions lib/plugins/console/list/tag.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict';

const chalk = require('chalk');
const { magenta, underline } = require('chalk');
const table = require('text-table');
const common = require('./common');
const { stringLength } = require('./common');

function listTag() {
const Tag = this.model('Tag');

const data = Tag.sort({name: 1}).map(tag => [tag.name, String(tag.length), chalk.magenta(tag.path)]);
const data = Tag.sort({name: 1}).map(tag => [tag.name, String(tag.length), magenta(tag.path)]);

// Table header
const header = ['Name', 'Posts', 'Path'].map(str => chalk.underline(str));
const header = ['Name', 'Posts', 'Path'].map(str => underline(str));

data.unshift(header);

const t = table(data, {
align: ['l', 'r', 'l'],
stringLength: common.stringLength
stringLength
});

console.log(t);
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/console/migrate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const chalk = require('chalk');
const { underline } = require('chalk');

function migrateConsole(args) {
// Display help message if user didn't input any arguments
Expand All @@ -17,7 +17,7 @@ function migrateConsole(args) {
help += `${type.magenta} migrator plugin is not installed.\n\n`;
help += 'Installed migrator plugins:\n';
help += ` ${Object.keys(migrators).join(', ')}\n\n`;
help += `For more help, you can check the online docs: ${chalk.underline('https://hexo.io/')}`;
help += `For more help, you can check the online docs: ${underline('https://hexo.io/')}`;

console.log(help);
return;
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/console/new.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const tildify = require('tildify');
const chalk = require('chalk');
const { magenta } = require('chalk');

const reservedKeys = {
_: true,
Expand Down Expand Up @@ -41,7 +41,7 @@ function newConsole(args) {
}

return this.post.create(data, args.r || args.replace).then(post => {
this.log.info('Created: %s', chalk.magenta(tildify(post.path)));
this.log.info('Created: %s', magenta(tildify(post.path)));
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/console/publish.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const tildify = require('tildify');
const chalk = require('chalk');
const { magenta } = require('chalk');

function publishConsole(args) {
// Display help message if user didn't input any arguments
Expand All @@ -13,7 +13,7 @@ function publishConsole(args) {
slug: args._.pop(),
layout: args._.length ? args._[0] : this.config.default_layout
}, args.r || args.replace).then(post => {
this.log.info('Published: %s', chalk.magenta(tildify(post.path)));
this.log.info('Published: %s', magenta(tildify(post.path)));
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/console/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { resolve } = require('path');
const tildify = require('tildify');
const prettyHrtime = require('pretty-hrtime');
const fs = require('hexo-fs');
const chalk = require('chalk');
const { cyan, magenta } = require('chalk');

function renderConsole(args) {
// Display help message if user didn't input any arguments
Expand Down Expand Up @@ -35,7 +35,7 @@ function renderConsole(args) {
const dest = resolve(baseDir, output);
const interval = prettyHrtime(process.hrtime(start));

log.info('Rendered in %s: %s -> %s', chalk.cyan(interval), chalk.magenta(tildify(src)), chalk.magenta(tildify(dest)));
log.info('Rendered in %s: %s -> %s', cyan(interval), magenta(tildify(src)), magenta(tildify(dest)));
return fs.writeFile(dest, result);
});
}
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/generator/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('hexo-fs');
const Promise = require('bluebird');
const { extname } = require('path');
const chalk = require('chalk');
const { magenta } = require('chalk');

const process = (name, ctx) => {
return Promise.filter(ctx.model(name).toArray(), asset => fs.exists(asset.source).tap(exist => {
Expand All @@ -25,7 +25,7 @@ const process = (name, ctx) => {
path: source,
toString: true
}).catch(err => {
ctx.log.error({err}, 'Asset render failed: %s', chalk.magenta(path));
ctx.log.error({err}, 'Asset render failed: %s', magenta(path));
});
} else {
data.data = () => fs.createReadStream(source);
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/helper/debug.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const util = require('util');
const { inspect } = require('util');

// this format object as string, resolves circular reference
function inspectObject(object, options) {
return util.inspect(object, options);
return inspect(object, options);
}

// wrapper to log to console
Expand Down
Loading

0 comments on commit 9b49688

Please sign in to comment.