-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
125 lines (116 loc) · 4.1 KB
/
publish.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
const Hexo = require('hexo');
const hexo = new Hexo(process.cwd(), {silent: false});
const fs = require('fs-extra');
const { join } = require('path');
const log = require('hexo-log').default({
debug: false,
silent: false
});
const { httpClientWithNonAuth, httpClientWithAuth } = require('../lib/httpClients');
const { objectsGenerator } = require('../lib/objectsGenerator');
const { postContent } = require('../lib/requests/postContent');
const { invalidateCache } = require('../lib/requests/invalidateCaches');
const { getAuthorId, getJwt } = require('../lib/requests/auth');
const { getCredential } = require('../lib/getCredential');
const { validate } = require('../lib/imageValidation');
const API_URL = process.argv[2];
const service = process.argv[3];
const authorName = process.argv[4];
(async () => {
const password = getCredential(service, authorName)
const author = getAuthorId(httpClientWithNonAuth(API_URL), authorName)
const token = await getJwt(httpClientWithNonAuth(API_URL), author, password)
try {
invalidateCache(httpClientWithAuth(API_URL, token));
log.info(`caches: invalidated`);
} catch(err) {
log.error(err);
} finally {
// Nothing todo
}
const daysAgo = process.argv[5] ? process.argv[5] : 5;
let cnt = 0;
let errorCnt = 0;
hexo.init().then(() => {
hexo.load().then(() => {
let date = new Date();
date = date.setDate(date.getDate() - daysAgo);
const wait = (ms) => new Promise(r => setTimeout(r, ms));
const postAsset = hexo.model('PostAsset');
const pageAsset = hexo.model('Asset');
const url = hexo.config.url;
(async () => {
const posts = hexo.locals.get('posts').filter(c => c.updated > date);
for (let post of posts.toArray()) {
const p = objectsGenerator(post, 'article', url);
if (!p) {
continue;
}
postContent(httpClientWithAuth(API_URL, token), p).then(response => {
cnt++;
log.info(`created - ${cnt}: ${response.data.id} - ${response.data.path}`);
const assets = postAsset.find({post: post._id}).toArray();
assets.forEach(a => {
validate(a.source)
// TODO: validate image
//if (validate(a.source)) {
fs.copy(a.source, join(hexo.base_dir, '_staticContentAssets', 'articles', a.path), (err) => {
if (err) {
log.error(err)
}
});
//}
});
})
.catch(error => {
try {
log.error(error.response.status);
errorCnt++
log.error(`error: - ${errorCnt} ${post.path}`);
} catch {
// Nothing todo
}
});
await wait(150);
}
})();
// TODO: DRY
(async () => {
// TODO: excludes scaffolds
const pages = hexo.locals.get('pages').filter(c => c.updated > date)
for(let page of pages.toArray()) {
const p = objectsGenerator(page, 'page', url);
if (!p) {
continue;
}
postContent(httpClientWithAuth(API_URL, token), p).then(response => {
cnt++;
log.info(`created - ${cnt}: ${response.data.id} - ${response.data.path}`);
const pageDir = page.path.slice(0, page.path.lastIndexOf("/"));
const assets = pageAsset.filter(x => x._id.includes(pageDir));
assets.forEach(a => {
// TODO: validate image
// if (validate(a.source)) {
fs.copy(a.source, join(hexo.base_dir, '_staticContentAssets', a.path), (err) => {
if (err) {
log.error(err)
}
});
// }
});
})
.catch(error => {
try {
log.error(error.response.status);
errorCnt++
log.error(`error: - ${errorCnt} ${post.path}`);
} catch {
// Nothing todo
}
});
await wait(150);
}
})();
});
});
})();