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.

226 lines
6.3 KiB

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