From 683c948f6c433f07dbade0b496e1c8d03b9ce3f6 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 6 Aug 2021 16:16:13 +0200 Subject: [PATCH] Added cli tool for adding new search engines/providers --- .prettierrc | 8 ++++ api.js | 5 +-- client/package-lock.json | 6 +++ client/package.json | 3 ++ client/src/utility/searchQueries.json | 40 +++++++++---------- client/utils/dev/cli-searchQueries.js | 57 +++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 .prettierrc create mode 100644 client/utils/dev/cli-searchQueries.js diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..f14788c --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +{ + "tabWidth": 2, + "useTabs": false, + "singleQuote": true, + "arrowParens": "always", + "printWidth": 80, + "trailingComma": "es5" +} diff --git a/api.js b/api.js index a720fe2..1c2d863 100644 --- a/api.js +++ b/api.js @@ -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; \ No newline at end of file +module.exports = api; diff --git a/client/package-lock.json b/client/package-lock.json index 8326fc6..2717839 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -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", diff --git a/client/package.json b/client/package.json index f50079e..6e05667 100644 --- a/client/package.json +++ b/client/package.json @@ -54,5 +54,8 @@ "last 1 firefox version", "last 1 safari version" ] + }, + "devDependencies": { + "prettier": "^2.3.2" } } diff --git a/client/src/utility/searchQueries.json b/client/src/utility/searchQueries.json index 6e23503..c4e055d 100644 --- a/client/src/utility/searchQueries.json +++ b/client/src/utility/searchQueries.json @@ -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=" } ] -} \ No newline at end of file +} diff --git a/client/utils/dev/cli-searchQueries.js b/client/utils/dev/cli-searchQueries.js new file mode 100644 index 0000000..c431b32 --- /dev/null +++ b/client/utils/dev/cli-searchQueries.js @@ -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);