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.

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