PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/gulpfile.js

https://gitlab.com/jyrkidn/test-dev
JavaScript | 259 lines | 108 code | 21 blank | 130 comment | 0 complexity | dac7c8452f8845c0523be056d4e80cf0 MD5 | raw file
  1. var gulp = require('gulp'),
  2. path = require('path'),
  3. uglifycss = require('gulp-uglifycss'),
  4. browserSync = require('browser-sync').create(),
  5. jshint = require('gulp-jshint'),
  6. uglify = require('gulp-uglify'),
  7. concat = require('gulp-concat'),
  8. wiredep = require('wiredep').stream,
  9. gulpFilter = require('gulp-filter'),
  10. mainBowerFiles = require('main-bower-files'),
  11. merge = require('merge-stream'),
  12. inject = require('gulp-inject'),
  13. sourcemaps = require('gulp-sourcemaps'),
  14. autoprefixer = require('gulp-autoprefixer'),
  15. sass = require('gulp-sass'),
  16. plumber = require('gulp-plumber'),
  17. gutil = require('gulp-util'),
  18. del = require('del'),
  19. fs = require('fs'),
  20. assets,
  21. destPath = 'public_html';
  22. require('es6-promise').polyfill();
  23. assets = {
  24. styles: 'app/Assets/Stylesheets/',
  25. scripts: 'app/Assets/Scripts/',
  26. fonts: 'app/Assets/Fonts/**/*'
  27. }
  28. function getFolders(dir) {
  29. return fs.readdirSync(dir)
  30. .filter(function(file) {
  31. return fs.statSync(path.join(dir, file)).isDirectory();
  32. });
  33. }
  34. gulp.task('styles', function() {
  35. var folders = getFolders(assets.styles),
  36. tasks,
  37. sassOptions = {
  38. style: 'expanded'
  39. },
  40. injectFiles = gulp.src([
  41. path.join(assets.styles, '/*.scss'),
  42. path.join('!' + assets.styles, '/website.scss')
  43. ], { read: false })
  44. injectOptions = {
  45. transform: function(filePath) {
  46. filePath = filePath.replace(assets.styles, '');
  47. return '@import "' + filePath + '";';
  48. },
  49. starttag: '// injector',
  50. endtag: '// endinjector',
  51. addRootSlash: false
  52. };
  53. tasks = folders.map(function(folder) {
  54. var name = folder.toLowerCase();
  55. return gulp.src([
  56. path.join(assets.styles, folder, '/', name + '.scss')
  57. ])
  58. .pipe(sourcemaps.init())
  59. .pipe(sass(sassOptions).on('error', sass.logError))
  60. .pipe(autoprefixer())
  61. .pipe(sourcemaps.write())
  62. .pipe(gulp.dest(path.join(destPath, '/.tmp/styles/')))
  63. .pipe(uglifycss({
  64. "max-line-len": 80
  65. }))
  66. .pipe(gulp.dest(path.join(destPath, '/css/')))
  67. .pipe(browserSync.stream());
  68. });
  69. root = gulp.src([
  70. path.join(assets.styles, '/website.scss')
  71. ])
  72. .pipe(inject(injectFiles, injectOptions))
  73. .pipe(wiredep({
  74. directory: 'app/bower_components',
  75. bowerJson: require('./app/bower.json')
  76. }))
  77. .pipe(sourcemaps.init())
  78. .pipe(sass(sassOptions).on('error', sass.logError))
  79. .pipe(autoprefixer())
  80. .pipe(sourcemaps.write())
  81. .pipe(gulp.dest(path.join(destPath, '/.tmp/styles/')))
  82. .pipe(uglifycss({
  83. "max-line-len": 80
  84. }))
  85. .pipe(gulp.dest(path.join(destPath, '/css/')))
  86. .pipe(browserSync.stream());
  87. return merge(tasks, root);
  88. });
  89. // Lint JS
  90. gulp.task('jshint', function () {
  91. return gulp.src(assets.scripts + '**/*.js')
  92. .pipe(jshint())
  93. .pipe(jshint.reporter('jshint-stylish'))
  94. .pipe(jshint.reporter('fail'));
  95. });
  96. // Concat & Minify JS
  97. gulp.task('scripts', ['jshint'], function() {
  98. var folders = getFolders(assets.scripts),
  99. tasks,
  100. root;
  101. tasks = folders.map(function(folder) {
  102. var name = folder.toLowerCase();
  103. return gulp.src(path.join(assets.scripts, folder, '/**/*.js'))
  104. .pipe(sourcemaps.init())
  105. .pipe(concat(name + '.js'))
  106. .pipe(sourcemaps.write())
  107. .pipe(gulp.dest('public_html/.tmp/scripts/'))
  108. .pipe(uglify())
  109. .pipe(gulp.dest('public_html/js'));
  110. });
  111. // root = gulp.src(['app/Plugin/*/Assets/Scripts/**/*.js', '!app/Plugin/CakeAdmin/Assets/Scripts/**/*.js', assets.scripts + '*.js'])
  112. root = gulp.src(assets.scripts + '*.js')
  113. .pipe(sourcemaps.init())
  114. .pipe(concat('website.js'))
  115. .pipe(sourcemaps.write())
  116. .pipe(gulp.dest('public_html/.tmp/scripts/'))
  117. .pipe(uglify())
  118. .pipe(gulp.dest('public_html/js'));
  119. return merge(tasks, root);
  120. });
  121. gulp.task('wiredep', function () {
  122. var jsFilter = gulpFilter('*.js', {restore: true}),
  123. cssFilter = gulpFilter('**/*.css', {restore: true});
  124. return gulp.src(mainBowerFiles({
  125. paths: {
  126. bowerDirectory: 'app/bower_components',
  127. bowerrc: 'app/.bowerrc',
  128. bowerJson: 'app/bower.json'
  129. }
  130. }))
  131. // grab vendor js files from bower_components, minify and push in /public
  132. .pipe(jsFilter)
  133. .pipe(concat('vendor.js'))
  134. .pipe(gulp.dest(destPath + '/.tmp/scripts'))
  135. .pipe(uglify())
  136. .pipe(gulp.dest(destPath + '/js'))
  137. .pipe(jsFilter.restore)
  138. // grab vendor css files from bower_components, minify and push in /public
  139. .pipe(cssFilter)
  140. .pipe(concat('vendor.css'))
  141. .pipe(gulp.dest(destPath + '/.tmp/styles'))
  142. .pipe(uglifycss())
  143. .pipe(gulp.dest(destPath + '/css'))
  144. .pipe(cssFilter.restore);
  145. });
  146. gulp.task('fonts', function () {
  147. return gulp.src(mainBowerFiles({
  148. filter: '**/*.{eot,svg,ttf,woff,woff2}',
  149. paths: {
  150. bowerDirectory: 'app/bower_components',
  151. bowerrc: 'app/.bowerrc',
  152. bowerJson: 'app/bower.json'
  153. }
  154. }).concat(assets.fonts))
  155. .pipe(gulp.dest(destPath + '/.tmp/fonts'))
  156. .pipe(gulp.dest(destPath + '/fonts'));
  157. });
  158. gulp.task('clean', function (cb) {
  159. del([
  160. destPath + '/.tmp/*',
  161. destPath + '/js/*',
  162. '!' + destPath + '/js/empty',
  163. destPath + '/css/*',
  164. '!' + destPath + '/css/empty',
  165. destPath + '/fonts/*',
  166. '!' + destPath + '/fonts/empty',
  167. ], cb);
  168. });
  169. gulp.task('watch', ['wiredep', 'fonts', 'scripts', 'styles', 'tmpCopy'], function() {
  170. browserSync.init({
  171. proxy: __dirname.replace('/srv', 'hq.codedor.be') + '/public_html/',
  172. host: 'hq.codedor.be',
  173. port: 4725,
  174. ui: false,
  175. open: false,
  176. rewriteRules: [
  177. {
  178. match: /\/www\/public_html\/css\//g,
  179. fn: function (match) {
  180. return '/www/public_html/.tmp/styles/';
  181. }
  182. },
  183. {
  184. match: /\/www\/public_html\/js\//g,
  185. fn: function (match) {
  186. return '/www/public_html/.tmp/scripts/';
  187. }
  188. }
  189. ]
  190. });
  191. gulp.watch('.git/HEAD', function() {
  192. throw new gutil.PluginError('git-branch', {
  193. message: 'You changed git branch!'
  194. });
  195. });
  196. // Recompile less when a less file was changed
  197. gulp.watch(assets.styles + '**/*', ['styles']);
  198. // Watch JS Files
  199. gulp.watch(assets.scripts + '**/*.js', ['scripts']).on('change', browserSync.reload);
  200. // Watch fonts folder
  201. gulp.watch(assets.fonts + '**/*', ['fonts']).on('change', browserSync.reload);
  202. // Reload browser and update vendor when a bower package has been updated or added
  203. gulp.watch('app/bower.json', ['wiredep']).on('change', browserSync.reload);
  204. });
  205. gulp.task('build', ['clean'], function(cb) {
  206. // Only start watching if clean task has finished
  207. gulp.start('watch');
  208. // Add php and ctp watch here, because it breaks if we do this in the watch task
  209. gulp.watch('app/**/*.{php,ctp}').on('change', browserSync.reload);
  210. });
  211. // Copy fonts and images
  212. gulp.task('tmpCopy', function() {
  213. var fonts = gulp.src(['fonts/**/*'], {cwd: destPath})
  214. .pipe(gulp.dest(destPath + '/.tmp/fonts'));
  215. var img = gulp.src(['img/**/*'], {cwd: destPath})
  216. .pipe(gulp.dest(destPath + '/.tmp/img'));
  217. return merge(fonts, img);
  218. });
  219. gulp.task('init', ['clean'], function(cb) {
  220. gulp.start('wiredep');
  221. gulp.start('fonts');
  222. gulp.start('scripts');
  223. gulp.start('styles');
  224. });
  225. gulp.task('default', ['clean', 'build'], browserSync.reload);