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.

219 lines
6.1 KiB

  1. /*******************************
  2. Watch Task
  3. *******************************/
  4. var
  5. gulp = require('gulp'),
  6. // node dependencies
  7. console = require('better-console'),
  8. fs = require('fs'),
  9. // gulp dependencies
  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. 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/project/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 tasks that shouldn't be exposed to end-user
  40. require('./collections/internal')(gulp);
  41. // export task
  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('theme.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. // impossible to tell which file was updated in theme.config, rebuild all
  84. gulp.start('build');
  85. }
  86. else if(isPackagedTheme) {
  87. console.log('Change detected in packaged theme');
  88. lessPath = util.replaceExtension(file.path, '.less');
  89. lessPath = lessPath.replace(tasks.regExp.theme, source.definitions);
  90. }
  91. else if(isSiteTheme) {
  92. console.log('Change detected in site theme');
  93. lessPath = util.replaceExtension(file.path, '.less');
  94. lessPath = lessPath.replace(source.site, source.definitions);
  95. }
  96. else if(isDefinition) {
  97. console.log('Change detected in definition');
  98. lessPath = file.path;
  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. ;
  115. // use 2 concurrent streams from same pipe
  116. uncompressedStream = stream.pipe(clone());
  117. compressedStream = stream.pipe(clone());
  118. uncompressedStream
  119. .pipe(plumber())
  120. .pipe(replace(assets.source, assets.uncompressed))
  121. .pipe(header(banner, settings.header))
  122. .pipe(gulp.dest(output.uncompressed))
  123. .pipe(print(log.created))
  124. .on('end', function() {
  125. gulp.start('package uncompressed css');
  126. })
  127. ;
  128. compressedStream = stream
  129. .pipe(plumber())
  130. .pipe(replace(assets.source, assets.compressed))
  131. .pipe(minifyCSS(settings.minify))
  132. .pipe(rename(settings.rename.minCSS))
  133. .pipe(header(banner, settings.header))
  134. .pipe(gulp.dest(output.compressed))
  135. .pipe(print(log.created))
  136. .on('end', function() {
  137. gulp.start('package compressed css');
  138. })
  139. ;
  140. }
  141. else {
  142. console.log('Cannot find UI definition at path', lessPath);
  143. }
  144. })
  145. ;
  146. /*--------------
  147. Watch JS
  148. ---------------*/
  149. gulp
  150. .watch([
  151. source.definitions + '**/*.js'
  152. ], function(file) {
  153. gulp.src(file.path)
  154. .pipe(plumber())
  155. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  156. .pipe(gulp.dest(output.uncompressed))
  157. .pipe(print(log.created))
  158. .pipe(uglify(settings.uglify))
  159. .pipe(rename(settings.rename.minJS))
  160. .pipe(gulp.dest(output.compressed))
  161. .pipe(print(log.created))
  162. .on('end', function() {
  163. gulp.start('package compressed js');
  164. gulp.start('package uncompressed js');
  165. })
  166. ;
  167. })
  168. ;
  169. /*--------------
  170. Watch Assets
  171. ---------------*/
  172. // only copy assets that match component names (or their plural)
  173. gulp
  174. .watch([
  175. source.themes + '**/assets/**/' + globs.components + '?(s).*'
  176. ], function(file) {
  177. // copy assets
  178. gulp.src(file.path, { base: source.themes })
  179. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  180. .pipe(gulp.dest(output.themes))
  181. .pipe(print(log.created))
  182. ;
  183. })
  184. ;
  185. };