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('.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.log('Change detected in theme config');
  99. gulp.start('build-docs');
  100. }
  101. else if(isPackagedTheme) {
  102. console.log('Change detected in packaged theme');
  103. lessPath = lessPath.replace(tasks.regExp.theme, source.definitions);
  104. lessPath = util.replaceExtension(file.path, '.less');
  105. }
  106. else if(isSiteTheme) {
  107. console.log('Change detected in site theme');
  108. lessPath = lessPath.replace(source.site, source.definitions);
  109. lessPath = util.replaceExtension(file.path, '.less');
  110. }
  111. else if(isDefinition) {
  112. console.log('Change detected in definition');
  113. lessPath = util.replaceExtension(file.path, '.less');
  114. }
  115. /*--------------
  116. Create CSS
  117. ---------------*/
  118. if( fs.existsSync(lessPath) ) {
  119. // unified css stream
  120. stream = gulp.src(lessPath)
  121. .pipe(plumber())
  122. .pipe(less(settings.less))
  123. .pipe(replace(comments.variables.in, comments.variables.out))
  124. .pipe(replace(comments.large.in, comments.large.out))
  125. .pipe(replace(comments.small.in, comments.small.out))
  126. .pipe(replace(comments.tiny.in, comments.tiny.out))
  127. .pipe(autoprefixer(settings.prefix))
  128. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  129. ;
  130. // use 2 concurrent streams from same pipe
  131. uncompressedStream = stream.pipe(clone());
  132. compressedStream = stream.pipe(clone());
  133. uncompressedStream
  134. .pipe(plumber())
  135. .pipe(replace(assets.source, assets.uncompressed))
  136. .pipe(header(banner, settings.header))
  137. .pipe(gulp.dest(output.uncompressed))
  138. .pipe(print(log.created))
  139. .on('end', function() {
  140. gulp.start('package uncompressed docs css');
  141. })
  142. ;
  143. compressedStream = stream
  144. .pipe(plumber())
  145. .pipe(replace(assets.source, assets.compressed))
  146. .pipe(minifyCSS(settings.minify))
  147. .pipe(rename(settings.rename.minCSS))
  148. .pipe(header(banner, settings.header))
  149. .pipe(gulp.dest(output.compressed))
  150. .pipe(print(log.created))
  151. .on('end', function() {
  152. gulp.start('package compressed docs css');
  153. })
  154. ;
  155. }
  156. else {
  157. console.log('Cannot find UI definition at path', lessPath);
  158. }
  159. })
  160. ;
  161. /*--------------
  162. Watch JS
  163. ---------------*/
  164. gulp
  165. .watch([
  166. source.definitions + '/**/*.js'
  167. ], function(file) {
  168. gulp.src(file.path)
  169. .pipe(plumber())
  170. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  171. .pipe(gulp.dest(output.uncompressed))
  172. .pipe(print(log.created))
  173. .pipe(uglify(settings.uglify))
  174. .pipe(rename(settings.rename.minJS))
  175. .pipe(gulp.dest(output.compressed))
  176. .pipe(print(log.created))
  177. .on('end', function() {
  178. gulp.start('package compressed docs js');
  179. gulp.start('package uncompressed docs js');
  180. })
  181. ;
  182. })
  183. ;
  184. /*--------------
  185. Watch Assets
  186. ---------------*/
  187. // only copy assets that match component names (or their plural)
  188. gulp
  189. .watch([
  190. source.themes + '/**/assets/**/' + globs.components + '?(s).*'
  191. ], function(file) {
  192. // copy assets
  193. gulp.src(file.path, { base: source.themes })
  194. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  195. .pipe(gulp.dest(output.themes))
  196. .pipe(print(log.created))
  197. ;
  198. })
  199. ;
  200. };