forked from funbox/languagetool-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·92 lines (75 loc) · 2.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env node
const axios = require('axios');
const deepmerge = require('deepmerge');
const fs = require('fs');
const os = require('os');
const ora = require('ora');
const path = require('path');
const reporter = require('vfile-reporter');
const checkJavaInstalled = require('./lib/checkJavaInstalled');
const createVfile = require('./lib/createVfile');
const generateReport = require('./lib/generateReport');
const { error, info } = require('./lib/log');
const startLanguageToolServer = require('./lib/startLanguageToolServer');
if (!checkJavaInstalled()) {
error('To use this command-line tool you need to install a JDK.');
info('Please visit the Java Developer Kit download website: https://www.java.com');
process.exit(1);
}
/* eslint-disable import/no-dynamic-require */
const configName = '.languagetoolrc.js';
const defaultConfig = require(`./${configName}`);
const externalConfigPath = path.join(os.homedir(), configName);
const appConfig = fs.existsSync(externalConfigPath)
? deepmerge(defaultConfig, require(externalConfigPath))
: defaultConfig;
/* eslint-enable import/no-dynamic-require */
const processArgs = process.argv.slice(2);
let files = [];
if (!process.stdin.isTTY && process.platform !== 'win32') {
// When Git BASH terminal is used we can't get data from STDIN.
// That's why it's turned off here, and it's impossible to use STDIN in Windows.
files.push(createVfile());
} else {
files = processArgs
.filter(file => fs.existsSync(file))
.map(createVfile);
}
if (files.length) {
check(files);
}
async function check(vfiles) {
const spinner = ora('Processing...').start();
try {
const { port } = await startLanguageToolServer();
// eslint-disable-next-line no-restricted-syntax
for (const vfile of vfiles) {
// eslint-disable-next-line no-await-in-loop
const matches = await axios({
url: `http://127.0.0.1:${port}/v2/check`,
method: 'post',
params: {
language: 'auto',
text: String(vfile.contents),
},
}).then(response => response.data.matches);
const filteredMatches = matches.filter(match => {
const ctx = match.context;
const badWord = ctx.text.slice(ctx.offset, ctx.offset + ctx.length);
return !appConfig.ignore.some(goodWord => RegExp(`^${goodWord}$`, 'i').test(badWord));
});
if (filteredMatches.length) {
generateReport({ matches: filteredMatches, vfile });
process.exitCode = 1;
}
}
spinner.stop();
console.log(reporter(vfiles, { quiet: true }));
} catch (err) {
spinner.stop();
error(err);
process.exitCode = 1;
}
process.exit();
}
process.on('unhandledRejection', error);