Added cli tool for adding new search engines/providers

This commit is contained in:
unknown 2021-08-06 16:16:13 +02:00
parent 1699146f79
commit 683c948f6c
6 changed files with 96 additions and 23 deletions

8
.prettierrc Normal file
View File

@ -0,0 +1,8 @@
{
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"arrowParens": "always",
"printWidth": 80,
"trailingComma": "es5"
}

5
api.js
View File

@ -9,8 +9,7 @@ api.use(express.static(join(__dirname, 'public')));
api.use('/uploads', express.static(join(__dirname, 'data/uploads')));
api.get(/^\/(?!api)/, (req, res) => {
res.sendFile(join(__dirname, 'public/index.html'));
})
});
// Body parser
api.use(express.json());
@ -25,4 +24,4 @@ api.use('/api/bookmarks', require('./routes/bookmark'));
// Custom error handler
api.use(errorHandler);
module.exports = api;
module.exports = api;

View File

@ -12113,6 +12113,12 @@
"resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-1.0.4.tgz",
"integrity": "sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw="
},
"prettier": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz",
"integrity": "sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==",
"dev": true
},
"pretty-bytes": {
"version": "5.6.0",
"resolved": "https://registry.npmjs.org/pretty-bytes/-/pretty-bytes-5.6.0.tgz",

View File

@ -54,5 +54,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"prettier": "^2.3.2"
}
}

View File

@ -1,9 +1,9 @@
{
"queries": [
{
"name": "Google",
"prefix": "g",
"template": "https://www.google.com/search?q="
"name": "Disroot",
"prefix": "ds",
"template": "http://search.disroot.org/search?q="
},
{
"name": "DuckDuckGo",
@ -11,19 +11,9 @@
"template": "https://duckduckgo.com/?q="
},
{
"name": "Disroot",
"prefix": "ds",
"template": "http://search.disroot.org/search?q="
},
{
"name": "YouTube",
"prefix": "yt",
"template": "https://www.youtube.com/results?search_query="
},
{
"name": "Reddit",
"prefix": "r",
"template": "https://www.reddit.com/search?q="
"name": "Google",
"prefix": "g",
"template": "https://www.google.com/search?q="
},
{
"name": "IMDb",
@ -31,14 +21,24 @@
"template": "https://www.imdb.com/find?q="
},
{
"name": "The Movie Database",
"prefix": "mv",
"template": "https://www.themoviedb.org/search?query="
"name": "Reddit",
"prefix": "r",
"template": "https://www.reddit.com/search?q="
},
{
"name": "Spotify",
"prefix": "sp",
"template": "https://open.spotify.com/search/"
},
{
"name": "The Movie Database",
"prefix": "mv",
"template": "https://www.themoviedb.org/search?query="
},
{
"name": "YouTube",
"prefix": "yt",
"template": "https://www.youtube.com/results?search_query="
}
]
}
}

View File

@ -0,0 +1,57 @@
const queries = require('../../src/utility/searchQueries.json');
const fs = require('fs');
const prettier = require('prettier');
/**
* @description CLI tool for adding new search engines/providers. It will ensure that prefix is unique and that all entries are sorted alphabetically
* @argumens name prefix template
* @example node cli-searchQueries.js "DuckDuckGo" "d" "https://duckduckgo.com/?q="
*/
// Get arguments
const args = process.argv.slice(2);
// Check arguments
if (args.length < 3) {
return console.log('Missing arguments');
} else if (args.length > 3) {
return console.log('Too many arguments provided');
}
// Construct new query object
const newQuery = {
name: args[0],
prefix: args[1],
template: args[2],
};
// Get old queries
let rawQueries = queries.queries;
let parsedQueries = '';
// Check if prefix is unique
const isUnique = !rawQueries.find((query) => query.prefix == newQuery.prefix);
if (!isUnique) {
return console.log('Prefix already exists');
}
// Add new query
rawQueries.push(newQuery);
// Sort alphabetically
rawQueries = rawQueries.sort((a, b) => {
const _a = a.name.toLowerCase();
const _b = b.name.toLowerCase();
if (_a < _b) return -1;
if (_a > _b) return 1;
return 0;
});
// Format JSON
parsedQueries = JSON.stringify(queries);
parsedQueries = prettier.format(parsedQueries, { parser: 'json' });
// Save file
fs.writeFileSync('../../src/utility/searchQueries.json', parsedQueries);