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

Commit

Permalink
style: crazy lint
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Feb 14, 2018
1 parent 40ba141 commit c348707
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 49 deletions.
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
'es6': true,
'node': true,
},
// 'extends': 'eslint:all',
'extends': 'eslint:recommended',
'parserOptions': {
'sourceType': 'module',
Expand Down Expand Up @@ -188,5 +189,21 @@ module.exports = {
'space-unary-ops': [
'error',
],
'spaced-comment': [
'error',
],
'quote-props': [
'error',
],
'require-jsdoc': [
'error',
],
'func-style': [
'warn',
'declaration',
],
'padded-blocks': [
'warn',
],
},
};
131 changes: 91 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @description The live2d-widget generator for hexo
*/
/*global hexo*/
/* global hexo */

const _ = require('lodash');
const colors = require('colors');
Expand All @@ -27,88 +27,131 @@ const onSiteJsPath = `${onSiteRootPath}lib/`;
const onSiteModelPath = `${onSiteRootPath}assets/`;

const defaultConfig = {
enable: true,
scriptFrom: 'local',
'enable': true,
'scriptFrom': 'local',
};

// Apply options with default
let config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);

/**
* Get entry point script URL according to type of source
* @param {String} scriptFrom The type of source
* @return {String} URL of entry point
*/
function getScriptURL (scriptFrom) {

switch (scriptFrom) {

case 'local': {
// Is local(1)
// Use local

/*
* Is local(1)
* Use local
*/
const scriptGenerators = buildGeneratorsFromManifest(manifest, path.dirname(mainfestPath), onSiteJsPath);
const useHash = getFileMD5(path.resolve(path.dirname(mainfestPath), coreScriptName));
generators.push(...scriptGenerators);
return `${url.resolve(onSiteJsPath, coreScriptName)}?${useHash}`;

}
case 'jsdelivr':
// Is jsdelivr online CDN(2)
// Use jsdelivr

/*
* Is jsdelivr online CDN(2)
* Use jsdelivr
*/
return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
case 'unpkg':
// Is unpkg online CDN(3)
// Use unpkg

/*
* Is unpkg online CDN(3)
* Use unpkg
*/
return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
default:
// Is custom(4)
// Use custom

/*
* Is custom(4)
* Use custom
*/
return scriptFrom;

}

}

if (config.enable) {

_.unset(config, 'enable');
if(_.hasIn(config, 'model.use')) {
if (_.hasIn(config, 'model.use')) {

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

/*
* Is in live2d_models(2)
* LoadModelFrom
*/
const {
modelGenerators,
modelJsonUrl: pkgModelJsonUrl,
'modelJsonUrl': pkgModelJsonUrl,
} = 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}'`);
}else{

} else {

tryPath = path.resolve(hexo.base_dir, config.model.use);
if(fs.existsSync(tryPath)) {
// Is in hexo base releated path(3)
// LoadModelFrom
if (fs.existsSync(tryPath)) {

/*
* Is in hexo base releated path(3)
* LoadModelFrom
*/
const {
modelGenerators,
modelJsonUrl: pkgModelJsonUrl,
'modelJsonUrl': pkgModelJsonUrl,
} = 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}'`);
}else if(getNodeModulePath(config.model.use) !== '') {
// Is npm-module(1)
// Convert path to assets folder
// LoadModelFrom

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

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

} else {

/*
* Is npm-module(1)
* Convert path to assets folder
* LoadModelFrom
*/
const packageJsonPath = path.resolve(getNodeModulePath(config.model.use), 'package.json');
const packageJsonObj = require(packageJsonPath); // eslint-disable-line global-require
const assetsDir = path.resolve(getNodeModulePath(config.model.use), './assets/');
const {
modelGenerators,
modelJsonUrl: pkgModelJsonUrl,
'modelJsonUrl': pkgModelJsonUrl,
} = 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}'`);
}else{
// Is custom(4)
// Use custom
modelJsonUrl = config.model.use;
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded Model from custom(4), at '${modelJsonUrl}'`);

}

}
_.unset(config, 'model.use');
config = _.set(config, 'model.jsonPath', modelJsonUrl);

}

/**
Expand All @@ -117,26 +160,34 @@ if (config.enable) {
* Don't manually add live2d tag into your site template
*/

hexo.extend.helper.register('live2d', function deprecatedWarning () {
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.`);

});

