[dev/precommitHook] add support for --fix flag (#36717)

* [dev/precommitHook] add support for --fix flag

* describe new --fix flag in precommit error text
This commit is contained in:
Spencer 2019-05-20 14:23:15 -07:00 committed by GitHub
parent 3cf9a49820
commit e091de9e1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 23 deletions

View file

@ -56,7 +56,6 @@
"start": "node --trace-warnings --trace-deprecation scripts/kibana --dev ",
"debug": "node --nolazy --inspect scripts/kibana --dev",
"debug-break": "node --nolazy --inspect-brk scripts/kibana --dev",
"precommit": "node scripts/precommit_hook",
"karma": "karma start",
"lint": "yarn run lint:es && yarn run lint:sass",
"lint:es": "node scripts/eslint",

View file

@ -30,15 +30,20 @@ import { REPO_ROOT } from '../constants';
* @param {Array<File>} files
* @return {undefined}
*/
export function lintFiles(log, files) {
export function lintFiles(log, files, { fix } = {}) {
const cli = new CLIEngine({
cache: true,
cwd: REPO_ROOT,
fix
});
const paths = files.map(file => file.getRelativePath());
const report = cli.executeOnFiles(paths);
if (fix) {
CLIEngine.outputFixes(report);
}
const failTypes = [];
if (report.errorCount > 0) failTypes.push('errors');
if (report.warningCount > 0) failTypes.push('warning');

View file

@ -48,58 +48,58 @@ function getKbnPrecommitGitHookScript(rootPath, nodeHome, platform) {
#
# ** THIS IS AN AUTO-GENERATED FILE **
# ** PLEASE DO NOT CHANGE IT MANUALLY **
#
#
# GENERATED BY ${__dirname}
# IF YOU WANNA CHANGE SOMETHING INTO THIS SCRIPT
# PLEASE RE-RUN 'yarn kbn bootstrap' or 'node scripts/register_git_hook' IN THE ROOT
# OF THE CURRENT PROJECT ${rootPath}
set -euo pipefail
# Export Git hook params
export GIT_PARAMS="$*"
has_node() {
command -v node >/dev/null 2>&1
}
has_nvm() {
command -v nvm >/dev/null 2>&1
}
try_load_node_from_nvm_paths () {
# If nvm is not loaded, load it
has_node || {
NVM_SH="${nodeHome}/.nvm/nvm.sh"
if [ "${platform}" == "darwin" ] && [ -s "$(brew --prefix nvm)/nvm.sh" ]; then
NVM_SH="$(brew --prefix nvm)/nvm.sh"
fi
export NVM_DIR=${nodeHome}/.nvm
[ -s "$NVM_SH" ] && \. "$NVM_SH"
# If nvm has been loaded correctly, use project .nvmrc
has_nvm && nvm use
}
}
extend_user_path() {
if [ "${platform}" == "win32" ]; then
export PATH="$PATH:/c/Program Files/nodejs"
else
else
export PATH="$PATH:/usr/local/bin:/usr/local"
try_load_node_from_nvm_paths
fi
}
# Extend path with common path locations for node
# in order to make the hook working on git GUI apps
extend_user_path
# Check if we have node js bin in path
has_node || {
has_node || {
echo "Can't found node bin in the PATH. Please update the PATH to proceed."
echo "If your PATH already has the node bin, maybe you are using some git GUI app."
echo "Can't found node bin in the PATH. Please update the PATH to proceed."
@ -107,12 +107,16 @@ function getKbnPrecommitGitHookScript(rootPath, nodeHome, platform) {
echo "In order to proceed, you need to config the PATH used by the application that are launching your git GUI app."
echo "If you are running macOS, you can do that using:"
echo "'sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'"
exit 1
}
npm run --silent precommit || { echo "Pre-commit hook failed (add --no-verify to bypass)"; exit 1; }
node scripts/precommit_hook || {
echo "Pre-commit hook failed (add --no-verify to bypass)";
echo ' For eslint failures you can try running \`node scripts/precommit_hook --fix\`';
exit 1;
}
exit 0
`);
}

View file

@ -22,7 +22,7 @@ import * as Eslint from './eslint';
import * as Sasslint from './sasslint';
import { getFilesForCommit, checkFileCasing } from './precommit_hook';
run(async ({ log }) => {
run(async ({ log, flags }) => {
const files = await getFilesForCommit();
const errors = [];
@ -36,7 +36,9 @@ run(async ({ log }) => {
const filesToLint = Linter.pickFilesToLint(log, files);
if (filesToLint.length > 0) {
try {
await Linter.lintFiles(log, filesToLint);
await Linter.lintFiles(log, filesToLint, {
fix: flags.fix
});
} catch (error) {
errors.push(error);
}
@ -46,4 +48,17 @@ run(async ({ log }) => {
if (errors.length) {
throw combineErrors(errors);
}
}, {
description: `
Run checks on files that are staged for commit
`,
flags: {
boolean: ['fix'],
default: {
fix: false
},
help: `
--fix Execute eslint in --fix mode
`
},
});