nodeVersionChecker.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Task, TaskRunner } from './task';
  2. import chalk from 'chalk';
  3. import { coerce, satisfies } from 'semver';
  4. import { readFileSync } from 'fs';
  5. interface FailedVersionCheck {
  6. file: string;
  7. line: string;
  8. }
  9. interface NodeVersionCheckerOptions {}
  10. const pattern = /(circleci\/|FROM )node\:([0-9]+(\.[0-9]+){0,2})/gm;
  11. const packageJsonFile = 'package.json';
  12. const failures: FailedVersionCheck[] = [];
  13. export const nodeVersionFiles = [packageJsonFile, 'Dockerfile', '.circleci/config.yml'];
  14. const nodeVersionCheckerRunner: TaskRunner<NodeVersionCheckerOptions> = async () => {
  15. // Read version from package json and treat that as the expected version in all other locations
  16. const packageJson = require(`${process.cwd()}/${packageJsonFile}`);
  17. const expectedVersion = packageJson.engines.node;
  18. console.log(chalk.yellow(`Specified node version in package.json is: ${expectedVersion}`));
  19. for (const file of nodeVersionFiles) {
  20. const fileContent = readFileSync(`${process.cwd()}/${file}`);
  21. const matches = fileContent.toString('utf8').match(pattern);
  22. if (!matches) {
  23. continue;
  24. }
  25. for (const match of matches) {
  26. const actualVersion = coerce(match);
  27. if (!actualVersion) {
  28. failures.push({
  29. file,
  30. line: match,
  31. });
  32. continue;
  33. }
  34. const satisfied = satisfies(actualVersion, expectedVersion);
  35. if (!satisfied) {
  36. failures.push({
  37. file,
  38. line: match,
  39. });
  40. }
  41. }
  42. }
  43. if (failures.length > 0) {
  44. console.log(chalk.red('--------------------------------------------------------------------'));
  45. console.log(chalk.red(`These entries don't satisfy the engine version in ${packageJsonFile}`));
  46. console.log(chalk.red('--------------------------------------------------------------------'));
  47. for (let index = 0; index < failures.length; index++) {
  48. const failure = failures[index];
  49. console.log(chalk.green(`\tIn ${failure.file} the line ${failure.line} does not satisfy ${expectedVersion}.`));
  50. }
  51. throw new Error('Node versions not in sync');
  52. }
  53. console.log(chalk.yellow('--------------------------------------------------------------------'));
  54. console.log(chalk.yellow('All node versions seem ok.'));
  55. console.log(chalk.yellow("Don't forget to sync https://github.com/grafana/grafana-build-container"));
  56. console.log(chalk.yellow(`also if you changed the engine version in ${packageJsonFile}`));
  57. console.log(chalk.yellow('--------------------------------------------------------------------'));
  58. };
  59. export const nodeVersionCheckerTask = new Task<NodeVersionCheckerOptions>(
  60. 'Node Version Checker',
  61. nodeVersionCheckerRunner
  62. );