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

Commit ba87f0c

Browse files
committed
style: add eslint
1 parent 537403d commit ba87f0c

File tree

10 files changed

+1020
-82
lines changed

10 files changed

+1020
-82
lines changed

.eslintrc.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = {
2+
'env': {
3+
'es6': true,
4+
'node': true,
5+
},
6+
'extends': 'eslint:recommended',
7+
'parserOptions': {
8+
'sourceType': 'module',
9+
},
10+
'rules': {
11+
'indent': [
12+
'error',
13+
2
14+
],
15+
'linebreak-style': [
16+
'error',
17+
'unix',
18+
],
19+
'quotes': [
20+
'error',
21+
'single',
22+
],
23+
'semi': [
24+
'error',
25+
'always',
26+
],
27+
'no-console': 'off',
28+
},
29+
};

index.js

Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @description The live2d-widget generator for hexo
33
*/
4+
/*global hexo*/
45

5-
6-
'use strict'
6+
'use strict';
77

88
const _ = require('lodash');
99
const colors = require('colors');
@@ -16,20 +16,6 @@ const getFileMD5 = require('./lib/getFileMD5');
1616
const getNodeModulePath = require('./lib/getNodeModulePath');
1717
const loadModelFrom = require('./lib/loadModelFrom');
1818

19-
const defaultConfig = _.merge({},
20-
{
21-
enable: true,
22-
scriptFrom: 'local'
23-
})
24-
25-
// apply options with default
26-
const config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);
27-
28-
// Check if enabled
29-
if (!config.enable) {
30-
return;
31-
}
32-
3319
const generators = [];
3420

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

45-
let scriptURL;
31+
const defaultConfig = _.merge({},
32+
{
33+
enable: true,
34+
scriptFrom: 'local',
35+
});
4636

47-
switch (config.scriptFrom) {
48-
case 'local':
37+
// apply options with default
38+
let config = _.defaultsDeep({}, hexo.config.live2d, hexo.theme.config.live2d, defaultConfig);
39+
40+
41+
function getScriptURL(scriptFrom) {
42+
switch (scriptFrom) {
43+
case 'local':{
4944
// a.1 is local
5045
// use local(1)
5146
const scriptGenerators = buildGeneratorsFromManifest(manifest, path.dirname(mainfestPath), onSiteJsPath);
5247
const useHash = getFileMD5(path.resolve(path.dirname(mainfestPath), coreScriptName));
5348
generators.push(...scriptGenerators);
54-
scriptURL = `${url.resolve(onSiteJsPath, coreScriptName)}?${useHash}`;
55-
break;
49+
return `${url.resolve(onSiteJsPath, coreScriptName)}?${useHash}`;
50+
}
5651
case 'jsdelivr':
5752
// a.2 is jsdelivr online CDN
5853
// use jsdelivr(2)
59-
scriptURL = `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
60-
break;
54+
return `https://cdn.jsdelivr.net/npm/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
6155
case 'unpkg':
6256
// a.3 is unpkg online CDN
6357
// use unpkg(3)
64-
scriptURL = `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
65-
break;
58+
return `https://unpkg.com/live2d-widget@${coreJsDepVer}/lib/${coreScriptName}`;
6659
default:
6760
// a.4 is custom
6861
// use custom(4)
69-
scriptURL = config.scriptFrom;
70-
break;
62+
return scriptFrom;
63+
}
7164
}
7265

73-
if (config.model.use) {
74-
const modelInHexoBaseDir = [
66+
if (config.enable) {
67+
_.unset(config, 'enable');
68+
if (config.model.use) {
69+
const modelInHexoBaseDir = [
7570
path.resolve(hexo.base_dir, './live2d_models/', config.model.use),
7671
path.resolve(hexo.base_dir, config.model.use),
7772
]
78-
.reduce((p, path) => {
79-
if (!p && fs.existsSync(path))
80-
return path;
81-
else
82-
return p;
83-
}, undefined);
84-
const modelPaths = modelInHexoBaseDir || getNodeModulePath(config.model.use) || config.model.use; // Search paths
85-
const { modelGenerators, modelJsonUrl, packageJsonObj, type } = loadModelFrom(modelPaths, onSiteModelPath);
86-
generators.push(...modelGenerators);
87-
config = _.set(config, 'model.jsonPath', modelJsonUrl);
88-
switch(type){
73+
.reduce((p, path) => {
74+
if (!p && fs.existsSync(path))
75+
return path;
76+
else
77+
return p;
78+
}, undefined);
79+
const modelPaths = modelInHexoBaseDir || getNodeModulePath(config.model.use) || config.model.use; // Search paths
80+
const { modelGenerators, modelJsonUrl, packageJsonObj, type } = loadModelFrom(modelPaths, onSiteModelPath);
81+
generators.push(...modelGenerators);
82+
config = _.set(config, 'model.jsonPath', modelJsonUrl);
83+
switch(type){
8984
case 1:
9085
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded model from npm-module(1), ${packageJsonObj.name}@${packageJsonObj.version} from '${modelPaths}'`);
9186
break;
@@ -95,31 +90,34 @@ if (config.model.use) {
9590
case 4:
9691
console.log(`${colors.green('hexo-helper-live2d'.toUpperCase())}: Loaded Model from online(4), at '${modelJsonUrl}'`);
9792
break;
93+
}
9894
}
99-
}
100-
101-
/**
102-
* Deprecated version support
103-
* since 3.0
104-
* Don't manually add live2d tag into your site template
105-
*/
106-
107-
hexo.extend.helper.register('live2d', function () {
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.`);
109-
});
110-
111-
// injector borrowed form here:
112-
// https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
113-
hexo.extend.filter.register('after_render:html', function (htmlContent) {
114-
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
115-
const contentToInject = `<script src="${scriptURL}"></script><script>${scriptToInject}</script>`;
116-
if (/<\/body>/gi.test(htmlContent)) {
117-
let lastIndex = htmlContent.lastIndexOf('</body>');
118-
htmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`;
119-
}
120-
return htmlContent;
121-
});
12295

123-
hexo.extend.generator.register('live2d', function (locals) {
124-
return generators;
125-
});
96+
/**
97+
* Deprecated version support
98+
* since 3.0
99+
* Don't manually add live2d tag into your site template
100+
*/
101+
102+
hexo.extend.helper.register('live2d', function () {
103+
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.`);
104+
});
105+
106+
// injector borrowed form here:
107+
// https://github.com/Troy-Yang/hexo-lazyload-image/blob/master/lib/addscripts.js
108+
hexo.extend.filter.register('after_render:html', function (htmlContent) {
109+
const scriptFrom = config.scriptFrom;
110+
_.unset(config, 'scriptFrom');
111+
const scriptToInject = `L2Dwidget.init(${JSON.stringify(config)});`;
112+
const contentToInject = `<script src="${getScriptURL(scriptFrom)}"></script><script>${scriptToInject}</script>`;
113+
if (/<\/body>/gi.test(htmlContent)) {
114+
let lastIndex = htmlContent.lastIndexOf('</body>');
115+
htmlContent = `${htmlContent.substring(0, lastIndex)}${contentToInject}${htmlContent.substring(lastIndex, htmlContent.length)}`;
116+
}
117+
return htmlContent;
118+
});
119+
120+
hexo.extend.generator.register('live2d', function () {
121+
return generators;
122+
});
123+
}

lib/buildGenerator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55

6-
'use strict'
6+
'use strict';
77

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

@@ -19,4 +19,4 @@ module.exports = function buildGenerator(sourcePath, distPath) {
1919
path: distPath,
2020
data: () => fs.createReadStream(sourcePath),
2121
};
22-
}
22+
};

lib/buildGeneratorsFromManifest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55

6-
'use strict'
6+
'use strict';
77

88
const buildGenerator = require('./buildGenerator');
99
const path = require('path');
@@ -22,4 +22,4 @@ module.exports = function buildGeneratorsFromManifest(manifest, rootPath, distPa
2222
return files.map((file) => {
2323
return buildGenerator(file, url.resolve(distPath, path.basename(file)));
2424
});
25-
}
25+
};

