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.

255 lines
6.6 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-clean-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. tasks = require('../config/tasks'),
  28. configSetup = require('../config/project/config'),
  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. Copy Examples
  67. ---------------*/
  68. gulp
  69. .watch([
  70. 'examples/**/*.*'
  71. ], function(file) {
  72. console.clear();
  73. return gulp.src(file.path, {
  74. base: 'examples/'
  75. })
  76. .pipe(gulp.dest(output.examples))
  77. .pipe(print(log.created))
  78. ;
  79. })
  80. ;
  81. /*--------------
  82. Watch CSS
  83. ---------------*/
  84. gulp
  85. .watch([
  86. source.config,
  87. source.definitions + '/**/*.less',
  88. source.site + '/**/*.{overrides,variables}',
  89. source.themes + '/**/*.{overrides,variables}'
  90. ], function(file) {
  91. var
  92. lessPath,
  93. stream,
  94. compressedStream,
  95. uncompressedStream,
  96. isDefinition,
  97. isPackagedTheme,
  98. isSiteTheme,
  99. isConfig
  100. ;
  101. // log modified file
  102. gulp.src(file.path)
  103. .pipe(print(log.modified))
  104. ;
  105. /*--------------
  106. Find Source
  107. ---------------*/
  108. // recompile on *.override , *.variable change
  109. isConfig = (file.path.indexOf('theme.config') !== -1 || file.path.indexOf('site.variables') !== -1);
  110. isPackagedTheme = (file.path.indexOf(source.themes) !== -1);
  111. isSiteTheme = (file.path.indexOf(source.site) !== -1);
  112. isDefinition = (file.path.indexOf(source.definitions) !== -1);
  113. if(isConfig) {
  114. // console.info('Rebuilding all files');
  115. // cant rebuild paths are wrong
  116. // gulp.start('build-docs');
  117. return;
  118. }
  119. else if(isPackagedTheme) {
  120. console.log('Change detected in packaged theme');
  121. lessPath = util.replaceExtension(file.path, '.less');
  122. lessPath = lessPath.replace(tasks.regExp.theme, source.definitions);
  123. }
  124. else if(isSiteTheme) {
  125. console.log('Change detected in site theme');
  126. lessPath = util.replaceExtension(file.path, '.less');
  127. lessPath = lessPath.replace(source.site, source.definitions);
  128. }
  129. else {
  130. console.log('Change detected in definition');
  131. lessPath = file.path;
  132. }
  133. /*--------------
  134. Create CSS
  135. ---------------*/
  136. if( fs.existsSync(lessPath) ) {
  137. // unified css stream
  138. stream = gulp.src(lessPath)
  139. .pipe(plumber())
  140. .pipe(less(settings.less))
  141. .pipe(replace(comments.variables.in, comments.variables.out))
  142. .pipe(replace(comments.large.in, comments.large.out))
  143. .pipe(replace(comments.small.in, comments.small.out))
  144. .pipe(replace(comments.tiny.in, comments.tiny.out))
  145. .pipe(autoprefixer(settings.prefix))
  146. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  147. ;
  148. // use 2 concurrent streams from same pipe
  149. uncompressedStream = stream.pipe(clone());
  150. compressedStream = stream.pipe(clone());
  151. uncompressedStream
  152. .pipe(plumber())
  153. .pipe(replace(assets.source, assets.uncompressed))
  154. .pipe(header(banner, settings.header))
  155. .pipe(gulp.dest(output.uncompressed))
  156. .pipe(print(log.created))
  157. .on('end', function() {
  158. gulp.start('package uncompressed docs css');
  159. })
  160. ;
  161. compressedStream = stream
  162. .pipe(plumber())
  163. .pipe(replace(assets.source, assets.compressed))
  164. .pipe(minifyCSS(settings.minify))
  165. .pipe(rename(settings.rename.minCSS))
  166. .pipe(header(banner, settings.header))
  167. .pipe(gulp.dest(output.compressed))
  168. .pipe(print(log.created))
  169. .on('end', function() {
  170. gulp.start('package compressed docs css');
  171. })
  172. ;
  173. }
  174. else {
  175. console.log('Cannot find UI definition at path', lessPath);
  176. }
  177. })
  178. ;
  179. /*--------------
  180. Watch JS
  181. ---------------*/
  182. gulp
  183. .watch([
  184. source.definitions + '/**/*.js'
  185. ], function(file) {
  186. gulp.src(file.path)
  187. .pipe(plumber())
  188. .pipe(gulpif(config.hasPermission, chmod(config.permission)))
  189. .pipe(gulp.dest(output.uncompressed))
  190. .pipe(print(log.created))
  191. .pipe(uglify(settings.uglify))
  192. .pipe(rename(settings.rename.minJS))
  193. .pipe(gulp.dest(output.compressed))
  194. .pipe(print(log.created))
  195. .on('end', function() {
  196. gulp.start('package compressed docs js');
  197. gulp.start('package uncompressed docs js');
  198. })
  199. ;
  200. })
  201. ;
  202. /*--------------
  203. Watch Assets
  204. ---------------*/
  205. // only copy assets that match component names (or their plural)
  206. gulp
  207. .watch([
  208. source.themes + '/**/assets/**/' + globs.components + '?(s).*'
  209. ], function(file) {
  210. // copy assets
  211. gulp.src(file.path, { 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. ;
  218. };