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

Commit 537403d

Browse files
committed
fix: fix model.use does'nt achieve 4
1 parent 3072c93 commit 537403d

10 files changed

+88
-68
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ live2d:
6969
enable: true
7070
scriptFrom: local # 'local'(1)||'jsdelivr'(2)||'unpkg'(3)||{Your own path, String}(4)
7171
model:
72-
use: live2d-widget-model-miku # {npm-module name}(1)||{folder name in live2d_models/}(2)||{Your own path, String}(3)
72+
use: live2d-widget-model-miku # {npm-module name}(1)||{folder name in hexo.base_dir/live2d_models/}(2/2.5)||{folder name related to hexo.base_dir}(3/2.5)||{your own path of model.json, String}(4)
7373
```
7474
7575
> To see Chinese explainations, please have a look at [Chinese document](./README.zh-CN.md).

index.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
'use strict'
77

8-
const fs = require('hexo-fs');
98
const _ = require('lodash');
9+
const colors = require('colors');
10+
const fs = require('hexo-fs');
1011
const path = require('path');
1112
const url = require('url');
12-
const colors = require('colors');
1313

1414
const buildGeneratorsFromManifest = require('./lib/buildGeneratorsFromManifest');
1515
const getFileMD5 = require('./lib/getFileMD5');
@@ -18,12 +18,12 @@ const loadModelFrom = require('./lib/loadModelFrom');
1818

1919
const defaultConfig = _.merge({},
2020
{
21-
enable: false,
21+
enable: true,
2222
scriptFrom: 'local'
2323
})
2424

25-
// using default options
26-
let config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);
25+
// apply options with default
26+
const config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);
2727

2828
// Check if enabled
2929
if (!config.enable) {
@@ -35,8 +35,8 @@ const generators = [];
3535
const manifest = require('live2d-widget/lib/manifest');
3636
const mainfestPath = require.resolve('live2d-widget/lib/manifest');
3737
const coreScriptName = manifest['main.js'];
38-
const pkgInfo = require('./package');
39-
const coreJsDepVer = pkgInfo.dependencies['live2d-widget'];
38+
const thisPkgInfo = require('./package');
39+
const coreJsDepVer = thisPkgInfo.dependencies['live2d-widget'];
4040

4141
const onSiteRootPath = '/live2dw/';
4242
const onSiteJsPath = `${onSiteRootPath}lib/`;
@@ -48,7 +48,7 @@ switch (config.scriptFrom) {
4848
case 'local':
4949
// a.1 is local
5050
// use local(1)
51-
const scriptGenerators = buildGeneratorsFromManifest(manifest, path.dirname(mainfestPath), onSiteJsPath)
51+
const scriptGenerators = buildGeneratorsFromManifest(manifest, path.dirname(mainfestPath), onSiteJsPath);
5252
const useHash = getFileMD5(path.resolve(path.dirname(mainfestPath), coreScriptName));
5353
generators.push(...scriptGenerators);
5454
scriptURL = `${url.resolve(onSiteJsPath, coreScriptName)}?${useHash}`;
@@ -64,28 +64,37 @@ switch (config.scriptFrom) {
6464
scriptURL = `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
6565
break;
6666
default:
67+
// a.4 is custom
68+
// use custom(4)
6769
scriptURL = config.scriptFrom;
6870
break;
6971
}
7072

7173
if (config.model.use) {
72-
// try './live2d_models/%config.model.use%' or './%config.model.use%'
7374
const modelInHexoBaseDir = [
7475
path.resolve(hexo.base_dir, './live2d_models/', config.model.use),
7576
path.resolve(hexo.base_dir, config.model.use),
7677
]
7778
.reduce((p, path) => {
7879
if (!p && fs.existsSync(path))
79-
return path;
80+
return path;
8081
else
8182
return p;
8283
}, undefined);
83-
const modelPath = modelInHexoBaseDir || getNodeModulePath(config.model.use);
84-
const { generators: modelGenerators, jsonUrl: modelJsonUrl, packageInfo } = loadModelFrom(modelPath, onSiteModelPath);
84+
const modelPaths = modelInHexoBaseDir || getNodeModulePath(config.model.use) || config.model.use; // Search paths
85+
const { modelGenerators, modelJsonUrl, packageJsonObj, type } = loadModelFrom(modelPaths, onSiteModelPath);
8586
generators.push(...modelGenerators);
8687
config = _.set(config, 'model.jsonPath', modelJsonUrl);
87-
if (packageInfo) {
88-
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Load model ${packageInfo.name || config.model.use}${`@${packageInfo.version}` || ''} at '${modelPath}'`);
88+
switch(type){
89+
case 1:
90+
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from npm-module(1), ${packageJsonObj.name}@${packageJsonObj.version} from '${modelPaths}'`);
91+
break;
92+
case 2.5:
93+
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from folder(2/3), '${url.pathname(modelJsonUrl)}' from '${modelPaths}'`);
94+
break;
95+
case 4:
96+
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded Model from online(4), at '${modelJsonUrl}'`);
97+
break;
8998
}
9099
}
91100

@@ -96,7 +105,7 @@ if (config.model.use) {
96105
*/
97106

98107
hexo.extend.helper.register('live2d', function () {
99-
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.`);
108+
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.`);
100109
});
101110

102111
// injector borrowed form here:

lib/buildGenerator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @description convert sourcePath and distPath to generator-use-object
2+
* @description Convert sourcePath and distPath to generator-use-object
33
*/
44

55

lib/buildGeneratorsFromManifest.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* @description convert manifest to generator-use-array
2+
* @description Convert manifest to generator-use-array
33
*/
44

55

66
'use strict'
77

8+
const buildGenerator = require('./buildGenerator');
89
const path = require('path');
910
const url = require('url');
10-
const buildGenerator = require('./buildGenerator');
1111