lib/getFileMD5.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55

6-
'use strict'
6+
'use strict';
77

88
const crypto = require('crypto');
99
const fs = require('hexo-fs');
@@ -18,4 +18,4 @@ module.exports = function getFileMD5(filePath) {
1818
const rs = fs.readFileSync(filePath);
1919
const hash = crypto.createHash('md5');
2020
return (hash.update(rs).digest('hex'));
21-
}
21+
};

lib/getNodeModulePath.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55

6-
'use strict'
6+
'use strict';
77

88
const path = require('path');
99

@@ -19,4 +19,4 @@ module.exports = function getNodeModulePath(packageName) {
1919
}catch(e){
2020
return undefined;
2121
}
22-
}
22+
};

lib/listFiles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55

6-
'use strict'
6+
'use strict';
77

88
const fs = require('hexo-fs');
99
const path = require('path');
@@ -25,4 +25,4 @@ module.exports = function listFiles(dirPath) {
2525
else filesArr.push(pathName);
2626
}
2727
return filesArr;
28-
}
28+
};

lib/loadModelFrom.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55

6-
'use strict'
6+
'use strict';
77

88
const buildGenerator = require('./buildGenerator');
99
const fs = require('hexo-fs');
@@ -35,7 +35,7 @@ module.exports = function loadModelFrom(modelPaths, rootUrl) {
3535
assetsDir = path.resolve(modelPaths, './assets/'); // convert 1 to 2/3
3636
}else{
3737
if(fs.existsSync(modelPaths)){
38-
type = 2.5 // 2 or 3
38+
type = 2.5; // 2 or 3
3939
assetsDir = modelPaths;
4040
}else{
4141
type = 4;
@@ -45,7 +45,7 @@ module.exports = function loadModelFrom(modelPaths, rootUrl) {
4545
if(type !== 4){
4646
const modelFiles = listFiles(assetsDir);
4747
modelGenerators = modelFiles.map(file => {
48-
return buildGenerator(file, url.resolve(rootUrl, path.relative(assetsDir, file)))
48+
return buildGenerator(file, url.resolve(rootUrl, path.relative(assetsDir, file)));
4949
});
5050
modelJsonUrl = modelGenerators.reduce((p, generator) => {
5151
if (!p && generator.path.endsWith('.model.json')) {
@@ -61,6 +61,5 @@ module.exports = function loadModelFrom(modelPaths, rootUrl) {
6161
modelJsonUrl,
6262
packageJsonObj,
6363
type,
64-
}
65-
66-
}
64+
};
65+
};

0 commit comments

Comments
 (0)