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
+2 -1
View File
@@ -41,6 +41,7 @@
"@changesets/cli": "^2.11.0",
"@spotify/eslint-config-oss": "^1.0.1",
"@spotify/prettier-config": "^8.0.0",
"command-exists": "^1.2.9",
"concurrently": "^5.2.0",
"fs-extra": "^9.0.0",
"husky": "^4.2.3",
@@ -65,7 +66,7 @@
"prettier --write"
],
"*.md": [
"vale"
"node ./scripts/check-docs-quality"
]
},
"jest": {
+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);
}
}
});
+5
View File
@@ -8537,6 +8537,11 @@ command-exists-promise@^2.0.2:
resolved "https://registry.npmjs.org/command-exists-promise/-/command-exists-promise-2.0.2.tgz#7beecc4b218299f3c61fa69a4047aa0b36a64a99"
integrity sha512-T6PB6vdFrwnHXg/I0kivM3DqaCGZLjjYSOe0a5WgFKcz1sOnmOeIjnhQPXVXX3QjVbLyTJ85lJkX6lUpukTzaA==
command-exists@^1.2.9:
version "1.2.9"
resolved "https://registry.npmjs.org/command-exists/-/command-exists-1.2.9.tgz#c50725af3808c8ab0260fd60b01fbfa25b954f69"
integrity sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w==
commander@^2.11.0, commander@^2.19.0, commander@^2.20.0, commander@^2.20.3, commander@~2.20.3:
version "2.20.3"
resolved "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"