1212
/**
1313
* Generate generator-use-array according to provieded manifest
@@ -20,6 +20,6 @@ const buildGenerator = require('./buildGenerator');
2020
module.exports = function buildGeneratorsFromManifest(manifest, rootPath, distPath) {
2121
const files = Object.keys(manifest).map(key => path.resolve(rootPath, manifest[key]));
2222
return files.map((file) => {
23-
return buildGenerator(file, url.resolve(distPath, path.basename(file)))
23+
return buildGenerator(file, url.resolve(distPath, path.basename(file)));
2424
});
2525
}

lib/getFileMD5.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* @description return the MD5 hash of provided file
2+
* @description Get the MD5 hash of provided file
33
*/
44

55

66
'use strict'
77

8-
const fs = require('hexo-fs');
98
const crypto = require('crypto');
9+
const fs = require('hexo-fs');
1010

1111
/**
1212
* Get file MD5 according to provieded file path

lib/getNodeModulePath.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @description resolve the path of provided package name
2+
* @description Resolve the path of provided package name
33
*/
44

55

@@ -10,7 +10,7 @@ const path = require('path');
1010
/**
1111
* Resolve the package path according to provieded package name
1212
* @param {String} packageName Package need to be resolved
13-
* @return {String or Undefined} if detected the package, the path; if not, undefined
13+
* @return {String or Undefined} If detected the package, the path; if not, undefined
1414
*/
1515

1616
module.exports = function getNodeModulePath(packageName) {

lib/listFiles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @description list files of provided path
2+
* @description List files of provided path
33
*/
44

55

@@ -9,7 +9,7 @@ const fs = require('hexo-fs');
99
const path = require('path');
1010

1111
/**
12-
* list files in the provieded directory path
12+
* List files in the provieded directory path
1313
* @param {String} dirPath The path to the dir
1414
* @return {Array} An array that contains all the file paths
1515
*/

lib/loadModelFrom.js

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,62 @@
55

66
'use strict'
77

8-
const path = require('path');
9-
const url = require('url');
10-
const fs = require('hexo-fs');
118
const buildGenerator = require('./buildGenerator');
9+
const fs = require('hexo-fs');
1210
const listFiles = require('./listFiles');
11+
const path = require('path');
12+
const url = require('url');
13+
14+
/**
15+
* @typedef {Object} PackageInfo
16+
* @property {Array} modelGenerators The generator-use-array for Hexo
17+
* @property {String} modelJsonUrl The model.json file path
18+
* @property {Object or Undefined} packageJsonObj The object of provied packageinfo(if don't have, undefined)
19+
* @property {Number} type The type of this package, see README
20+
*/
21+
22+
/**
23+
* Resolve model packages according to provided infos
24+
* @param {String} modelPaths Search paths for the model folder
25+
* @param {String} rootUrl The target path(need to be relative)
26+
* @return {PackageInfo} The package info resolved
27+
*/
1328

14-
module.exports = function loadModelFrom(modelPath, modelrootUrl) {
15-
const packageFile = path.resolve(modelPath, 'package.json');
16-
const packageInfo = fs.existsSync(packageFile) && require(packageFile);
17-
const assetsDir = path.resolve(modelPath, './assets/');
18-
const modelFiles = listFiles(assetsDir);
19-
const modelGenerators = modelFiles.map(file => {
20-
return buildGenerator(file, url.resolve(modelrootUrl, path.relative(assetsDir, file)))
21-
});
22-
// find the model.json from model files
23-
const modelJsonUrl = modelGenerators.reduce((p, generator) => {
24-
if (!p && generator.path.endsWith('.model.json')) {
25-
return generator.path;
26-
} else {
27-
return p;
29+
module.exports = function loadModelFrom(modelPaths, rootUrl) {
30+
const packageJsonPath = path.resolve(modelPaths, 'package.json');
31+
let assetsDir, modelGenerators, modelJsonUrl, packageJsonObj, type;
32+
if(fs.existsSync(packageJsonPath)){
33+
type = 1;
34+
packageJsonObj = require(packageJsonPath);
35+
assetsDir = path.resolve(modelPaths, './assets/'); // convert 1 to 2/3
36+
}else{
37+
if(fs.existsSync(modelPaths)){
38+
type = 2.5 // 2 or 3
39+
assetsDir = modelPaths;
40+
}else{
41+
type = 4;
42+
modelJsonUrl = modelPaths; // 4 Done.
2843
}
29-
}, undefined);
44+
}
45+
if(type !== 4){
46+
const modelFiles = listFiles(assetsDir);
47+
modelGenerators = modelFiles.map(file => {
48+
return buildGenerator(file, url.resolve(rootUrl, path.relative(assetsDir, file)))
49+
});
50+
modelJsonUrl = modelGenerators.reduce((p, generator) => {
51+
if (!p && generator.path.endsWith('.model.json')) {
52+
return generator.path;
53+
} else {
54+
return p;
55+
}
56+
}, undefined); // 1, 2, 3 Done.
57+
}
58+
3059
return {
31-
generators: modelGenerators,
32-
jsonUrl: modelJsonUrl,
33-
packageInfo
60+
modelGenerators,
61+
modelJsonUrl,
62+
packageJsonObj,
63+
type,
3464
}
65+
3566
}

package-lock.json

Lines changed: 0 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"hexo-fs": "^0.2.2",
1414
"live2d-widget": "3.x",
1515
"lodash": "^4.17.5",
16-
"path": "^0.12.7",
17-
"uglify-js": "^3.3.10"
16+
"path": "^0.12.7"
1817
},
1918
"scripts": {
2019
"test": "echo \"Sayly, there's nothing here. ToT\" && exit 1",

0 commit comments

Comments
 (0)