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

Commit

Permalink
fix: fix model.use does'nt achieve 4
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Feb 14, 2018
1 parent 3072c93 commit 537403d
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ live2d:
enable: true
scriptFrom: local # 'local'(1)||'jsdelivr'(2)||'unpkg'(3)||{Your own path, String}(4)
model:
use: live2d-widget-model-miku # {npm-module name}(1)||{folder name in live2d_models/}(2)||{Your own path, String}(3)
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)
```
> To see Chinese explainations, please have a look at [Chinese document](./README.zh-CN.md).
Expand Down
39 changes: 24 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

'use strict'

const fs = require('hexo-fs');
const _ = require('lodash');
const colors = require('colors');
const fs = require('hexo-fs');
const path = require('path');
const url = require('url');
const colors = require('colors');

const buildGeneratorsFromManifest = require('./lib/buildGeneratorsFromManifest');
const getFileMD5 = require('./lib/getFileMD5');
Expand All @@ -18,12 +18,12 @@ const loadModelFrom = require('./lib/loadModelFrom');

const defaultConfig = _.merge({},
{
enable: false,
enable: true,
scriptFrom: 'local'
})

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

// Check if enabled
if (!config.enable) {
Expand All @@ -35,8 +35,8 @@ const generators = [];
const manifest = require('live2d-widget/lib/manifest');
const mainfestPath = require.resolve('live2d-widget/lib/manifest');
const coreScriptName = manifest['main.js'];
const pkgInfo = require('./package');
const coreJsDepVer = pkgInfo.dependencies['live2d-widget'];
const thisPkgInfo = require('./package');
const coreJsDepVer = thisPkgInfo.dependencies['live2d-widget'];

const onSiteRootPath = '/live2dw/';
const onSiteJsPath = `${onSiteRootPath}lib/`;
Expand All @@ -48,7 +48,7 @@ switch (config.scriptFrom) {
case 'local':
// a.1 is local
// use local(1)
const scriptGenerators = buildGeneratorsFromManifest(manifest, path.dirname(mainfestPath), onSiteJsPath)
const scriptGenerators = buildGeneratorsFromManifest(manifest, path.dirname(mainfestPath), onSiteJsPath);
const useHash = getFileMD5(path.resolve(path.dirname(mainfestPath), coreScriptName));
generators.push(...scriptGenerators);
scriptURL = `${url.resolve(onSiteJsPath, coreScriptName)}?${useHash}`;
Expand All @@ -64,28 +64,37 @@ switch (config.scriptFrom) {
scriptURL = `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
break;
default:
// a.4 is custom
// use custom(4)
scriptURL = config.scriptFrom;
break;
}

if (config.model.use) {
// try './live2d_models/%config.model.use%' or './%config.model.use%'
const modelInHexoBaseDir = [
path.resolve(hexo.base_dir, './live2d_models/', config.model.use),
path.resolve(hexo.base_dir, config.model.use),
]
.reduce((p, path) => {
if (!p && fs.existsSync(path))
return path;
return path;
else
return p;
}, undefined);
const modelPath = modelInHexoBaseDir || getNodeModulePath(config.model.use);
const { generators: modelGenerators, jsonUrl: modelJsonUrl, packageInfo } = loadModelFrom(modelPath, onSiteModelPath);
const modelPaths = modelInHexoBaseDir || getNodeModulePath(config.model.use) || config.model.use; // Search paths
const { modelGenerators, modelJsonUrl, packageJsonObj, type } = loadModelFrom(modelPaths, onSiteModelPath);
generators.push(...modelGenerators);
config = _.set(config, 'model.jsonPath', modelJsonUrl);
if (packageInfo) {
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Load model ${packageInfo.name || config.model.use}${`@${packageInfo.version}` || ''} at '${modelPath}'`);
switch(type){
case 1:
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from npm-module(1), ${packageJsonObj.name}@${packageJsonObj.version} from '${modelPaths}'`);
break;
case 2.5:
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from folder(2/3), '${url.pathname(modelJsonUrl)}' from '${modelPaths}'`);
break;
case 4:
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded Model from online(4), at '${modelJsonUrl}'`);
break;
}
}

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

hexo.extend.helper.register('live2d', function () {
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.`);
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.`);
});

// injector borrowed form here:
Expand Down
2 changes: 1 addition & 1 deletion lib/buildGenerator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @description convert sourcePath and distPath to generator-use-object
* @description Convert sourcePath and distPath to generator-use-object
*/


Expand Down
6 changes: 3 additions & 3 deletions lib/buildGeneratorsFromManifest.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* @description convert manifest to generator-use-array
* @description Convert manifest to generator-use-array
*/


'use strict'

