-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.js
132 lines (123 loc) · 3.67 KB
/
plugin.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const fs = require("hexo-fs");
const path = require("path");
const axios = require("axios");
const cheerio = require("cheerio");
let hexo = null;
const ALL_PLUGINS_LIST_PATH = path.join(__dirname, "data", "plugins.json");
async function setup(hexoInstance) {
hexo = hexoInstance;
try {
//Check if update is needed.
if (fs.existsSync(ALL_PLUGINS_LIST_PATH)) {
const currentData = JSON.parse(fs.readFileSync(ALL_PLUGINS_LIST_PATH));
// One day in milliseconds
const oneDay = 1000 * 60 * 60 * 24;
const diffInTime = new Date().getTime() - new Date(currentData.updated).getTime();
const diffInDays = Math.round(diffInTime / oneDay);
if (diffInDays < 7) {
return; //No need to update.
}
}
//Parse the list of plugins from the hexo website.
const res = await axios.get("https://hexo.io/plugins/");
const $ = cheerio.load(res.data);
let byTags = {
all: {
plugins: [],
count: 0,
},
};
$("#plugin-list > .plugin").each((i, el) => {
const name = $(el).find(".plugin-name").text().trim();
const description = $(el).find(".plugin-desc").text().trim();
const link = $(el).find(".plugin-name").attr("href");
const tags = [];
$(el)
.find(".plugin-tag")
.each((i, el) => {
tags.push($(el).text().trim());
});
//Sort the plugins based on tags.
const plugin = { name, description, link, tags };
byTags.all.plugins.push(plugin);
byTags.all.count += 1;
plugin.tags.forEach((tag) => {
tag = tag.toString().trim().toLowerCase();
if (!byTags.hasOwnProperty(tag)) {
byTags[tag] = {
plugins: [],
count: 0,
};
}
byTags[tag].plugins.push(plugin);
byTags[tag].count += 1;
});
});
//Save the list of plugins to a file.
fs.writeFile(
ALL_PLUGINS_LIST_PATH,
JSON.stringify({
updated: new Date().toDateString(),
plugins: byTags,
})
);
} catch (error) {
//Do nothing, use the local file.
}
}
function getAll() {
const data = JSON.parse(fs.readFileSync(ALL_PLUGINS_LIST_PATH));
return data.plugins;
}
function getInstalled() {
const allPlugins = getAll();
const packageJson = require(path.join(hexo.base_dir, "package.json"));
let hexoPlugins = [];
//TODO: Exclude themes when hexo 5 is launched.
//https://github.com/hexojs/hexo/issues/3890
for (let module in packageJson.dependencies) {
if (module.startsWith("hexo-") && !module.startsWith("hexo-theme")) {
let moduleInfo = allPlugins.all.plugins.find((item) => item.name === module);
if (!moduleInfo) {
moduleInfo = {};
}
hexoPlugins.push({
name: module,
version: packageJson.dependencies[module],
description: moduleInfo.description,
link: moduleInfo.link,
});
}
}
return hexoPlugins;
}
function getScriptsFromDirectory(dir) {
const allScripts = [];
const files = fs.readdirSync(dir, { withFileTypes: true });
files.forEach((file) => {
if (file.isFile() && file.name.endsWith(".js")) {
allScripts.push({
name: file.name,
filePath: path.join(dir, file.name),
content: fs.readFileSync(path.join(dir, file.name)),
});
}
if (file.isDirectory()) {
allScripts.push(...getScriptsFromDirectory(path.join(dir, file.name)));
}
});
return allScripts;
}
function getScripts() {
if (fs.existsSync(hexo.script_dir)) {
return getScriptsFromDirectory(hexo.script_dir);
} else {
return [];
}
}
module.exports = {
setup: setup,
getAll: getAll,
getInstalled: getInstalled,
getScripts: getScripts,
};