Skip to content

Commit 683c948

Browse files
committed
Added cli tool for adding new search engines/providers
1 parent 1699146 commit 683c948

File tree

6 files changed

+92
-19
lines changed

6 files changed

+92
-19
lines changed

.prettierrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"tabWidth": 2,
3+
"useTabs": false,
4+
"singleQuote": true,
5+
"arrowParens": "always",
6+
"printWidth": 80,
7+
"trailingComma": "es5"
8+
}

api.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ api.use(express.static(join(__dirname, 'public')));
99
api.use('/uploads', express.static(join(__dirname, 'data/uploads')));
1010
api.get(/^\/(?!api)/, (req, res) => {
1111
res.sendFile(join(__dirname, 'public/index.html'));
12-
})
13-
12+
});
1413

1514
// Body parser
1615
api.use(express.json());
@@ -25,4 +24,4 @@ api.use('/api/bookmarks', require('./routes/bookmark'));
2524
// Custom error handler
2625
api.use(errorHandler);
2726

28-
module.exports = api;
27+
module.exports = api;

client/package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,8 @@
5454
"last 1 firefox version",
5555
"last 1 safari version"
5656
]
57+
},
58+
"devDependencies": {
59+
"prettier": "^2.3.2"
5760
}
5861
}

client/src/utility/searchQueries.json

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
{
22
"queries": [
33
{
4-
"name": "Google",
5-
"prefix": "g",
6-
"template": "https://www.google.com/search?q="
4+
"name": "Disroot",
5+
"prefix": "ds",
6+
"template": "http://search.disroot.org/search?q="
77
},
88
{
99
"name": "DuckDuckGo",
1010
"prefix": "d",
1111
"template": "https://duckduckgo.com/?q="
1212
},
1313
{
14-
"name": "Disroot",
15-
"prefix": "ds",
16-
"template": "http://search.disroot.org/search?q="
14+
"name": "Google",
15+
"prefix": "g",
16+
"template": "https://www.google.com/search?q="
1717
},
1818
{
19-
"name": "YouTube",
20-
"prefix": "yt",
21-
"template": "https://www.youtube.com/results?search_query="
19+
"name": "IMDb",
20+
"prefix": "im",
21+
"template": "https://www.imdb.com/find?q="
2222
},
2323
{
2424
"name": "Reddit",
2525
"prefix": "r",
2626
"template": "https://www.reddit.com/search?q="
2727
},
2828
{
29-
"name": "IMDb",
30-
"prefix": "im",
31-
"template": "https://www.imdb.com/find?q="
29+
"name": "Spotify",
30+
"prefix": "sp",
31+
"template": "https://open.spotify.com/search/"
3232
},
3333
{
3434
"name": "The Movie Database",
3535
"prefix": "mv",
3636
"template": "https://www.themoviedb.org/search?query="
3737
},
3838
{
39-
"name": "Spotify",
40-
"prefix": "sp",
41-
"template": "https://open.spotify.com/search/"
39+
"name": "YouTube",
40+
"prefix": "yt",
41+
"template": "https://www.youtube.com/results?search_query="
4242
}
4343
]
44-
}
44+
}

client/utils/dev/cli-searchQueries.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const queries = require('../../src/utility/searchQueries.json');
2+
const fs = require('fs');
3+
const prettier = require('prettier');
4+
5+
/**
6+
* @description CLI tool for adding new search engines/providers. It will ensure that prefix is unique and that all entries are sorted alphabetically
7+
* @argumens name prefix template
8+
* @example node cli-searchQueries.js "DuckDuckGo" "d" "https://duckduckgo.com/?q="
9+
*/
10+
11+
// Get arguments
12+
const args = process.argv.slice(2);
13+
14+
// Check arguments
15+
if (args.length < 3) {
16+
return console.log('Missing arguments');
17+
} else if (args.length > 3) {
18+
return console.log('Too many arguments provided');
19+
}
20+
21+
// Construct new query object
22+
const newQuery = {
23+
name: args[0],
24+
prefix: args[1],
25+
template: args[2],
26+
};
27+
28+
// Get old queries
29+
let rawQueries = queries.queries;
30+
let parsedQueries = '';
31+
32+
// Check if prefix is unique
33+
const isUnique = !rawQueries.find((query) => query.prefix == newQuery.prefix);
34+
35+
if (!isUnique) {
36+
return console.log('Prefix already exists');
37+
}
38+
39+
// Add new query
40+
rawQueries.push(newQuery);
41+
42+
// Sort alphabetically
43+
rawQueries = rawQueries.sort((a, b) => {
44+
const _a = a.name.toLowerCase();
45+
const _b = b.name.toLowerCase();
46+
47+
if (_a < _b) return -1;
48+
if (_a > _b) return 1;
49+
return 0;
50+
});
51+
52+
// Format JSON
53+
parsedQueries = JSON.stringify(queries);
54+
parsedQueries = prettier.format(parsedQueries, { parser: 'json' });
55+
56+
// Save file
57+
fs.writeFileSync('../../src/utility/searchQueries.json', parsedQueries);

0 commit comments

Comments
 (0)