Run vale linter only if vale is installed

This commit is contained in:
Himanshu Mishra
2020-10-12 14:27:59 +02:00
parent caeb82b3d0
commit 689b2f50e5
3 changed files with 57 additions and 37 deletions
+50 -36
View File
@@ -16,6 +16,8 @@
*/
const { execSync, spawnSync } = require('child_process');
const path = require('path');
// eslint-disable-next-line import/no-extraneous-dependencies
const commandExists = require('command-exists');
const listFilesTrackedByGit = 'git ls-files';
@@ -23,46 +25,58 @@ const inheritStdIo = {
stdio: 'inherit',
};
const LINT_SKIPPED_MESSAGE =
'Skipping documentation quality check (vale not found). Install vale linter (https://docs.errata.ai/vale/install) to enable.\n';
const ERROR_MESSAGE =
'Please install vale linter(https://docs.errata.ai/vale/install). Ignore this message if already installed.\n';
'Language linter (vale) generated errors. Please check the errors and review any markdown files that you changed.\n';
// xargs is not supported by shx.
if (process.platform === 'win32') {
const validMDFilesCommand = `${listFilesTrackedByGit} | .\\node_modules\\.bin\\shx grep ".md"`;
try {
// get list of all md files except in directories of gitignore.
let filesToLint = execSync(validMDFilesCommand, {
stdio: ['ignore', 'pipe', 'inherit'],
});
// Proceed with the script only if Vale linter is installed. Limit the friction and surprises caused by the script.
commandExists('vale')
.catch(() => {
console.log(LINT_SKIPPED_MESSAGE);
process.exit(0);
})
.then(() => {
// xargs is not supported by shx.
if (process.platform === 'win32') {
const validMDFilesCommand = `${listFilesTrackedByGit} | .\\node_modules\\.bin\\shx grep ".md"`;
try {
// get list of all md files except in directories of gitignore.
let filesToLint = execSync(validMDFilesCommand, {
stdio: ['ignore', 'pipe', 'inherit'],
});
// set all file(s) path as absolute path
filesToLint = filesToLint
.toString()
.split('\n')
.map(filepath => (filepath ? path.join(process.cwd(), filepath) : null))
.filter(Boolean);
// set all file(s) path as absolute path
filesToLint = filesToLint
.toString()
.split('\n')
.map(filepath =>
filepath ? path.join(process.cwd(), filepath) : null,
)
.filter(Boolean);
const output = spawnSync('vale', filesToLint, inheritStdIo);
const output = spawnSync('vale', filesToLint, inheritStdIo);
// if the command does not succeed
if (output.status !== 0) {
// if it contains system level error. [in this case vale does not exist]
if (output.error) {
console.error(ERROR_MESSAGE);
// if the command does not succeed
if (output.status !== 0) {
// if it contains system level error. [in this case vale does not exist]
if (output.error) {
console.error(ERROR_MESSAGE);
}
process.exit(1);
}
} catch (e) {
console.error(e.message);
process.exit(1);
}
} else {
const validMDFilesCommand = `${listFilesTrackedByGit} | ./node_modules/.bin/shx grep ".md"`;
// use xargs
try {
execSync(`${validMDFilesCommand} | xargs vale`, inheritStdIo);
} catch (e) {
console.error(ERROR_MESSAGE);
process.exit(1);
}
process.exit(1);
}
} catch (e) {
console.error(e.message);
process.exit(1);
}
} else {
const validMDFilesCommand = `${listFilesTrackedByGit} | ./node_modules/.bin/shx grep ".md"`;
// use xargs
try {
execSync(`${validMDFilesCommand} | xargs vale`, inheritStdIo);
} catch (e) {
console.error(ERROR_MESSAGE);
process.exit(1);
}
}
});