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

Commit f77edf8

Browse files
committed
feat: new AI for file copy
1 parent fc6ec6b commit f77edf8

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

index.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,31 @@ const fs = require('hexo-fs'),
99
path = require('path'),
1010
url = require('url'),
1111
_ = require('lodash'),
12-
localJsPath = '/live2d/lib/',
12+
localJsPath = '/live2dw/lib/',
13+
localModelPath = '/live2dw/assets/'
1314
coreJsList = require('live2d-widget/lib/manifest'),
15+
coreJsDepPath = require('live2d-widget/lib'),
1416
defaultConfig = require('live2d-widget/src/config/defaultConfig');
1517

1618
let fileArr = new Array(),
1719
modelPath,
1820
jsPath,
21+
coreJsNameLength,
1922
modelJsonPath,
2023
config = _.defaultsDeep(hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);
2124

22-
function getModelJson(pathName){
23-
var fileName = path.parse(pathName).name.split('.');
25+
function getCoreJs(path){
26+
let fileName = path.parse(path).name;
27+
if((fileName.length < coreJsNameLength) || (coreJsNameLength === undefined)){
28+
jsPath = fileName;
29+
coreJsNameLength = fileName.length;
30+
}
31+
}
32+
33+
function getModelJson(path){
34+
let fileName = path.parse(path).name.split('.');
2435
if(fileName[1] === 'model'){
25-
modelJsonPath = pathName;
36+
modelJsonPath = path;
2637
}
2738
}
2839

@@ -33,10 +44,11 @@ function addFile(destPath, sourceFile){
3344
});
3445
}
3546

36-
function addDir(destPath, sourceDir) {
47+
function addDir(destPath, sourceDir, func) {
3748
let lsDir = fs.readdirSync(sourceDir)
3849
lsDir.forEach(function (file) {
3950
addFile(destPath + file, path.resolve(sourceDir, file));
51+
if(func !== undefined) func(destPath);
4052
}, this);
4153
}
4254

@@ -78,24 +90,24 @@ if(_.hasIn(config, 'model.jsonPath')){
7890
// Set modelPath and config.model.use in some case
7991
// Copy file and apply config.model.jsonPath only if !_.hasIn(config, 'model.jsonPath')
8092
if(_.hasIn(config, 'model.use')){
81-
// 2.a is a npm-module
8293
try(){
83-
modelPath = require.resolve(config.model.use);
94+
// 2.a is a npm-module
95+
modelPath = path.resolve(path.dirname(require.resolve(config.model.use + '/package')), './assets/');
8496
}catch(e){
8597
// 2.b is in live2d_models/ folder
8698
let tryPath = path.resolve(hexo.base_dir, path.join('./live2d_models/', config.model.use));
8799
fs.exists(tryPath, function(exists){
88100
if(exists){
89-
// 2.b continue
101+
// 2.b founded in live2d_models/
102+
// 3.b Apply config.model.jsonPath
103+
modelPath = tryPath;
90104
}else{
91-
// 2.c maybe an url or something, just apply it.
105+
// 2.c maybe an url or something, let it go~
92106
// 3.c Apply config.model.jsonPath
93107
config.model.jsonPath = config.model.use;
94108
}
95109
})
96110
}
97-
98-
99111
_.unset(config, 'model.use');
100112
}else{
101113
// 2.d doesn't have config.model.use use default
@@ -106,20 +118,22 @@ if(_.hasIn(config, 'model.use')){
106118
// Process config.model.jsonPath
107119
// and copy files
108120
if(!_.hasIn(config, 'model.jsonPath')){
109-
121+
addDir(localModelPath, modelPath, getModelJson);
122+
config.model.jsonPath = localModelPath + modelJsonPath;
110123
}
111124

112125

113-
// Process jsPath with real_jsPath(not a varible)
126+
// Process jsPath with jsPath(processed)
114127
// and copy files
115128
if(jsPath === jsOnLocalPath){
116129
// a. is local
117130
// copy coreJs
118-
119-
// apply jsPath
120-
131+
for(let f of Object.keys(coreJsList)){
132+
addFile(localJsPath + f, path.resolve(coreJsDepPath, f));
133+
getCoreJs(localJsPath + f);
134+
}
121135
}else{
122-
// b. is a CDN or url, let it go ~
136+
// b. is a url or something, let it go ~
123137
}
124138

125139

0 commit comments

Comments
 (0)