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.

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