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.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(source.definitions);
  49. console.log('Watching source files for changes');
  50. /*--------------
  51. Watch CSS
  52. ---------------*/
  53. gulp
  54. .watch([
  55. source.config,
  56. source.definitions + '**/*.less',
  57. source.site + '**/*.{overrides,variables}',
  58. source.themes + '**/*.{overrides,variables}'
  59. ], function(file) {
  60. var
  61. lessPath,
  62. stream,
  63. compressedStream,
  64. uncompressedStream,
  65. isDefinition,
  66. isPackagedTheme,
  67. isSiteTheme,
  68. isConfig
  69. ;
  70. // log modified file
  71. gulp.src(file.path)
  72. .pipe(print(log.modified))
  73. ;
  74. /*--------------
  75. Find Source
  76. ---------------*/
  77. // recompile on *.override , *.variable change
  78. isConfig = (file.path.indexOf('theme.config') !== -1);
  79. isPackagedTheme = (file.path.indexOf(source.themes) !== -1);
  80. isSiteTheme = (file.path.indexOf(source.site) !== -1);
  81. isDefinition = (file.path.indexOf(source.definitions) !== -1);
  82. if(isConfig) {
  83. console.log('Change detected in theme config');
  84. // impossible to tell which file was updated in theme.config, rebuild all
  85. gulp.start('build');
  86. }
  87. else if(isPackagedTheme) {
  88. console.log('Change detected in packaged theme');
  89. lessPath = util.replaceExtension(file.path, '.less');
  90. lessPath = lessPath.replace(tasks.regExp.theme, source.definitions);
  91. }
  92. else if(isSiteTheme) {
  93. console.log('Change detected in site theme');
  94. lessPath = util.replaceExtension(file.path, '.less');
  95. lessPath = lessPath.replace(source.site, source.definitions);
  96. }
  97. else if(isDefinition) {
  98. console.log('Change detected in definition');
  99. lessPath = file.path;
  100. }
  101. /*--------------
  102. Create CSS
  103. ---------------*/
  104. if( fs.existsSync(lessPath) ) {
  105. // unified css stream
  106. stream = gulp.src(lessPath)
  107. .pipe(plumber())
  108. .pipe(less(settings.less))
  109. .pipe(replace(comments.variables.in, comments.variables.out))
  110. .pipe(replace(comments.large.in, comments.large.out))
  111. .pipe(replace(comments.small.in, comments.small.out))
  112. .pipe(replace(comments.tiny.in, comments.tiny.out))
  113. .pipe(autoprefixer(settings.prefix))
  114. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  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(gulp.dest(output.uncompressed))
  124. .pipe(print(log.created))
  125. .on('end', function() {
  126. gulp.start('package uncompressed 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(header(banner, settings.header))
  135. .pipe(gulp.dest(output.compressed))
  136. .pipe(print(log.created))
  137. .on('end', function() {
  138. gulp.start('package compressed 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(gulpif(config.hasPermission, chmod(config.permission)))
  157. .pipe(gulp.dest(output.uncompressed))
  158. .pipe(print(log.created))
  159. .pipe(uglify(settings.uglify))
  160. .pipe(rename(settings.rename.minJS))
  161. .pipe(gulp.dest(output.compressed))
  162. .pipe(print(log.created))
  163. .on('end', function() {
  164. gulp.start('package compressed js');
  165. gulp.start('package uncompressed js');
  166. })
  167. ;
  168. })
  169. ;
  170. /*--------------
  171. Watch Assets
  172. ---------------*/
  173. // only copy assets that match component names (or their plural)
  174. gulp
  175. .watch([
  176. source.themes + '**/assets/**/' + globs.components + '?(s).*'
  177. ], function(file) {
  178. // copy assets
  179. gulp.src(file.path, { base: source.themes })
  180. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  181. .pipe(gulp.dest(output.themes))
  182. .pipe(print(log.created))
  183. ;
  184. })
  185. ;
  186. };