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.

230 lines
6.4 KiB

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