remove-use-strict.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const gulp = require( 'gulp' );
  7. const path = require( 'path' );
  8. const replace = require( 'gulp-replace' );
  9. const mergeStream = require( 'merge-stream' );
  10. const filterGitignore = require( '../utils/filtergitignore' );
  11. /**
  12. * Removes lines with `'use strict';` directive.
  13. *
  14. * Example:
  15. *
  16. * gulp exec --task remove-use-strict
  17. *
  18. * @param {String} workdir
  19. * @returns {Stream}
  20. */
  21. module.exports = function executeRemoveUseStrict( workdir ) {
  22. return mergeStream(
  23. updateJshintrc( workdir ),
  24. removeUseStrict( workdir )
  25. );
  26. };
  27. // Updates .jshintrc file's `strict` option with `implied` value.
  28. //
  29. // @param {String} workdir Path of directory to be processed.
  30. // @returns {Stream}
  31. function updateJshintrc( workdir ) {
  32. const jshintrcPath = path.join( workdir, '.jshintrc' );
  33. const strictRegex = /("strict":.*?").*?(".*)/;
  34. const replaceWith = 'implied';
  35. return gulp.src( jshintrcPath )
  36. .pipe( replace(
  37. strictRegex,
  38. `$1${ replaceWith }$2`
  39. ) )
  40. .pipe( gulp.dest( workdir ) );
  41. }
  42. // Removes `'use strict';` directive from project's source files. Omits files listed in `.gitignore`.
  43. //
  44. // @param {String} workdir Path of directory to be processed.
  45. // @returns {Stream}
  46. function removeUseStrict( workdir ) {
  47. const glob = path.join( workdir, '**/*' );
  48. const useStrictRegex = /^\s*'use strict';\s*$/gm;
  49. return gulp.src( glob )
  50. .pipe( filterGitignore() )
  51. .pipe( replace(
  52. useStrictRegex,
  53. '',
  54. { skipBinary: true }
  55. ) )
  56. .pipe( gulp.dest( workdir ) );
  57. }