Skip to content

Commit

Permalink
[7.x] [dev/precommitHook] add support for --fix flag (#36717) (#36724)
Browse files Browse the repository at this point in the history
* [dev/precommitHook] add support for --fix flag

* describe new --fix flag in precommit error text
  • Loading branch information
Spencer committed May 21, 2019
1 parent 120cc65 commit 40f94f6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion src/dev/eslint/lint_files.js
Expand Up @@ -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');
Expand Down
42 changes: 23 additions & 19 deletions src/dev/register_git_hook/register_git_hook.js
Expand Up @@ -48,71 +48,75 @@ 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."
echo "If your PATH already has the node bin, maybe you are using some git GUI app not launched from the shell."
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
`);
}
Expand Down
19 changes: 17 additions & 2 deletions src/dev/run_precommit_hook.js
Expand Up @@ -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 = [];

Expand All @@ -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);
}
Expand All @@ -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
`
},
});

0 comments on commit 40f94f6

Please sign in to comment.