-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
209 lines (184 loc) · 6.59 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* global hexo */
'use strict';
const AV = require('leancloud-storage');
const readlineSync = require('readline-sync');
const packageInfo = require('./package.json');
const pathFn = require('path');
const fs = require('fs');
function generate_post_list(locals) {
let { config } = this;
if (!config.leancloud_counter_security.enable_sync) return;
var urlsPath = 'leancloud_counter_security_urls.json';
var urls = [...locals.posts.toArray()]
.filter(x => x.published)
.map(x => {
return {
title: x.title,
url : config.root + x.path
};
});
return {
path: urlsPath,
data: JSON.stringify(urls)
};
}
hexo.extend.generator.register('leancloud_counter_security_generator', generate_post_list);
function cmp(x, y) {
if (x.url < y.url) {
return -1;
} else if (x.url === y.url) {
return 0;
}
return 1;
}
function postOperation(env, cnt, limit, newData, memoData) {
if (cnt !== limit) return;
newData.sort(cmp);
var sourceDir = env.source_dir;
var publicDir = env.public_dir;
var memoFile = pathFn.join(sourceDir, 'leancloud.memo');
fs.writeFileSync(memoFile, '[\n');
var memoIdx = 1;
for (var i = 0; newData[i]; i++) {
while (memoData[memoIdx] !== ']') {
var y = JSON.parse(memoData[memoIdx].substring(0, memoData[memoIdx].length - 1));
if (y.url > newData[i].url) break;
fs.writeFileSync(memoFile, memoData[memoIdx] + '\n', { flag: 'a' });
memoIdx++;
}
fs.writeFileSync(memoFile, '{"title":"' + newData[i].title + '","url":"' + newData[i].url + '"},\n', { flag: 'a' });
}
while (memoData[memoIdx] !== ']') {
fs.writeFileSync(memoFile, memoData[memoIdx] + '\n', { flag: 'a' });
memoIdx++;
}
fs.writeFileSync(memoFile, memoData[memoIdx], { flag: 'a' });
var srcFile = pathFn.join(sourceDir, 'leancloud.memo');
var destFile = pathFn.join(publicDir, 'leancloud.memo');
var readStream = fs.createReadStream(srcFile);
var writeStream = fs.createWriteStream(destFile);
readStream.pipe(writeStream);
this.log.info('leancloud.memo successfully updated.');
}
async function sync() {
let { log, config } = this;
if (!config.leancloud_counter_security.enable_sync) return;
let { app_id, app_key, server_url } = config.leancloud_counter_security;
var publicDir = this.public_dir;
var UrlsFile = pathFn.join(publicDir, 'leancloud_counter_security_urls.json');
var urls = JSON.parse(fs.readFileSync(UrlsFile, 'utf8'));
AV.init({
appId : app_id,
appKey : app_key,
serverURL: server_url
});
var currentUser = AV.User.current();
if (!currentUser) {
var userName = config.leancloud_counter_security.username;
var passWord = config.leancloud_counter_security.password;
if (!userName) {
userName = readlineSync.question('Enter your username: ');
passWord = readlineSync.question('Enter your password: ', { hideEchoBack: true });
} else if (!passWord) {
passWord = readlineSync.question('Enter your password: ', { hideEchoBack: true });
}
await AV.User.logIn(userName, passWord).then(loginedUser => {
log.info('Logined as: ' + loginedUser.getUsername());
}, error => {
log.error(error);
});
}
log.info('Now syncing your posts list to leancloud counter...');
var Counter = AV.Object.extend('Counter');
var memoFile = pathFn.join(publicDir, 'leancloud.memo');
if (!fs.existsSync(memoFile)) {
fs.writeFileSync(memoFile, '[\n]');
}
var memoData = fs.readFileSync(memoFile, 'utf-8').split('\n');
var memoIdx = 1;
var newData = [];
var cnt = 0;
var limit = 0;
var env = this;
urls.sort(cmp).forEach(x => {
var y = {};
y.title = '';
y.url = '';
var flag = false;
while (memoData[memoIdx] !== ']') {
y = JSON.parse(memoData[memoIdx].substring(0, memoData[memoIdx].length - 1));
if (y.url > x.url) break;
if (y.url === x.url && y.title === x.title) {
flag = true;
break;
}
memoIdx++;
}
if (flag) return;
log.info('Dealing with record of ' + x.title);
limit++;
var query = new AV.Query('Counter');
query.equalTo('url', x.url);
query.count().then(count => {
if (count === 0) {
var counter = new Counter();
counter.set('url', x.url);
counter.set('title', x.title);
counter.set('time', 0);
counter.save().then(obj => {
log.info(x.title + ' is saved as: ' + obj.id);
newData.push(x);
cnt++;
postOperation(env, cnt, limit, newData, memoData);
}, error => {
log.error(error);
cnt++;
postOperation(env, cnt, limit, newData, memoData);
});
} else {
newData.push(x);
cnt++;
postOperation(env, cnt, limit, newData, memoData);
}
}, error => {
log.error(error);
cnt++;
postOperation(env, cnt, limit, newData, memoData);
});
});
}
hexo.extend.deployer.register('leancloud_counter_security_sync', sync);
var commandOptions = {
desc : packageInfo.description,
usage : ' <argument>',
arguments: [
{
name: 'register | r <username> <password>',
desc: 'Register a new user.'
}
]
};
function commandFunc(args) {
let { log, config } = this;
if (args._.length !== 3) {
log.error('Too Few or Many Arguments.');
} else if (args._[0] === 'register' || args._[0] === 'r') {
let { app_id, app_key, server_url } = config.leancloud_counter_security;
AV.init({
appId : app_id,
appKey : app_key,
serverURL: server_url
});
var user = new AV.User();
user.setUsername(String(args._[1]));
user.setPassword(String(args._[2]));
user.signUp().then(loginedUser => {
log.info(loginedUser.getUsername() + ' is successfully signed up');
}, error => {
log.error(error);
});
} else {
log.error('Unknown Command.');
}
}
hexo.extend.console.register('lc-counter', 'hexo-leancloud-counter-security', commandOptions, commandFunc);