gulpfile.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const gulp = require('gulp');
  2. const sourcemaps = require('gulp-sourcemaps');
  3. const source = require('vinyl-source-stream');
  4. const buffer = require('vinyl-buffer');
  5. const browserify = require('browserify');
  6. const watchify = require('watchify');
  7. const babelify = require('babelify');
  8. const pathmod = require('pathmodify');
  9. const babel = require('gulp-babel');
  10. const uglify = require('gulp-uglify');
  11. const gutil = require('gulp-util');
  12. const zip = require('gulp-zip');
  13. const fs = require('fs');
  14. const path = require('path');
  15. const es = require('event-stream');
  16. const Promise = require('bluebird');
  17. function slugify(text)
  18. {
  19. return text.toString().toLowerCase()
  20. .replace(/\s+/g, '-') // Replace spaces with -
  21. .replace(/[^\w\-]+/g, '') // Remove all non-word chars
  22. .replace(/\-\-+/g, '-') // Replace multiple - with single -
  23. .replace(/^-+/, '') // Trim - from start of text
  24. .replace(/-+$/, ''); // Trim - from end of text
  25. }
  26. Promise.promisifyAll(fs);
  27. // const archiveFiles = [
  28. // 'style.css',
  29. // 'screenshot.png',
  30. // '*.php'
  31. // ];
  32. // const builtFiles = [
  33. // 'js/*'
  34. // ];
  35. const watchedFiles = [
  36. 'src/markdown/**/*'
  37. ];
  38. // const themeName = 'reago';
  39. function buildClient(watch, done) {
  40. var bundler =
  41. browserify('./src/index.js', { debug: true })
  42. .plugin(pathmod, {mods: [
  43. pathmod.mod.dir('node_modules', __dirname + '/node_modules'),
  44. ]})
  45. // Transform JSX https://github.com/andreypopp/reactify/issues/58
  46. // Fix unexpected ... https://github.com/babel/babel-loader/issues/170
  47. .transform(babelify, { presets: ['es2015', 'stage-0', 'react'] });
  48. return new Promise(function (resolve, reject) {
  49. bundler.bundle()
  50. .on('error', function(err) { console.error(err); this.emit('end'); })
  51. .pipe(source('bundle.react.js'))
  52. .pipe(buffer())
  53. // .pipe(uglify())
  54. .pipe(sourcemaps.init({ loadMaps: true }))
  55. .pipe(sourcemaps.write('./'))
  56. .pipe(gulp.dest('./js'))
  57. .on('end', resolve);
  58. });
  59. }
  60. function extractMarkdown() {
  61. // return new Promise(function (resolve, reject) {
  62. return fs.readdirAsync('src/markdown')
  63. .then(subdirs => Promise.reduce(
  64. subdirs,
  65. (carry, dir) =>
  66. fs.readdirAsync('src/markdown/' + dir)
  67. .then(files => Promise.reduce(
  68. files,
  69. (carry, f) => fs.readFileAsync('src/markdown/' + dir + '/' + f)
  70. .then(buf => buf.toString())
  71. .then(content => {
  72. const title = path.basename(f, '.md');
  73. return carry.concat({
  74. title,
  75. path: '/' + slugify(title),
  76. content
  77. });
  78. }),
  79. []
  80. ))
  81. .then(items => carry.concat({ title: dir, path: '/' + slugify(dir), items })),
  82. []
  83. ))
  84. .then(JSON.stringify)
  85. .then(markdownJson => fs.writeFileAsync('src/markdown.json', markdownJson))
  86. // });
  87. }
  88. // function makeZip(cb) {
  89. // return extractThemeVersion()
  90. // .then(themeVersion => {
  91. // var base = 'dist/' + themeVersion;
  92. // var tmp = base + '/reago';
  93. // var rebasedFiles = base + '/**/*';
  94. // es.concat(
  95. // gulp.src(archiveFiles)
  96. // .pipe(gulp.dest(tmp)),
  97. // gulp.src(builtFiles)
  98. // .pipe(gulp.dest( tmp + '/js')),
  99. // gulp.src(rebasedFiles, { base })
  100. // .pipe(zip(themeName + '-' + themeVersion + '.zip'))
  101. // .pipe(gulp.dest('dist'))
  102. // ).on('end', cb)
  103. // });
  104. // }
  105. gulp.task('watch', function() {
  106. gulp.watch(['src'], buildClient);
  107. gulp.watch(watchedFiles, extractMarkdown);
  108. });
  109. gulp.task('buildClient', function() {
  110. return buildClient();
  111. });
  112. gulp.task('extractMarkdown', function() {
  113. return extractMarkdown();
  114. });
  115. // gulp.task('makeZip', makeZip);
  116. gulp.task('default', gulp.series('buildClient', 'extractMarkdown', 'watch'));