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.

220 lines
6.2 KiB

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