const buildGenerator = require('./buildGenerator');
const path = require('path');
const url = require('url');
const buildGenerator = require('./buildGenerator');

/**
* Generate generator-use-array according to provieded manifest
Expand All @@ -20,6 +20,6 @@ const buildGenerator = require('./buildGenerator');
module.exports = function buildGeneratorsFromManifest(manifest, rootPath, distPath) {
const files = Object.keys(manifest).map(key => path.resolve(rootPath, manifest[key]));
return files.map((file) => {
return buildGenerator(file, url.resolve(distPath, path.basename(file)))
return buildGenerator(file, url.resolve(distPath, path.basename(file)));
});
}
4 changes: 2 additions & 2 deletions lib/getFileMD5.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @description return the MD5 hash of provided file
* @description Get the MD5 hash of provided file
*/


'use strict'

const fs = require('hexo-fs');
const crypto = require('crypto');
const fs = require('hexo-fs');

/**
* Get file MD5 according to provieded file path
Expand Down
4 changes: 2 additions & 2 deletions lib/getNodeModulePath.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @description resolve the path of provided package name
* @description Resolve the path of provided package name
*/


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

module.exports = function getNodeModulePath(packageName) {
Expand Down
4 changes: 2 additions & 2 deletions lib/listFiles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @description list files of provided path
* @description List files of provided path
*/


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

/**
* list files in the provieded directory path
* List files in the provieded directory path
* @param {String} dirPath The path to the dir
* @return {Array} An array that contains all the file paths
*/
Expand Down
73 changes: 52 additions & 21 deletions lib/loadModelFrom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,62 @@

'use strict'

const path = require('path');
const url = require('url');
const fs = require('hexo-fs');
const buildGenerator = require('./buildGenerator');
const fs = require('hexo-fs');
const listFiles = require('./listFiles');
const path = require('path');
const url = require('url');

/**
* @typedef {Object} PackageInfo
* @property {Array} modelGenerators The generator-use-array for Hexo
* @property {String} modelJsonUrl The model.json file path
* @property {Object or Undefined} packageJsonObj The object of provied packageinfo(if don't have, undefined)
* @property {Number} type The type of this package, see README
*/

/**
* Resolve model packages according to provided infos
* @param {String} modelPaths Search paths for the model folder
* @param {String} rootUrl The target path(need to be relative)
* @return {PackageInfo} The package info resolved
*/

module.exports = function loadModelFrom(modelPath, modelrootUrl) {
const packageFile = path.resolve(modelPath, 'package.json');
const packageInfo = fs.existsSync(packageFile) && require(packageFile);
const assetsDir = path.resolve(modelPath, './assets/');
const modelFiles = listFiles(assetsDir);
const modelGenerators = modelFiles.map(file => {
return buildGenerator(file, url.resolve(modelrootUrl, path.relative(assetsDir, file)))
});
// find the model.json from model files
const modelJsonUrl = modelGenerators.reduce((p, generator) => {
if (!p && generator.path.endsWith('.model.json')) {
return generator.path;
} else {
return p;
module.exports = function loadModelFrom(modelPaths, rootUrl) {
const packageJsonPath = path.resolve(modelPaths, 'package.json');
let assetsDir, modelGenerators, modelJsonUrl, packageJsonObj, type;
if(fs.existsSync(packageJsonPath)){
type = 1;
packageJsonObj = require(packageJsonPath);
assetsDir = path.resolve(modelPaths, './assets/'); // convert 1 to 2/3
}else{
if(fs.existsSync(modelPaths)){
type = 2.5 // 2 or 3
assetsDir = modelPaths;
}else{
type = 4;
modelJsonUrl = modelPaths; // 4 Done.
}
}, undefined);
}
if(type !== 4){
const modelFiles = listFiles(assetsDir);
modelGenerators = modelFiles.map(file => {
return buildGenerator(file, url.resolve(rootUrl, path.relative(assetsDir, file)))
});
modelJsonUrl = modelGenerators.reduce((p, generator) => {
if (!p && generator.path.endsWith('.model.json')) {
return generator.path;
} else {
return p;
}
}, undefined); // 1, 2, 3 Done.
}

return {
generators: modelGenerators,
jsonUrl: modelJsonUrl,
packageInfo
modelGenerators,
modelJsonUrl,
packageJsonObj,
type,
}

}
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"hexo-fs": "^0.2.2",
"live2d-widget": "3.x",
"lodash": "^4.17.5",
"path": "^0.12.7",
"uglify-js": "^3.3.10"
"path": "^0.12.7"
},
"scripts": {
"test": "echo \"Sayly, there's nothing here. ToT\" && exit 1",
Expand Down

0 comments on commit 537403d

Please sign in to comment.