You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.4 KiB

  1. /*******************************
  2. Watch Task
  3. *******************************/
  4. let
  5. gulp = require('gulp'),
  6. // node deps
  7. console = require('better-console'),
  8. fs = require('fs'),
  9. // gulp deps
  10. autoprefixer = require('gulp-autoprefixer'),
  11. chmod = require('gulp-chmod'),
  12. clone = require('gulp-clone'),
  13. gulpif = require('gulp-if'),
  14. less = require('gulp-less'),
  15. minifyCSS = require('gulp-clean-css'),
  16. plumber = require('gulp-plumber'),
  17. print = require('gulp-print').default,
  18. rename = require('gulp-rename'),
  19. replace = require('gulp-replace'),
  20. rtlcss = require('gulp-rtlcss'),
  21. uglify = require('gulp-uglify'),
  22. replaceExt = require('replace-ext'),
  23. // user config
  24. config = require('../config/user'),
  25. // task config
  26. tasks = require('../config/tasks'),
  27. install = require('../config/project/install'),
  28. // shorthand
  29. globs = config.globs,
  30. assets = config.paths.assets,
  31. output = config.paths.output,
  32. source = config.paths.source,
  33. banner = tasks.banner,
  34. comments = tasks.regExp.comments,
  35. log = tasks.log,
  36. settings = tasks.settings,
  37. watchRTL,
  38. // individual watch tasks
  39. watchCSS,
  40. watchJS,
  41. watchAssets,
  42. watchCSSCallback,
  43. watchJSCallback,
  44. watchAssetsCallback
  45. ;
  46. // add internal tasks (concat release)
  47. require('../collections/internal')(gulp);
  48. watchRTL = function(callback) {
  49. if( !install.isSetup() ) {
  50. console.error('Cannot watch files. Run "gulp install" to set-up Semantic');
  51. return;
  52. }
  53. console.clear();
  54. console.log('Watching source files for changes');
  55. /*--------------
  56. Watch CSS
  57. ---------------*/
  58. gulp
  59. .watch([
  60. source.config,
  61. source.definitions + '/**/*.less',
  62. source.site + '/**/*.{overrides,variables}',
  63. source.themes + '/**/*.{overrides,variables}'
  64. ], function(file) {
  65. let
  66. lessPath,
  67. stream,
  68. compressedStream,
  69. uncompressedStream,
  70. isDefinition,
  71. isPackagedTheme,
  72. isSiteTheme,
  73. isConfig
  74. ;
  75. // log modified file
  76. gulp.src(file.path)
  77. .pipe(print(log.modified))
  78. ;
  79. /*--------------
  80. Find Source
  81. ---------------*/
  82. // recompile on *.override , *.variable change
  83. isConfig = (file.path.indexOf('.config') !== -1);
  84. isPackagedTheme = (file.path.indexOf(source.themes) !== -1);
  85. isSiteTheme = (file.path.indexOf(source.site) !== -1);
  86. isDefinition = (file.path.indexOf(source.definitions) !== -1);
  87. if(isConfig) {
  88. console.log('Change detected in theme config');
  89. // cant tell which theme was changed in theme.config, rebuild all
  90. gulp.start('build');
  91. }
  92. else if(isPackagedTheme) {
  93. console.log('Change detected in packaged theme');
  94. lessPath = lessPath.replace(tasks.regExp.theme, source.definitions);
  95. lessPath = replaceExt(file.path, '.less');
  96. }
  97. else if(isSiteTheme) {
  98. console.log('Change detected in site theme');
  99. lessPath = lessPath.replace(source.site, source.definitions);
  100. lessPath = replaceExt(file.path, '.less');
  101. }
  102. else if(isDefinition) {
  103. console.log('Change detected in definition');
  104. lessPath = replaceExt(file.path, '.less');
  105. }
  106. /*--------------
  107. Create CSS
  108. ---------------*/
  109. if( fs.existsSync(lessPath) ) {
  110. // unified css stream
  111. stream = gulp.src(lessPath)
  112. .pipe(plumber())
  113. .pipe(less(settings.less))
  114. .pipe(replace(comments.variables.in, comments.variables.out))
  115. .pipe(replace(comments.license.in, comments.license.out))
  116. .pipe(replace(comments.large.in, comments.large.out))
  117. .pipe(replace(comments.small.in, comments.small.out))
  118. .pipe(replace(comments.tiny.in, comments.tiny.out))
  119. .pipe(autoprefixer(settings.prefix))
  120. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  121. .pipe(rtlcss())
  122. ;
  123. // use 2 concurrent streams from same pipe
  124. uncompressedStream = stream.pipe(clone());
  125. compressedStream = stream.pipe(clone());
  126. uncompressedStream
  127. .pipe(plumber())
  128. .pipe(replace(assets.source, assets.uncompressed))
  129. .pipe(rename(settings.rename.rtlCSS))
  130. .pipe(gulp.dest(output.uncompressed))
  131. .pipe(print(log.created))
  132. .on('end', function() {
  133. gulp.start('package uncompressed rtl css');
  134. })
  135. ;
  136. compressedStream
  137. .pipe(plumber())
  138. .pipe(replace(assets.source, assets.compressed))
  139. .pipe(minifyCSS(settings.minify))
  140. .pipe(rename(settings.rename.minCSS))
  141. .pipe(rename(settings.rename.rtlMinCSS))
  142. .pipe(gulp.dest(output.compressed))
  143. .pipe(print(log.created))
  144. .on('end', function() {
  145. gulp.start('package compressed rtl css');
  146. })
  147. ;
  148. }
  149. else {
  150. console.log('Cannot find UI definition at path', lessPath);
  151. }
  152. })
  153. ;
  154. /*--------------
  155. Watch JS
  156. ---------------*/
  157. gulp
  158. .watch([
  159. source.definitions + '/**/*.js'
  160. ], function(file) {
  161. gulp.src(file.path)
  162. .pipe(plumber())
  163. .pipe(replace(comments.license.in, comments.license.out))
  164. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  165. .pipe(gulp.dest(output.uncompressed))
  166. .pipe(print(log.created))
  167. .pipe(uglify(settings.uglify))
  168. .pipe(rename(settings.rename.minJS))
  169. .pipe(gulp.dest(output.compressed))
  170. .pipe(print(log.created))
  171. .on('end', function() {
  172. gulp.start('package compressed js');
  173. gulp.start('package uncompressed js');
  174. })
  175. ;
  176. })
  177. ;
  178. /*--------------
  179. Watch Assets
  180. ---------------*/
  181. // only copy assets that match component names (or their plural)
  182. gulp
  183. .watch([
  184. source.themes + '/**/assets/**/' + globs.components + '?(s).*'
  185. ], function(file) {
  186. // copy assets
  187. gulp.src(file.path, { base: source.themes })
  188. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  189. .pipe(gulp.dest(output.themes))
  190. .pipe(print(log.created))
  191. ;
  192. })
  193. ;
  194. };
  195. /* Export with Metadata */
  196. watchRTL.displayName = 'watch-rtl';
  197. watchRTL.description = 'Watch files as RTL';
  198. module.exports = watchRTL;