forked from zao95/tech-blog-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (49 loc) · 1.62 KB
/
Copy pathindex.js
File metadata and controls
55 lines (49 loc) · 1.62 KB
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
#!/usr/bin/env node
const fs = require('fs/promises')
const open = require('open')
const prompts = require('prompts')
const path = require('path')
const techBlogSearch = async () => {
const response = await prompts([
{
type: 'multiselect',
name: 'languages',
message: 'Choose the language you want.',
choices: [
{ title: 'English', value: 'en' },
{ title: 'Korean', value: 'kr' },
],
hint: '- Space to select. Return to submit',
min: 1,
},
{
type: 'text',
name: 'searchWord',
message: 'What do you want to search?',
validate: (searchWord) =>
searchWord.length <= 1
? 'You must enter at least two letters of search word.'
: true,
},
])
const db = await fs.readFile(path.join(__dirname, 'db.json'), 'utf8')
const data = JSON.parse(db)
const toSearch = []
for (const datum in data) {
if (response.languages.includes(data[datum].lang)) {
toSearch.push(data[datum].url)
}
}
// Maximum query is 2048
const searchWord = response.searchWord
const query = toSearch.slice(1).reduce((prev, curr) => {
const nextValue = prev + ` OR site:${curr}`
if (searchWord.length + nextValue.length + 1 >= 2048) {
return prev
} else {
return nextValue
}
}, `site:${toSearch[0]}`)
open(encodeURI(`https://www.google.com/search?q=${searchWord} ${query}`))
}
;(async () => await techBlogSearch())()