precommit.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Task, TaskRunner } from './task';
  2. import chalk from 'chalk';
  3. import get from 'lodash/get';
  4. import flatten from 'lodash/flatten';
  5. import execa = require('execa');
  6. const simpleGit = require('simple-git/promise')(process.cwd());
  7. interface PrecommitOptions {}
  8. const tasks = {
  9. lint: {
  10. sass: ['newer:sasslint'],
  11. core: ['newer:exec:tslintRoot'],
  12. gui: ['newer:exec:tslintPackages'],
  13. },
  14. typecheck: {
  15. core: ['newer:exec:typecheckRoot'],
  16. gui: ['newer:exec:typecheckPackages'],
  17. },
  18. test: {
  19. lint: {
  20. ts: ['no-only-tests'],
  21. go: ['no-focus-convey-tests'],
  22. },
  23. },
  24. };
  25. const precommitRunner: TaskRunner<PrecommitOptions> = async () => {
  26. const status = await simpleGit.status();
  27. const sassFiles = status.files.filter(
  28. file => (file.path as string).match(/^[a-zA-Z0-9\_\-\/]+(\.scss)$/g) || file.path.indexOf('.sass-lint.yml') > -1
  29. );
  30. const tsFiles = status.files.filter(file => (file.path as string).match(/^[a-zA-Z0-9\_\-\/]+(\.(ts|tsx))$/g));
  31. const testFiles = status.files.filter(file => (file.path as string).match(/^[a-zA-Z0-9\_\-\/]+(\.test.(ts|tsx))$/g));
  32. const goTestFiles = status.files.filter(file => (file.path as string).match(/^[a-zA-Z0-9\_\-\/]+(\_test.go)$/g));
  33. const grafanaUiFiles = tsFiles.filter(file => (file.path as string).indexOf('grafana-ui') > -1);
  34. const grafanaUIFilesChangedOnly = tsFiles.length > 0 && tsFiles.length - grafanaUiFiles.length === 0;
  35. const coreFilesChangedOnly = tsFiles.length > 0 && grafanaUiFiles.length === 0;
  36. const taskPaths = [];
  37. if (sassFiles.length > 0) {
  38. taskPaths.push('lint.sass');
  39. }
  40. if (testFiles.length) {
  41. taskPaths.push('test.lint.ts');
  42. }
  43. if (goTestFiles.length) {
  44. taskPaths.push('test.lint.go');
  45. }
  46. if (tsFiles.length > 0) {
  47. if (grafanaUIFilesChangedOnly) {
  48. taskPaths.push('lint.gui', 'typecheck.core', 'typecheck.gui');
  49. } else if (coreFilesChangedOnly) {
  50. taskPaths.push('lint.core', 'typecheck.core');
  51. } else {
  52. taskPaths.push('lint.core', 'lint.gui', 'typecheck.core', 'typecheck.gui');
  53. }
  54. }
  55. const gruntTasks = flatten(taskPaths.map(path => get(tasks, path)));
  56. if (gruntTasks.length > 0) {
  57. console.log(chalk.yellow(`Precommit checks: ${taskPaths.join(', ')}`));
  58. const task = execa('grunt', gruntTasks);
  59. // @ts-ignore
  60. const stream = task.stdout;
  61. stream.pipe(process.stdout);
  62. return task;
  63. }
  64. console.log(chalk.yellow('Skipping precommit checks, not front-end changes detected'));
  65. return;
  66. };
  67. export const precommitTask = new Task<PrecommitOptions>();
  68. precommitTask.setName('Precommit task');
  69. precommitTask.setRunner(precommitRunner);