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.

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