gulpfile.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. const gulp = require('gulp');
  3. const format = require('gulp-clang-format');
  4. const clangFormat = require('clang-format');
  5. const spawn = require('child_process').spawn;
  6. const tslint = require('gulp-tslint');
  7. const fs = require('fs');
  8. const path = require('path');
  9. const semver = require('semver');
  10. const runSpawn = (done, task, opt_arg, opt_io) => {
  11. opt_arg = typeof opt_arg !== 'undefined' ? opt_arg : [];
  12. const stdio = 'inherit';
  13. if (opt_io === 'ignore') {
  14. stdio = 'ignore';
  15. }
  16. const child = spawn(task, opt_arg, {stdio: stdio});
  17. let running = false;
  18. child.on('close', () => {
  19. if (!running) {
  20. running = true;
  21. done();
  22. }
  23. });
  24. child.on('error', () => {
  25. if (!running) {
  26. console.error('gulp encountered a child error');
  27. running = true;
  28. done();
  29. }
  30. });
  31. };
  32. gulp.task('tslint', () => {
  33. return gulp.src(['lib/**/*.ts', 'spec/**/*.ts', '!spec/install/**/*.ts'])
  34. .pipe(tslint())
  35. .pipe(tslint.report());
  36. });
  37. gulp.task('format:enforce', () => {
  38. return gulp.src(['lib/**/*.ts'])
  39. .pipe(format.checkFormat('file', clangFormat,
  40. {verbose: true, fail: true}));
  41. });
  42. gulp.task('lint', gulp.series('tslint', 'format:enforce'));
  43. // prevent contributors from using the wrong version of node
  44. gulp.task('checkVersion', (done) => {
  45. // read minimum node on package.json
  46. const packageJson = JSON.parse(fs.readFileSync(path.resolve('package.json')));
  47. const protractorVersion = packageJson.version;
  48. const nodeVersion = packageJson.engines.node;
  49. if (semver.satisfies(process.version, nodeVersion)) {
  50. done();
  51. } else {
  52. throw new Error('minimum node version for Protractor ' +
  53. protractorVersion + ' is node ' + nodeVersion);
  54. }
  55. });
  56. gulp.task('built:copy', () => {
  57. return gulp.src(['lib/**/*.js'])
  58. .pipe(gulp.dest('built/'));
  59. });
  60. gulp.task('built:copy:typings', () => {
  61. return gulp.src(['lib/selenium-webdriver/**/*.d.ts'])
  62. .pipe(gulp.dest('built/selenium-webdriver/'));
  63. });
  64. gulp.task('webdriver:update', (done) => {
  65. runSpawn(done, 'node', ['bin/webdriver-manager', 'update',
  66. '--versions.chrome=2.44']);
  67. });
  68. gulp.task('format', () => {
  69. return gulp.src(['lib/**/*.ts'], { base: '.' })
  70. .pipe(format.format('file', clangFormat))
  71. .pipe(gulp.dest('.'));
  72. });
  73. gulp.task('tsc', (done) => {
  74. runSpawn(done, 'node', ['node_modules/typescript/bin/tsc']);
  75. });
  76. gulp.task('tsc:spec', (done) => {
  77. runSpawn(done, 'node', ['node_modules/typescript/bin/tsc', '-p', 'ts_spec_config.json']);
  78. });
  79. gulp.task('prepublish', gulp.series('checkVersion', 'tsc', 'built:copy'));
  80. gulp.task('pretest', gulp.series(
  81. 'checkVersion',
  82. gulp.parallel('webdriver:update', 'tslint', 'format'),
  83. 'tsc', 'built:copy', 'built:copy:typings', 'tsc:spec'));
  84. gulp.task('default', gulp.series('prepublish'));