const scriptUrlToInject = getScriptURL(config.scriptFrom);
_.unset(config, 'scriptFrom');

// Injector borrowed form here:
// https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
hexo.extend.filter.register('after_render:html', function HTMLInjector (htmlContent) {
/*
* Injector borrowed form here:
* https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
*/
hexo.extend.filter.register('after_render:html', (htmlContent) => {

const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
const contentToInject = `<script src="${scriptUrlToInject}"></script><script>${scriptToInject}</script>`;
let newHtmlContent = htmlContent;
if (/<\/body>/gi.test(htmlContent)) {

const lastIndex = htmlContent.lastIndexOf('</body>');
htmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`;
newHtmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`;

}
return htmlContent;
});
return newHtmlContent;

hexo.extend.generator.register('live2d', function fileGenerator () {
return generators;
});

hexo.extend.generator.register('live2d', () => generators);

}
6 changes: 4 additions & 2 deletions lib/buildGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ const fs = require('hexo-fs');
*/

module.exports = function buildGenerator (sourcePath, distPath) {

return {
path: distPath,
data: () => fs.createReadStream(sourcePath),
'data': () => fs.createReadStream(sourcePath),
'path': distPath,
};

};
5 changes: 3 additions & 2 deletions lib/buildGeneratorsFromManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const url = require('url');
*/

module.exports = function buildGeneratorsFromManifest (manifest, rootPath, distPath) {

const files = Object.keys(manifest).map((key) => path.resolve(rootPath, manifest[key]));
return files.map((file) =>
buildGenerator(file, url.resolve(distPath, path.basename(file))));
return files.map((file) => buildGenerator(file, url.resolve(distPath, path.basename(file))));

};
2 changes: 2 additions & 0 deletions lib/getFileMD5.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const fs = require('hexo-fs');
*/

module.exports = function getFileMD5 (filePath) {

const rs = fs.readFileSync(filePath);
const hash = crypto.createHash('md5');
return hash.update(rs).digest('hex');

};
10 changes: 8 additions & 2 deletions lib/getNodeModulePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ const path = require('path');
*/

module.exports = function getNodeModulePath (packageName) {
try{

try {

return path.dirname(require.resolve(`${packageName}/package.json`));
}catch(e) {

} catch (e) {

return '';

}

};
10 changes: 9 additions & 1 deletion lib/listFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ const path = require('path');
*/

module.exports = function listFiles (dirPath) {

const lsDir = fs.readdirSync(dirPath);
const filesArr = [];
for (const fileName of lsDir) {

const pathName = path.join(dirPath, fileName);
if (fs.statSync(pathName).isDirectory()) {

filesArr.push(...listFiles(pathName));
}else{

} else {

filesArr.push(pathName);

}

}
return filesArr;

};
9 changes: 7 additions & 2 deletions lib/loadModelFrom.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ const url = require('url');
*/

module.exports = function loadModelFrom (assetsDir, rootUrl) {

const modelFiles = listFiles(assetsDir);
const modelGenerators = modelFiles.map((file) =>
buildGenerator(file, url.resolve(rootUrl, path.relative(assetsDir, file))));
const modelGenerators = modelFiles.map((file) => buildGenerator(file, url.resolve(rootUrl, path.relative(assetsDir, file))));
const modelJsonUrl = modelGenerators.reduce((p, generator) => {

if (!p && generator.path.endsWith('.model.json')) {

return generator.path;

}
return p;

}, undefined);

return {
modelGenerators,
modelJsonUrl,
};

};

0 comments on commit c348707

Please sign in to comment.