diff --git a/CHANGELOG.md b/CHANGELOG.md index 4161114..73ed1ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,3 +53,4 @@ First stable release. - When local server is installed but not running, Gramma will now try to use command-line interface for LanguageTool communication instead of spawning HTTP server (if possible). - Gramma will now automatically check for updates once a day. +- Added validation for languages and rules parameters. diff --git a/src/cli.js b/src/cli.js index ad7f1c9..1e4de2b 100755 --- a/src/cli.js +++ b/src/cli.js @@ -15,6 +15,9 @@ const paths = require("./commands/paths") const server = require("./commands/server") const { hook } = require("./commands/hook") +const { languageOptions } = require("./validators/languages") +const { ruleOptions } = require("./validators/rules") + // eslint-disable-next-line no-unused-expressions yargs .command( @@ -109,18 +112,21 @@ yargs type: "string", default: "config", describe: "Set the language of the text", + choices: languageOptions, }) .option("disable", { alias: "d", type: "string", describe: "Disable specific rule", default: [], + choices: ruleOptions, }) .option("enable", { alias: "e", type: "string", describe: "Enable specific rule", default: [], + choices: ruleOptions, }) .option("all", { alias: "a", diff --git a/src/requests/checkViaCmd.js b/src/requests/checkViaCmd.js index a71e9f3..747f5d5 100644 --- a/src/requests/checkViaCmd.js +++ b/src/requests/checkViaCmd.js @@ -1,5 +1,6 @@ const fs = require("fs") const path = require("path") +const kleur = require("kleur") const { execSync } = require("child_process") const initialConfig = require("../initialConfig") @@ -44,7 +45,12 @@ const MAX_REPLACEMENTS = 30 * * @returns {Promise} grammar checker suggestions */ -const checkViaCmd = (text, options = {}, serverDirPath, configDirPath) => { +const checkViaCmd = async ( + text, + options = {}, + serverDirPath, + configDirPath, +) => { const cfg = { ...initialConfig, ...options } // console.log({ cfg, serverDirPath, configDirPath }) @@ -65,19 +71,22 @@ const checkViaCmd = (text, options = {}, serverDirPath, configDirPath) => { const cmd = `java -jar ${jar}${lang}${disabled} --json ${tempFile}` let response + let result try { response = execSync(cmd, { stdio: "pipe" }) - response = response.toString() + response = response.toString().split("\n") + result = JSON.parse(response[response.length - 1]) } catch (e) { removeTempFile(tempFile) - throw new Error("Cannot execute command via local LanguageTool cmd") + + console.log(kleur.red("Cannot execute command via local LanguageTool cmd")) + console.log("Please check if your command if valid.") + process.exit(1) } removeTempFile(tempFile) - const result = JSON.parse(response) - const resultWithWords = { ...result, matches: removeFalsePositives( diff --git a/src/requests/checkWithFallback.js b/src/requests/checkWithFallback.js index 89bc665..b4170ad 100644 --- a/src/requests/checkWithFallback.js +++ b/src/requests/checkWithFallback.js @@ -25,7 +25,7 @@ const checkWithFallback = async (text, cfg) => { if (!session.markdown) { console.info(`Checking via local LanguageTool cmd...`) - response = checkViaCmd( + response = await checkViaCmd( text, session, global.server_path,