-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
85 lines (82 loc) · 2.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const { readFileSync, statSync, readdirSync } = require('fs');
const { join, relative } = require('path');
function walk(dir, callback) {
readdirSync(dir).forEach(file => {
const filepath = join(dir, file);
const stats = statSync(filepath);
if (stats.isDirectory()) {
walk(filepath, callback);
} else if (stats.isFile()) {
callback(filepath);
}
});
}
function readFile(plugin_dir, value) {
let { name, file, dir } = value;
if (!file) file = dir;
const data = [];
const base = `${plugin_dir}/${name}`;
try {
statSync(base);
} catch (err) {
return {
error: err.message
};
}
const origin = `${base}/${file}`;
const dist = `lib/${name}/${file}`;
let stats;
try {
stats = statSync(origin);
} catch (err) {
return {
error: err.message
};
}
if (stats.isDirectory()) {
walk(origin, path => {
data.push({
path: join(dist, relative(origin, path)),
data: readFileSync(path)
});
});
} else if (stats.isFile()) {
data.push({
path: dist,
data: readFileSync(origin)
});
}
return {
data
};
}
module.exports = function(hexo, vendors) {
let generator = [];
let errors = [];
vendors.fontawesome_font = {
name: '@fortawesome/fontawesome-free',
file: 'webfonts'
};
vendors.katex_font = {
name: 'katex',
file: 'dist/fonts'
};
vendors.mathjax_font = {
name: 'mathjax',
file: 'es5/output/chtml/fonts'
};
for (const value of Object.values(vendors)) {
const { data, error } = readFile(hexo.plugin_dir, value);
if (data) generator = generator.concat(data);
if (error) errors.push(error);
}
if (errors.length) {
errors = [...new Set(errors)];
hexo.log.warn('The following packages are not found by `@next-theme/plugins`.');
errors.forEach(error => {
hexo.log.warn(error);
});
hexo.log.warn('Maybe you can find the solution here: https://github.com/next-theme/plugins#debug');
}
hexo.extend.generator.register('next_vendors', () => generator);
};