gulpfile.js 3.4 KB

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