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.

267 lines
6.8 KiB

  1. /*******************************
  2. Watch Task
  3. *******************************/
  4. let
  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-clean-css'),
  16. plumber = require('gulp-plumber'),
  17. print = require('gulp-print').default,
  18. rename = require('gulp-rename'),
  19. replace = require('gulp-replace'),
  20. uglify = require('gulp-uglify'),
  21. replaceExt = require('replace-ext'),
  22. // user config
  23. config = require('./config/user'),
  24. // task config
  25. tasks = require('./config/tasks'),
  26. install = require('./config/project/install'),
  27. // shorthand
  28. globs = config.globs,
  29. assets = config.paths.assets,
  30. output = config.paths.output,
  31. source = config.paths.source,
  32. banner = tasks.banner,
  33. comments = tasks.regExp.comments,
  34. log = tasks.log,
  35. settings = tasks.settings,
  36. {series, parallel} = gulp,
  37. watch,
  38. // individual watch tasks
  39. watchCSS,
  40. watchJS,
  41. watchAssets,
  42. watchCSSCallback,
  43. watchJSCallback,
  44. watchAssetsCallback
  45. ;
  46. // add tasks referenced using gulp.run (sub-tasks)
  47. if(config.rtl) {
  48. require('./collections/rtl')(gulp);
  49. }
  50. require('./collections/internal')(gulp);
  51. // export task
  52. watch = function(callback) {
  53. if( !install.isSetup() ) {
  54. console.error('Cannot watch files. Run "gulp install" to set-up Semantic');
  55. return;
  56. }
  57. // check for right-to-left (RTL) language
  58. if(config.rtl == 'both') {
  59. gulp.start('watch-rtl');
  60. }
  61. if(config.rtl === true || config.rtl === 'Yes') {
  62. gulp.start('watch-rtl');
  63. return;
  64. }
  65. //console.clear();
  66. console.log('Watching source files for changes');
  67. /*--------------
  68. Watch CSS
  69. ---------------*/
  70. watchCSS = gulp
  71. .watch([
  72. source.config,
  73. source.definitions + '/**/*.less',
  74. source.site + '/**/*.{overrides,variables}',
  75. source.themes + '/**/*.{overrides,variables}'
  76. ])
  77. ;
  78. watchCSSCallback = (filePath) => {
  79. let
  80. lessPath,
  81. stream,
  82. compressedStream,
  83. uncompressedStream,
  84. isDefinition,
  85. isPackagedTheme,
  86. isSiteTheme,
  87. isConfig
  88. ;
  89. // log modified file
  90. gulp.src(filePath)
  91. .pipe(print(log.modified))
  92. ;
  93. /*--------------
  94. Find Source
  95. ---------------*/
  96. // recompile on *.override , *.variable change
  97. isConfig = (filePath.indexOf('theme.config') !== -1 || filePath.indexOf('site.variables') !== -1);
  98. isPackagedTheme = (filePath.indexOf(source.themes) !== -1);
  99. isSiteTheme = (filePath.indexOf(source.site) !== -1);
  100. isDefinition = (filePath.indexOf(source.definitions) !== -1);
  101. if(isConfig) {
  102. console.info('Rebuilding all UI');
  103. // impossible to tell which file was updated in theme.config, rebuild all
  104. gulp.start('build-css');
  105. return;
  106. }
  107. else if(isPackagedTheme) {
  108. console.log('Change detected in packaged theme');
  109. lessPath = replaceExt(filePath, '.less');
  110. lessPath = lessPath.replace(tasks.regExp.theme, source.definitions);
  111. }
  112. else if(isSiteTheme) {
  113. console.log('Change detected in site theme');
  114. lessPath = replaceExt(filePath, '.less');
  115. lessPath = lessPath.replace(source.site, source.definitions);
  116. }
  117. else {
  118. console.log('Change detected in definition');
  119. lessPath = filePath;
  120. }
  121. /*--------------
  122. Create CSS
  123. ---------------*/
  124. if( fs.existsSync(lessPath) ) {
  125. // unified css stream
  126. stream = gulp.src(lessPath)
  127. .pipe(plumber(settings.plumber.less))
  128. .pipe(less(settings.less))
  129. .pipe(print(log.created))
  130. .pipe(replace(comments.variables.in, comments.variables.out))
  131. .pipe(replace(comments.license.in, comments.license.out))
  132. .pipe(replace(comments.large.in, comments.large.out))
  133. .pipe(replace(comments.small.in, comments.small.out))
  134. .pipe(replace(comments.tiny.in, comments.tiny.out))
  135. .pipe(autoprefixer(settings.prefix))
  136. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  137. ;
  138. // use 2 concurrent streams from same pipe
  139. uncompressedStream = stream.pipe(clone());
  140. compressedStream = stream.pipe(clone());
  141. uncompressedStream
  142. .pipe(plumber())
  143. .pipe(replace(assets.source, assets.uncompressed))
  144. .pipe(gulp.dest(output.uncompressed))
  145. .pipe(print(log.created))
  146. .on('end', function() {
  147. gulp.start('package uncompressed css');
  148. })
  149. ;
  150. compressedStream
  151. .pipe(plumber())
  152. .pipe(replace(assets.source, assets.compressed))
  153. .pipe(minifyCSS(settings.minify))
  154. .pipe(rename(settings.rename.minCSS))
  155. .pipe(gulp.dest(output.compressed))
  156. .pipe(print(log.created))
  157. .on('end', function() {
  158. gulp.start('package compressed css');
  159. })
  160. ;
  161. }
  162. else {
  163. console.log('Cannot find UI definition at path', lessPath);
  164. }
  165. };
  166. // these are separate handlers in gulp 4
  167. watchCSS
  168. .on('change', watchCSSCallback)
  169. .on('add', watchCSSCallback)
  170. ;
  171. /*--------------
  172. Watch JS
  173. ---------------*/
  174. watchJS = gulp
  175. .watch([
  176. source.definitions + '/**/*.js'
  177. ])
  178. ;
  179. watchJSCallback = (filePath) => {
  180. gulp.src(filePath)
  181. .pipe(plumber())
  182. .pipe(replace(comments.license.in, comments.license.out))
  183. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  184. .pipe(gulp.dest(output.uncompressed))
  185. .pipe(print(log.created))
  186. .pipe(uglify(settings.uglify))
  187. .pipe(rename(settings.rename.minJS))
  188. .pipe(gulp.dest(output.compressed))
  189. .pipe(print(log.created))
  190. .on('end', function() {
  191. gulp.start('package compressed js');
  192. gulp.start('package uncompressed js');
  193. })
  194. ;
  195. };
  196. watchJS
  197. .on('change', watchJSCallback)
  198. .on('add', watchJSCallback)
  199. ;
  200. /*--------------
  201. Watch Assets
  202. ---------------*/
  203. // only copy assets that match component names (or their plural)
  204. watchAssets = gulp
  205. .watch([
  206. source.themes + '/**/assets/**/*.*'
  207. ])
  208. ;
  209. watchAssetsCallback = (filePath) => {
  210. // copy assets
  211. gulp.src(filePath, { base: source.themes })
  212. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  213. .pipe(gulp.dest(output.themes))
  214. .pipe(print(log.created))
  215. ;
  216. };
  217. watchAssets
  218. .on('change', watchAssetsCallback)
  219. .on('add', watchAssetsCallback)
  220. ;
  221. };
  222. /* Export with Metadata */
  223. watch.displayName = 'watch';
  224. watch.description = 'Watch for site/theme changes';
  225. module.exports = series(watch);