-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathindex.ts
130 lines (115 loc) · 3.2 KB
/
index.ts
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
"use strict";
import { createWriteStream, mkdirSync } from "hexo-fs";
import createLogger from "hexo-log";
import { generate } from "./lib/generate";
import path from "path";
const supported_types = ["book", "movie", "game", "song"];
const log = createLogger({
debug: false,
silent: false,
});
// Register `hexo g` and `hexo s`
supported_types.forEach((type) => {
hexo.extend.generator.register(`${type}s`, function (locals) {
if (
!this.config.douban ||
!this.config.douban[type] ||
!this.config.douban.builtin
) {
return;
}
let path = this.config.douban[type].path;
if (path) {
this.config.douban[type].path = path.replace(/^\//, "");
} else {
this.config.douban[type].path = `${type}s/index.html`;
}
return generate(
this.config.lang,
this.config.url,
type,
this.config.douban
) as any;
});
});
const options = {
options: [
{ name: "-b, --books", desc: "Generate douban books only" },
{ name: "-m, --movies", desc: "Generate douban movies only" },
{ name: "-g, --games", desc: "Generate douban games only" },
{ name: "-s, --songs", desc: "Generate douban songs only" },
],
};
// Register `hexo douban`
hexo.extend.console.register(
"douban",
"Generate pages from douban",
options,
function (args) {
if (!this.config.douban) {
log.info("No douban config specified");
return;
}
if (!this.config.douban.id) {
log.info("No douban id specified");
return;
}
let force_types: string[] = [];
supported_types.forEach((supported_type) => {
if (
(Object.keys(args).includes(supported_type[0]) ||
Object.keys(args).includes(`${supported_type}s`)) &&
this.config.douban[supported_type]
) {
force_types.push(supported_type);
}
});
let enabled_types: string[] = [];
if (force_types.length !== 0) {
enabled_types = force_types;
} else {
supported_types.forEach((type) => {
if (this.config.douban[type]) {
enabled_types.push(type);
}
});
}
if (enabled_types.length === 0) {
log.info("No douban type specified");
return;
}
// Prepare path
enabled_types.forEach((type) => {
let path = this.config.douban[type].path;
if (path) {
this.config.douban[type].path = path.replace(/^\//, "");
} else {
this.config.douban[type].path = `${type}s/index.html`;
}
hexo.extend.generator.register(type, function (locals) {
return generate(
this.config.lang,
this.config.url,
type,
this.config.douban
) as any;
});
});
const self = this;
//Generate files
self.load().then(function () {
enabled_types.forEach((type) => {
const publicDir = self.public_dir;
const id = self.config.douban[type].path;
mkdirSync(path.join(publicDir, id.replace("index.html", "")), {
recursive: true,
});
let stream = self.route.get(id);
if (stream) {
self.route.get(id).pipe(createWriteStream(path.join(publicDir, id)));
log.info("Generated: %s", id);
}
});
});
}
);