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

Commit

Permalink
style: add eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazeyu committed Feb 14, 2018
1 parent 537403d commit ba87f0c
Show file tree
Hide file tree
Showing 10 changed files with 1,020 additions and 82 deletions.
29 changes: 29 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
'env': {
'es6': true,
'node': true,
},
'extends': 'eslint:recommended',
'parserOptions': {
'sourceType': 'module',
},
'rules': {
'indent': [
'error',
2
],
'linebreak-style': [
'error',
'unix',
],
'quotes': [
'error',
'single',
],
'semi': [
'error',
'always',
],
'no-console': 'off',
},
};
130 changes: 64 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @description The live2d-widget generator for hexo
*/
/*global hexo*/


'use strict'
'use strict';

const _ = require('lodash');
const colors = require('colors');
Expand All @@ -16,20 +16,6 @@ const getFileMD5 = require('./lib/getFileMD5');
const getNodeModulePath = require('./lib/getNodeModulePath');
const loadModelFrom = require('./lib/loadModelFrom');

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

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

// Check if enabled
if (!config.enable) {
return;
}

const generators = [];

const manifest = require('live2d-widget/lib/manifest');
Expand All @@ -42,50 +28,59 @@ const onSiteRootPath = '/live2dw/';
const onSiteJsPath = `${onSiteRootPath}lib/`;
const onSiteModelPath = `${onSiteRootPath}assets/`;

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

switch (config.scriptFrom) {
case 'local':
// apply options with default
let config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);


function getScriptURL(scriptFrom) {
switch (scriptFrom) {
case 'local':{
// a.1 is local
// use local(1)
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}`;
break;
return `${url.resolve(onSiteJsPath, coreScriptName)}?${useHash}`;
}
case 'jsdelivr':
// a.2 is jsdelivr online CDN
// use jsdelivr(2)
scriptURL = `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
break;
return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
case 'unpkg':
// a.3 is unpkg online CDN
// use unpkg(3)
scriptURL = `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
break;
return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
default:
// a.4 is custom
// use custom(4)
scriptURL = config.scriptFrom;
break;
return scriptFrom;
}
}

if (config.model.use) {
const modelInHexoBaseDir = [
if (config.enable) {
_.unset(config, 'enable');
if (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;
else
return p;
}, undefined);
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);
switch(type){
.reduce((p, path) => {
if (!p && fs.existsSync(path))
return path;
else
return p;
}, undefined);
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);
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;
Expand All @@ -95,31 +90,34 @@ if (config.model.use) {
case 4:
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded Model from online(4), at '${modelJsonUrl}'`);
break;
}
}
}

/**
* Deprecated version support
* since 3.0
* Don't manually add live2d tag into your site template
*/

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.`);
});

// injector borrowed form here:
// https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
hexo.extend.filter.register('after_render:html', function (htmlContent) {
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
const contentToInject = `<script src="${scriptURL}"></script><script>${scriptToInject}</script>`;
if (/<\/body>/gi.test(htmlContent)) {
let lastIndex = htmlContent.lastIndexOf('</body>');
htmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`;
}
return htmlContent;
});

hexo.extend.generator.register('live2d', function (locals) {
return generators;
});
/**
* Deprecated version support
* since 3.0
* Don't manually add live2d tag into your site template
*/

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.`);
});

// injector borrowed form here:
// https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
hexo.extend.filter.register('after_render:html', function (htmlContent) {
const scriptFrom = config.scriptFrom;
_.unset(config, 'scriptFrom');
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
const contentToInject = `<script src="${getScriptURL(scriptFrom)}"></script><script>${scriptToInject}</script>`;
if (/<\/body>/gi.test(htmlContent)) {
let lastIndex = htmlContent.lastIndexOf('</body>');
htmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`;
}
return htmlContent;
});

hexo.extend.generator.register('live2d', function () {
return generators;
});
}
4 changes: 2 additions & 2 deletions lib/buildGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/


'use strict'
'use strict';

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

Expand All @@ -19,4 +19,4 @@ module.exports = function buildGenerator(sourcePath, distPath) {
path: distPath,
data: () => fs.createReadStream(sourcePath),
};
}
};
4 changes: 2 additions & 2 deletions lib/buildGeneratorsFromManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/


'use strict'
'use strict';

const buildGenerator = require('./buildGenerator');
const path = require('path');
Expand All @@ -22,4 +22,4 @@ module.exports = function buildGeneratorsFromManifest(manifest, rootPath, distPa
return files.map((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
Expand Up @@ -3,7 +3,7 @@
*/


'use strict'
'use strict';

const crypto = require('crypto');
const fs = require('hexo-fs');
Expand All @@ -18,4 +18,4 @@ module.exports = function getFileMD5(filePath) {
const rs = fs.readFileSync(filePath);
const hash = crypto.createHash('md5');
return (hash.update(rs).digest('hex'));
}
};
4 changes: 2 additions & 2 deletions lib/getNodeModulePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/


'use strict'
'use strict';

const path = require('path');

Expand All @@ -19,4 +19,4 @@ module.exports = function getNodeModulePath(packageName) {
}catch(e){
return undefined;
}
}
};
4 changes: 2 additions & 2 deletions lib/listFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/


'use strict'
'use strict';

const fs = require('hexo-fs');
const path = require('path');
Expand All @@ -25,4 +25,4 @@ module.exports = function listFiles(dirPath) {
else filesArr.push(pathName);
}
return filesArr;
}
};
11 changes: 5 additions & 6 deletions lib/loadModelFrom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/


'use strict'
'use strict';

const buildGenerator = require('./buildGenerator');
const fs = require('hexo-fs');
Expand Down Expand Up @@ -35,7 +35,7 @@ module.exports = function loadModelFrom(modelPaths, rootUrl) {
assetsDir = path.resolve(modelPaths, './assets/'); // convert 1 to 2/3
}else{
if(fs.existsSync(modelPaths)){
type = 2.5 // 2 or 3
type = 2.5; // 2 or 3
assetsDir = modelPaths;
}else{
type = 4;
Expand All @@ -45,7 +45,7 @@ module.exports = function loadModelFrom(modelPaths, rootUrl) {
if(type !== 4){
const modelFiles = listFiles(assetsDir);
modelGenerators = modelFiles.map(file => {
return buildGenerator(file, url.resolve(rootUrl, path.relative(assetsDir, file)))
return buildGenerator(file, url.resolve(rootUrl, path.relative(assetsDir, file)));
});
modelJsonUrl = modelGenerators.reduce((p, generator) => {
if (!p && generator.path.endsWith('.model.json')) {
Expand All @@ -61,6 +61,5 @@ module.exports = function loadModelFrom(modelPaths, rootUrl) {
modelJsonUrl,
packageJsonObj,
type,
}

}
};
};
Loading

0 comments on commit ba87f0c

Please sign in to comment.