Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
style: move print into new function, fix null of undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Feb 15, 2018
1 parent c348707 commit 6431600
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
/* global hexo */

const _ = require('lodash');
const colors = require('colors');
const fs = require('hexo-fs');
const path = require('path');
const url = require('url');
Expand All @@ -13,6 +12,7 @@ const buildGeneratorsFromManifest = require('./lib/buildGeneratorsFromManifest')
const getFileMD5 = require('./lib/getFileMD5');
const getNodeModulePath = require('./lib/getNodeModulePath');
const loadModelFrom = require('./lib/loadModelFrom');
const print = require('./lib/print');

const generators = [];

Expand Down Expand Up @@ -86,7 +86,7 @@ if (config.enable) {
_.unset(config, 'enable');
if (_.hasIn(config, 'model.use')) {

let modelJsonUrl;
let modelJsonUrl = null;
let tryPath = path.resolve(hexo.base_dir, './live2d_models/', config.model.use);
if (fs.existsSync(tryPath)) {

Expand All @@ -100,7 +100,7 @@ if (config.enable) {
} = loadModelFrom(tryPath, onSiteModelPath);
modelJsonUrl = pkgModelJsonUrl;
generators.push(...modelGenerators);
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from live2d_models folder(2), '${url.parse(modelJsonUrl).pathname}' from '${tryPath}'`);
print.log(`Loaded model from live2d_models folder(2), '${url.parse(modelJsonUrl).pathname}' from '${tryPath}'`);

} else {

Expand All @@ -117,16 +117,16 @@ if (config.enable) {
} = loadModelFrom(tryPath, onSiteModelPath);
modelJsonUrl = pkgModelJsonUrl;
generators.push(...modelGenerators);
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from hexo base releated path(3), '${url.parse(modelJsonUrl).pathname}' from '${tryPath}'`);
print.log(`Loaded model from hexo base releated path(3), '${url.parse(modelJsonUrl).pathname}' from '${tryPath}'`);

} else if (getNodeModulePath(config.model.use) === '') {
} else if (getNodeModulePath(config.model.use) === null) {

/*
* Is custom(4)
* Use custom
*/
modelJsonUrl = config.model.use;
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded Model from custom(4), at '${modelJsonUrl}'`);
print.log(`Loaded Model from custom(4), at '${modelJsonUrl}'`);

} else {

Expand All @@ -144,10 +144,15 @@ if (config.enable) {
} = loadModelFrom(assetsDir, onSiteModelPath);
modelJsonUrl = pkgModelJsonUrl;
generators.push(...modelGenerators);
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from npm-module(1), ${packageJsonObj.name}@${packageJsonObj.version} from '${assetsDir}'`);
print.log(`Loaded model from npm-module(1), ${packageJsonObj.name}@${packageJsonObj.version} from '${assetsDir}'`);

}

}
if (modelJsonUrl === null) {

print.error('Did not found model json');

}
_.unset(config, 'model.use');
config = _.set(config, 'model.jsonPath', modelJsonUrl);
Expand All @@ -162,7 +167,7 @@ if (config.enable) {

hexo.extend.helper.register('live2d', () => {

console.warn(`${colors.green('hexo-helper-live2d'.toUpperCase())}: live2d tag was deprecated since 3.0. See #36. PLEASE REMOVE live2d TAG IN YOUR TEMPLATE FILE.`);
print.warn('live2d tag was deprecated since 3.0. See #36. PLEASE REMOVE live2d TAG IN YOUR TEMPLATE FILE.');

});

Expand Down
4 changes: 2 additions & 2 deletions lib/getNodeModulePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const path = require('path');
/**
* Resolve the package path according to provieded package name
* @param {String} packageName Package need to be resolved
* @return {String} If detected the package, the path; if not, ''
* @return {String} If detected the package, the path; if not, null
*/

module.exports = function getNodeModulePath (packageName) {
Expand All @@ -19,7 +19,7 @@ module.exports = function getNodeModulePath (packageName) {

} catch (e) {

return '';
return null;

}

Expand Down
4 changes: 2 additions & 2 deletions lib/loadModelFrom.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const url = require('url');
/**
* @typedef {Object} ModelPackageInfo
* @property {Array} modelGenerators The generator-use-array for Hexo
* @property {String} modelJsonUrl The model.json file path
* @property {String} modelJsonUrl The model.json file path, null if not found
*/

/**
Expand All @@ -34,7 +34,7 @@ module.exports = function loadModelFrom (assetsDir, rootUrl) {
}
return p;

}, undefined);
}, null);

return {
modelGenerators,
Expand Down
45 changes: 45 additions & 0 deletions lib/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @description Package of IO in console
*/


const colors = require('colors');

/**
* Print log
* @param {...String} argvs Strings to print
* @return {undefined}
*/
function log (...argvs) {

console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: ${argvs}`);

}

/**
* Print warn
* @param {...String} argvs Strings to print
* @return {undefined}
*/
function warn (...argvs) {

console.log(`${colors.yellow('hexo-helper-live2d'.toUpperCase())}: ${argvs}`);

}

/**
* Print error
* @param {...String} argvs Strings to print
* @return {undefined}
*/
function error (...argvs) {

console.log(`${colors.red('hexo-helper-live2d'.toUpperCase())}: ${argvs}`);

}

module.exports = {
error,
log,
warn,
};

0 comments on commit 6431600

Please sign in to comment.