Skip to content

Commit

Permalink
Added validation for languages and rules parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
caderek committed Oct 17, 2021
1 parent 1b421a4 commit 897dae6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 6 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down
19 changes: 14 additions & 5 deletions src/requests/checkViaCmd.js
Original file line number Diff line number Diff line change
@@ -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")

Expand Down Expand Up @@ -44,7 +45,12 @@ const MAX_REPLACEMENTS = 30
*
* @returns {Promise<Object>} 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 })

Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/requests/checkWithFallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 897dae6

Please sign in to comment.