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

Commit 120386b

Browse files
committed
style: reduce exposed varible, function!
1 parent e123d6a commit 120386b

File tree

2 files changed

+101
-107
lines changed

2 files changed

+101
-107
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ live2d:
6767
## https://github.com/EYHN/hexo-helper-live2d
6868
live2d:
6969
enable: true
70-
jsPath: local # 'local'||'jsdelivr'||'unpkg'||{Your own path, String}
70+
jsPath: local # 'local'(1)||'jsdelivr'(2)||'unpkg'(3)||{Your own path, String}(4)
7171
model:
72-
use: live2d-widget-model-miku # {npm-module name}||{folder name in live2d_models/}||{Your own path, String}
72+
use: live2d-widget-model-miku # {npm-module name}(1)||{folder name in live2d_models/}(2)||{Your own path, String}(3)
7373
```
7474
7575
> To see Chinese explainations, please have a look at [Chinese document](./README.zh-CN.md).

index.js

Lines changed: 99 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,25 @@ const fs = require('hexo-fs'),
99
path = require('path'),
1010
url = require('url'),
1111
_ = require('lodash'),
12-
localJsPath = '/live2dw/lib/',
13-
localModelPath = '/live2dw/assets/',
12+
onSiteRootPath = '/live2dw/'
13+
onSiteJsPath = onSiteRootPath + 'lib/',
14+
onSiteModelPath = onSiteRootPath + 'assets/',
15+
pkgInfo = require('./package'),
16+
coreJsName = 'L2Dwidget.min.js',
1417
coreJsList = require('live2d-widget/lib/manifest'),
15-
coreJsDepPath = require('live2d-widget/lib'),
18+
coreJsPath = path.dirname(require.resolve('live2d-widget/lib/manifest')),
19+
coreJsDepVer = pkgInfo.dependencies['live2d-widget'],
1620
defaultConfig = require('live2d-widget/src/config/defaultConfig');
1721

1822
let fileArr = new Array(),
19-
modelPath,
20-
jsPath,
21-
coreJsNameLength,
22-
modelJsonPath,
2323
config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);
2424

25-
function getCoreJs(where){
26-
let fileName = path.parse(where).name;
27-
if((fileName.length < coreJsNameLength) || (coreJsNameLength === undefined)){
28-
jsPath = where;
29-
coreJsNameLength = fileName.length;
30-
}
31-
}
32-
33-
function getModelJson(where){
34-
let fileName = path.parse(where).name.split('.');
35-
if(fileName[1] === 'model'){
36-
modelJsonPath = where;
25+
// Check if enabled
26+
if(_.hasIn(config, 'enable')){
27+
if(!config.enable){
28+
return;
3729
}
30+
_.unset(config, 'enable');
3831
}
3932

4033
function addFile(destPath, sourceFile){
@@ -43,104 +36,105 @@ function addFile(destPath, sourceFile){
4336
data: () => fs.createReadStream(sourceFile),
4437
});
4538
}
46-
47-
function addDir(destPath, sourceDir, func) {
48-
let lsDir = fs.readdirSync(sourceDir)
49-
lsDir.forEach(function (file) {
50-
if (fs.statSync(path.resolve(sourceDir, file)).isDirectory()) {
51-
addDir(path.resolve(destPath, file), path.resolve(sourceDir, file), func)
52-
} else {
53-
addFile(path.resolve(destPath, file), path.resolve(sourceDir, file));
54-
if(func !== undefined) func(path.resolve(destPath, file));
55-
}
56-
}, this);
39+
/**
40+
function addFile(destPath, sourceFile){
41+
console.log({
42+
"dest": destPath,
43+
"sour": sourceFile,
44+
});
5745
}
58-
59-
// Check if enabled
60-
if(_.hasIn(config, 'enable')){
61-
if(!config.enable){
62-
return;
46+
**/
47+
48+
function process_modelFileIsOnLocal(where, from = onSiteModelPath) {
49+
if(fs.statSync(where).isDirectory()){
50+
let lsDir = fs.readdirSync(where),
51+
modelJsonName;
52+
for(let item of lsDir){
53+
modelJsonName = modelJsonName || process_modelFileIsOnLocal(path.resolve(where, item), url.resolve(from, item + (fs.statSync(where).isDirectory() ? '/' : '')));
54+
}
55+
return modelJsonName;
56+
}else{
57+
addFile(url.resolve(from, path.basename(where)), where);
58+
let fileName = path.basename(where);
59+
if(fileName.split('.')[1] === 'model'){
60+
return fileName;
61+
}
6362
}
64-
_.unset(config, 'enable');
6563
}
6664

67-
// Preprocess jsPath for injecting and file copying
68-
// Copy file and apply real_jsPath only if jsPath === jsOnLocalPath
69-
if(_.hasIn(config, 'jsPath')){
70-
switch(config.jsPath){
71-
case 'local':
72-
jsPath = localJsPath;
73-
break;
74-
case 'jsdelivr':
75-
jsPath = 'https://cdn.jsdelivr.net/npm/live2d-widget@3.x/lib/clL2D.min.js';
76-
break;
77-
case 'unpkg':
78-
jsPath = 'https://unpkg.com/live2d-widget@3.x/lib/clL2D.min.js';
79-
break;
80-
default:
81-
jsPath = config.jsPath;
65+
function process_jsPathIsLocal(){
66+
for(let f of Object.keys(coreJsList)){
67+
addFile(url.resolve(onSiteJsPath, coreJsList[f]), path.resolve(coreJsPath, coreJsList[f]));
8268
}
83-
_.unset(config, 'jsPath');
84-
}else{
85-
jsPath = jsOnLocalPath;
69+
return url.resolve(onSiteJsPath, coreJsName);
8670
}
8771

88-
// Preprocess modelPath for injecting and file copying
89-
// 1. Unset model.jsonPath
90-
if(_.hasIn(config, 'model.jsonPath')){
91-
_.unset(config, 'model.jsonPath');
92-
}
93-
// 2. Process config.model.use
94-
// Set modelPath and config.model.use in some case
95-
// Copy file and apply config.model.jsonPath only if !_.hasIn(config, 'model.jsonPath')
96-
if(_.hasIn(config, 'model.use')){
97-
try{
98-
// 2.a is a npm-module
99-
modelPath = path.resolve(path.dirname(require.resolve(config.model.use + '/package')), './assets/');
100-
}catch(e){
101-
// 2.b is in live2d_models/ folder
102-
let tryPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.use));
103-
fs.exists(tryPath, function(exists){
104-
if(exists){
105-
// 2.b founded in live2d_models/
106-
// 3.b Apply config.model.jsonPath
107-
modelPath = tryPath;
108-
}else{
109-
// 2.c maybe an url or something, let it go~
110-
// 3.c Apply config.model.jsonPath
111-
config.model.jsonPath = config.model.use;
112-
}
113-
})
72+
function getJsPath(){
73+
if(_.hasIn(config, 'jsPath')){
74+
// a. have user modified config.jsPath
75+
switch(config.jsPath){
76+
case 'local':
77+
// a.1 is local
78+
// use local(1)
79+
return process_jsPathIsLocal();
80+
case 'jsdelivr':
81+
// a.2 is jsdelivr online CDN
82+
// use jsdelivr(2)
83+
return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreJsName}`;
84+
case 'unpkg':
85+
// a.3 is unpkg online CDN
86+
// use unpkg(3)
87+
return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreJsName}`;
88+
default:
89+
// a.4 is custom url or path, etc.
90+
// use custom(4), let it go~
91+
return config.jsPath;
92+
}
93+
_.unset(config, 'jsPath');
94+
}else{
95+
// b. don't have user modified config.jsPath
96+
// use local(1)
97+
return process_jsPathIsLocal();
11498
}
115-
_.unset(config, 'model.use');
116-
}else{
117-
// 2.d doesn't have config.model.use use default
118-
// 3.d Apply config.model.jsonPath
119-
config.model.jsonPath = defaultConfig.model.jsonPath;
120-
}
121-
122-
// Process config.model.jsonPath
123-
// and copy files
124-
if(!_.hasIn(config, 'model.jsonPath')){
125-
addDir(localModelPath, modelPath, getModelJson);
126-
config.model.jsonPath = modelJsonPath;
127-
console.log(config.model.jsonPath)
12899
}
129100

101+
function getModelJsonPath(){
102+
// Unset model.jsonPath
103+
if(_.hasIn(config, 'model.jsonPath')){
104+
_.unset(config, 'model.jsonPath');
105+
}
130106

131-
// Process jsPath with jsPath(processed)
132-
// and copy files
133-
if(jsPath === localJsPath){
134-
// a. is local
135-
// copy coreJs
136-
for(let f of Object.keys(coreJsList)){
137-
addFile(localJsPath + f, path.resolve(coreJsDepPath, coreJsList[f]));
138-
getCoreJs(localJsPath + f);
107+
// Process config.model.use
108+
if(_.hasIn(config, 'model.use')){
109+
// a. have user modified config.model.use
110+
try{
111+
// a.1 is a npm-module(1)
112+
let tryModulePath = path.dirname(require.resolve(config.model.use + '/package'));
113+
let modelPath = path.resolve(tryModulePath, './assets/');
114+
return process_modelFileIsOnLocal(modelPath);
115+
}catch(e){
116+
let tryFolderPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.use));
117+
fs.exists(tryFolderPath, function(exists){
118+
if(exists){
119+
// a.2 founded in live2d_models/
120+
let modelPath = path.resolve(tryFolderPath, './assets/');
121+
return process_modelFileIsOnLocal(modelPath);
122+
}else{
123+
// a.3 is custom url or path, etc.
124+
// use custom(3), let it go~
125+
return config.model.use;
126+
}
127+
})
128+
}
129+
_.unset(config, 'model.use');
130+
}else{
131+
// b. don't have user modified config.model.use
132+
// use default
133+
return defaultConfig.model.jsonPath;
139134
}
140-
}else{
141-
// b. is a url or something, let it go ~
142135
}
143136

137+
config.model.jsonPath = getModelJsonPath();
144138

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

167161
hexo.extend.generator.register('live2d', function (locals) {
168162
return fileArr;
169-
});
163+
});

0 commit comments

Comments
 (0)