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

Commit

Permalink
style: reduce exposed varible, function!
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Feb 9, 2018
1 parent e123d6a commit 120386b
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 107 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ live2d:
## https://github.com/EYHN/hexo-helper-live2d
live2d:
enable: true
jsPath: local # 'local'||'jsdelivr'||'unpkg'||{Your own path, String}
jsPath: local # 'local'(1)||'jsdelivr'(2)||'unpkg'(3)||{Your own path, String}(4)
model:
use: live2d-widget-model-miku # {npm-module name}||{folder name in live2d_models/}||{Your own path, String}
use: live2d-widget-model-miku # {npm-module name}(1)||{folder name in live2d_models/}(2)||{Your own path, String}(3)
```
> To see Chinese explainations, please have a look at [Chinese document](./README.zh-CN.md).
Expand Down
204 changes: 99 additions & 105 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,25 @@ const fs = require('hexo-fs'),
path = require('path'),
url = require('url'),
_ = require('lodash'),
localJsPath = '/live2dw/lib/',
localModelPath = '/live2dw/assets/',
onSiteRootPath = '/live2dw/'
onSiteJsPath = onSiteRootPath + 'lib/',
onSiteModelPath = onSiteRootPath + 'assets/',
pkgInfo = require('./package'),
coreJsName = 'L2Dwidget.min.js',
coreJsList = require('live2d-widget/lib/manifest'),
coreJsDepPath = require('live2d-widget/lib'),
coreJsPath = path.dirname(require.resolve('live2d-widget/lib/manifest')),
coreJsDepVer = pkgInfo.dependencies['live2d-widget'],
defaultConfig = require('live2d-widget/src/config/defaultConfig');

let fileArr = new Array(),
modelPath,
jsPath,
coreJsNameLength,
modelJsonPath,
config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);

function getCoreJs(where){
let fileName = path.parse(where).name;
if((fileName.length < coreJsNameLength) || (coreJsNameLength === undefined)){
jsPath = where;
coreJsNameLength = fileName.length;
}
}

function getModelJson(where){
let fileName = path.parse(where).name.split('.');
if(fileName[1] === 'model'){
modelJsonPath = where;
// Check if enabled
if(_.hasIn(config, 'enable')){
if(!config.enable){
return;
}
_.unset(config, 'enable');
}

function addFile(destPath, sourceFile){
Expand All @@ -43,104 +36,105 @@ function addFile(destPath, sourceFile){
data: () => fs.createReadStream(sourceFile),
});
}

function addDir(destPath, sourceDir, func) {
let lsDir = fs.readdirSync(sourceDir)
lsDir.forEach(function (file) {
if (fs.statSync(path.resolve(sourceDir, file)).isDirectory()) {
addDir(path.resolve(destPath, file), path.resolve(sourceDir, file), func)
} else {
addFile(path.resolve(destPath, file), path.resolve(sourceDir, file));
if(func !== undefined) func(path.resolve(destPath, file));
}
}, this);
/**
function addFile(destPath, sourceFile){
console.log({
"dest": destPath,
"sour": sourceFile,
});
}

// Check if enabled
if(_.hasIn(config, 'enable')){
if(!config.enable){
return;
**/

function process_modelFileIsOnLocal(where, from = onSiteModelPath) {
if(fs.statSync(where).isDirectory()){
let lsDir = fs.readdirSync(where),
modelJsonName;
for(let item of lsDir){
modelJsonName = modelJsonName || process_modelFileIsOnLocal(path.resolve(where, item), url.resolve(from, item + (fs.statSync(where).isDirectory() ? '/' : '')));
}
return modelJsonName;
}else{
addFile(url.resolve(from, path.basename(where)), where);
let fileName = path.basename(where);
if(fileName.split('.')[1] === 'model'){
return fileName;
}
}
_.unset(config, 'enable');
}

// Preprocess jsPath for injecting and file copying
// Copy file and apply real_jsPath only if jsPath === jsOnLocalPath
if(_.hasIn(config, 'jsPath')){
switch(config.jsPath){
case 'local':
jsPath = localJsPath;
break;
case 'jsdelivr':
jsPath = 'https://cdn.jsdelivr.net/npm/[email protected]/lib/clL2D.min.js';
break;
case 'unpkg':
jsPath = 'https://unpkg.com/[email protected]/lib/clL2D.min.js';
break;
default:
jsPath = config.jsPath;
function process_jsPathIsLocal(){
for(let f of Object.keys(coreJsList)){
addFile(url.resolve(onSiteJsPath, coreJsList[f]), path.resolve(coreJsPath, coreJsList[f]));
}
_.unset(config, 'jsPath');
}else{
jsPath = jsOnLocalPath;
return url.resolve(onSiteJsPath, coreJsName);
}

// Preprocess modelPath for injecting and file copying
// 1. Unset model.jsonPath
if(_.hasIn(config, 'model.jsonPath')){
_.unset(config, 'model.jsonPath');
}
// 2. Process config.model.use
// Set modelPath and config.model.use in some case
// Copy file and apply config.model.jsonPath only if !_.hasIn(config, 'model.jsonPath')
if(_.hasIn(config, 'model.use')){
try{
// 2.a is a npm-module
modelPath = path.resolve(path.dirname(require.resolve(config.model.use + '/package')), './assets/');
}catch(e){
// 2.b is in live2d_models/ folder
let tryPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.use));
fs.exists(tryPath, function(exists){
if(exists){
// 2.b founded in live2d_models/
// 3.b Apply config.model.jsonPath
modelPath = tryPath;
}else{
// 2.c maybe an url or something, let it go~
// 3.c Apply config.model.jsonPath
config.model.jsonPath = config.model.use;
}
})
function getJsPath(){
if(_.hasIn(config, 'jsPath')){
// a. have user modified config.jsPath
switch(config.jsPath){
case 'local':
// a.1 is local
// use local(1)
return process_jsPathIsLocal();
case 'jsdelivr':
// a.2 is jsdelivr online CDN
// use jsdelivr(2)
return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreJsName}`;
case 'unpkg':
// a.3 is unpkg online CDN
// use unpkg(3)
return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreJsName}`;
default:
// a.4 is custom url or path, etc.
// use custom(4), let it go~
return config.jsPath;
}
_.unset(config, 'jsPath');
}else{
// b. don't have user modified config.jsPath
// use local(1)
return process_jsPathIsLocal();
}
_.unset(config, 'model.use');
}else{
// 2.d doesn't have config.model.use use default
// 3.d Apply config.model.jsonPath
config.model.jsonPath = defaultConfig.model.jsonPath;
}

// Process config.model.jsonPath
// and copy files
if(!_.hasIn(config, 'model.jsonPath')){
addDir(localModelPath, modelPath, getModelJson);
config.model.jsonPath = modelJsonPath;
console.log(config.model.jsonPath)
}

function getModelJsonPath(){
// Unset model.jsonPath
if(_.hasIn(config, 'model.jsonPath')){
_.unset(config, 'model.jsonPath');
}

// Process jsPath with jsPath(processed)
// and copy files
if(jsPath === localJsPath){
// a. is local
// copy coreJs
for(let f of Object.keys(coreJsList)){
addFile(localJsPath + f, path.resolve(coreJsDepPath, coreJsList[f]));
getCoreJs(localJsPath + f);
// Process config.model.use
if(_.hasIn(config, 'model.use')){
// a. have user modified config.model.use
try{
// a.1 is a npm-module(1)
let tryModulePath = path.dirname(require.resolve(config.model.use + '/package'));
let modelPath = path.resolve(tryModulePath, './assets/');
return process_modelFileIsOnLocal(modelPath);
}catch(e){
let tryFolderPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.use));
fs.exists(tryFolderPath, function(exists){
if(exists){
// a.2 founded in live2d_models/
let modelPath = path.resolve(tryFolderPath, './assets/');
return process_modelFileIsOnLocal(modelPath);
}else{
// a.3 is custom url or path, etc.
// use custom(3), let it go~
return config.model.use;
}
})
}
_.unset(config, 'model.use');
}else{
// b. don't have user modified config.model.use
// use default
return defaultConfig.model.jsonPath;
}
}else{
// b. is a url or something, let it go ~
}

config.model.jsonPath = getModelJsonPath();

/**
* Deprecated version support
Expand All @@ -156,7 +150,7 @@ hexo.extend.helper.register('live2d', function(){
// https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
hexo.extend.filter.register('after_render:html', function(htmlContent){
let launcherScript = `L2Dwidget.init(${JSON.stringify(config)});`;
let injectExtraScript = `<script src="${jsPath}"></script><script>${launcherScript}</script>`
let injectExtraScript = `<script src="${getJsPath()}"></script><script>${launcherScript}</script>`
if(/<\/body>/gi.test(htmlContent)){
let lastIndex = htmlContent.lastIndexOf('</body>');
htmlContent = htmlContent.substring(0, lastIndex) + injectExtraScript + htmlContent.substring(lastIndex, htmlContent.length);
Expand All @@ -166,4 +160,4 @@ hexo.extend.filter.register('after_render:html', function(htmlContent){

hexo.extend.generator.register('live2d', function (locals) {
return fileArr;
});
});

0 comments on commit 120386b

Please sign in to comment.