diff --git a/.eslintrc.js b/.eslintrc.js index d199283..223795c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,6 +3,7 @@ module.exports = { 'es6': true, 'node': true, }, + // 'extends': 'eslint:all', 'extends': 'eslint:recommended', 'parserOptions': { 'sourceType': 'module', @@ -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', + ], }, }; diff --git a/index.js b/index.js index 4b8c48f..8747e9a 100755 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ /** * @description The live2d-widget generator for hexo */ -/*global hexo*/ +/* global hexo */ const _ = require('lodash'); const colors = require('colors'); @@ -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); + } /** @@ -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 = ``; + let newHtmlContent = htmlContent; if (/<\/body>/gi.test(htmlContent)) { + const lastIndex = htmlContent.lastIndexOf(''); - 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); + } diff --git a/lib/buildGenerator.js b/lib/buildGenerator.js index 3ae593d..e2debf5 100644 --- a/lib/buildGenerator.js +++ b/lib/buildGenerator.js @@ -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, }; + }; diff --git a/lib/buildGeneratorsFromManifest.js b/lib/buildGeneratorsFromManifest.js index 7c7e8a8..1b1f612 100644 --- a/lib/buildGeneratorsFromManifest.js +++ b/lib/buildGeneratorsFromManifest.js @@ -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)))); + }; diff --git a/lib/getFileMD5.js b/lib/getFileMD5.js index 9540a49..cf8f1ea 100644 --- a/lib/getFileMD5.js +++ b/lib/getFileMD5.js @@ -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'); + }; diff --git a/lib/getNodeModulePath.js b/lib/getNodeModulePath.js index 38e1538..22b81b2 100644 --- a/lib/getNodeModulePath.js +++ b/lib/getNodeModulePath.js @@ -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 ''; + } + }; diff --git a/lib/listFiles.js b/lib/listFiles.js index 4df7be0..6500364 100644 --- a/lib/listFiles.js +++ b/lib/listFiles.js @@ -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; + }; diff --git a/lib/loadModelFrom.js b/lib/loadModelFrom.js index 6e7b90c..31a54f6 100644 --- a/lib/loadModelFrom.js +++ b/lib/loadModelFrom.js @@ -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, }; + };