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.

235 lines
6.3 KiB

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