-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
340 lines (302 loc) · 7.79 KB
/
utils.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
const config = require('./searchConfig.json')
const _ = require('lodash')
const createHtmlElement = require('create-html-element')
const isTravis = require('is-travis')
const fs = require('fs')
const arvish = require('arvish')
!isTravis && require('dotenv').config()
function authenticationNotProgressed () {
arvish.output([
{
valid: false,
title: 'Authentication has not progressed.',
subtitle: 'Please get an API token by reference to README.md',
autocomplete: '',
arg: 'error'
}
])
process.exit()
}
function getEnv (object) {
let result = ''
for (const key of Object.keys(object)) {
result += `${key}=${object[key]}
`
}
return result
}
function getEnvAsObject (envPath) {
const result = {}
const lines = fs.readFileSync(envPath).toString().split('\n')
for (const lineIndex in lines) {
if (lines[lineIndex].trim() === '') continue
const [key, ...value] = lines[lineIndex].split('=')
result[key] = value.join('=')
}
return result
}
function replaceAll (string, search, replace) {
return string.split(search).join(replace)
}
const handleInput = (str) => {
const re = /\$\{.*\}/
const [first, ...query] = str.split(' ')
if (re.test(first)) {
return query.join(' ')
}
return str
}
async function fetchTagGuid (targetName, tags) {
let result = -1
_.transform(tags, (tags, tag) => {
if (tag.name === targetName) {
result = tag.guid
return false
}
return true
})
return result === -1 ? [] : [result]
}
async function fetchNotebookGuid (targetName, notebooks) {
let result = -1
_.transform(notebooks, (notebooks, notebook) => {
if (notebook.name === targetName) {
result = notebook.guid
return false
}
return true
})
return result === -1 ? undefined : result
}
function ab2str (buf) {
// _.map() not working
let result = ''
for (let i = 0; i < buf.length; i++) {
const newValue = buf[i] < 16 ? `0${buf[i].toString(16)}` : buf[i].toString(16)
result += newValue
}
return result
}
const insertResource = (htmlStr) => {
// <en-media hash="{hash}" type="image/png" />
const imageCheckingRegexp = /(.*)<en-media hash="(\w*)" type="image\/([\w]*)"(.*)/g
while (imageCheckingRegexp.test(htmlStr)) {
// 1: pre string of en-media
// 2: resource hash
// 3: resource type
// 4: post string of en-media
htmlStr = htmlStr.replace(imageCheckingRegexp, '$1<img src="_$2.$3"$4')
}
return htmlStr
}
const catchThriftException = func => async (...args) => {
try {
return await func(...args)
} catch (err) {
if (err) {
switch (err.errorCode) {
case 2: {
const title = 'Not valid oauth token'
return [
{
valid: false,
title,
subtitle: 'Please read README.md first',
arg: 'error',
text: {
copy: title,
largetype: title
},
icon: {
path: './icon/warning.png'
}
}
]
}
case 19: {
const title = "Evernote sdk's ratelimit has reached its limit."
return [
{
valid: true,
title,
subtitle: 'Please try again in an hour.',
arg: 'error',
text: {
copy: title,
largetype: title
},
icon: {
path: './icon/warning.png'
}
}
]
}
}
}
}
}
const handleSubtitleRestrictor = func => async (count, ...args) => {
if (count <= config.subtitle_restrictor) return await func(...args)
}
const decideSearchOrder = (option) => {
// 'CREATED': 1,
// 'UPDATED': 2,
// 'RELEVANCE': 3,
// 'UPDATE_SEQUENCE_NUMBER': 4,
// 'TITLE': 5
switch (option) {
case 'created':
return 1
case 'updated':
return 2
case 'relevance':
return 3
case 'title':
return 5
}
}
const getTimeString = (updatedTimestamp) => {
const dateObj = new Date(updatedTimestamp)
const year = dateObj.getFullYear()
let month = dateObj.getMonth() + 1
if (month < 10) month = '0' + month
let day = dateObj.getDate()
if (day < 10) day = '0' + day
let time = dateObj.getHours() * 3600 + dateObj.getMinutes() * 60 + dateObj.getSeconds()
if ((time >= 1000) && time < 10000) time = '0' + time
else if ((time >= 100) && time < 1000) time = '00' + time
else if ((time >= 10) && time < 100) time = '000' + time
else if (time < 10) time = '0000' + time
const weightValue = `${year}${month}${day}${time}`
return parseFloat(weightValue)
}
const getHtmlMetaData = ({ title, updated, created }) => {
const locale = process.env.systemLocale
const fontSize = 'font-size: 20;'
const fontFamily =
'font-family: "Trebuchet MS", "Lucida Sans Unicode", "Lucida Grande", "Lucida Sans", Arial, sans-serif;'
const marginBottom = 'margin-bottom: 15;'
const htmlStyle = `
img {
max-width: 100%;
height: auto;
}`
return (
createHtmlElement({
name: 'style',
html: htmlStyle
}) +
createHtmlElement({
name: 'p',
attributes: {
id: 'title',
style: `${fontSize} ${fontFamily}`
},
html: `Title: ${title}`
}) +
createHtmlElement({
name: 'p',
attributes: {
id: 'editedDate',
style: `${fontSize} ${fontFamily}`
},
html: `Last Edited In: ${getLocaleString(updated, locale)}`
}) +
createHtmlElement({
name: 'p',
attributes: {
id: 'creationDate',
style: `${fontSize} ${fontFamily}`
},
html: `Created In: ${getLocaleString(created, locale)}`
}) +
createHtmlElement({
name: 'hr',
attributes: {
style: `${marginBottom}`
}
})
)
}
const getLocaleString = (datetime, locale) => {
const dateObj = new Date(datetime)
const year = dateObj.getFullYear()
const month = dateObj.getMonth() + 1
const day = dateObj.getDate()
const hour =
// AM 12
dateObj.getHours() === 0 ? 12
// PM 12
: dateObj.getHours() === 12 ? 12
// Other times
: dateObj.getHours() % 12
const minute =
dateObj.getMinutes() < 10
? `0${dateObj.getMinutes()}`
: dateObj.getMinutes()
const seconds =
dateObj.getSeconds() < 10
? `0${dateObj.getSeconds()}`
: dateObj.getSeconds()
switch (locale) {
case 'ko-KR': {
const koDayOfTheWeek = [
'일요일',
'월요일',
'화요일',
'수요일',
'목요일',
'금요일',
'토요일'
]
const isPM = dateObj.getHours() >= 12 ? '오후' : '오전'
const dayOfTheWeek = koDayOfTheWeek[dateObj.getDay() % 7]
return `${year}년 ${month}월 ${day}일 ${dayOfTheWeek} ${isPM} ${hour}:${minute}:${seconds}`
}
default: {
const enDayOfTheWeek = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
]
const enMonthStr = [
'December',
'January',
'Feburary',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November'
]
const isPM = dateObj.getHours() >= 12 ? 'PM' : 'AM'
const dayOfTheWeek = enDayOfTheWeek[dateObj.getDay() % 7]
const monthStr = enMonthStr[month % 12]
return `${dayOfTheWeek}, ${monthStr} ${day}, ${year} ${hour}:${minute}:${seconds} ${isPM}`
}
}
}
module.exports = {
ab2str,
authenticationNotProgressed,
getEnv,
getEnvAsObject,
fetchTagGuid,
fetchNotebookGuid,
replaceAll,
handleInput,
getTimeString,
decideSearchOrder,
catchThriftException,
handleSubtitleRestrictor,
insertResource,
getHtmlMetaData
}