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.

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