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.

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