gulpfile.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. const spawn = require('child_process').spawn;
  2. const spawnSync = require('child_process').spawnSync;
  3. const path = require('path');
  4. const del = require('del');
  5. const gulp = require('gulp');
  6. const autoprefixer = require('gulp-autoprefixer');
  7. const babel = require("gulp-babel");
  8. const changed = require("gulp-changed");
  9. const rename = require('gulp-rename');
  10. const sass = require("gulp-sass");
  11. const webpack = require('webpack-stream');
  12. let params = require('./config/params');
  13. function onError(err) {
  14. console.log(err);
  15. this.emit('end');
  16. }
  17. gulp.task('clean-www', () => {
  18. return del.sync('dist/www');
  19. });
  20. gulp.task('clean-server', () => {
  21. return del.sync('dist/server');
  22. });
  23. gulp.task('clean-config', () => {
  24. return del.sync('dist/config');
  25. });
  26. gulp.task('clean-catsoop', () => {
  27. return del.sync('dist/catsoop');
  28. });
  29. gulp.task('clean', ['clean-server', 'clean-config', 'clean-www', 'clean-catsoop'], () => {
  30. return del.sync('dist');
  31. });
  32. gulp.task('build-www-js', () => {
  33. return gulp.src('www/js/*.js')
  34. .pipe(webpack({
  35. entry: './www/js/queue.js',
  36. module: {
  37. loaders: [
  38. {test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/},
  39. {test: /\.html$/, loader: 'ractive'},
  40. ],
  41. },
  42. }))
  43. .on('error', onError)
  44. .pipe(rename({basename: 'queue'}))
  45. .pipe(gulp.dest('dist/www/js'))
  46. });
  47. gulp.task('build-www', ['build-www-js'], () => {
  48. js_includes = [
  49. path.join('bootstrap', 'dist', 'js', 'bootstrap.js'),
  50. path.join('jquery', 'dist', 'jquery.js'),
  51. ].map(p => path.join(__dirname, 'bower_components', p))
  52. .concat([
  53. 'queue-onload-staff.js',
  54. 'queue-onload-student.js',
  55. ].map(p => path.join(__dirname, 'www', 'js')));
  56. js_includes.push('www/js/queue-onload-staff.js');
  57. js_includes.push('www/js/queue-onload-student.js');
  58. font_includes = [
  59. path.join('bootstrap-sass', 'assets', 'fonts', '**/*'),
  60. ].map(p => path.join(__dirname, 'node_modules', p));
  61. return Promise.all([
  62. gulp.src('www/index.html')
  63. .pipe(changed('dist/www/'))
  64. .pipe(gulp.dest('dist/www/')),
  65. gulp.src('www/images/*')
  66. .pipe(changed('dist/www/images'))
  67. .pipe(gulp.dest('dist/www/images')),
  68. gulp.src('www/audio/*')
  69. .pipe(changed('dist/www/audio'))
  70. .pipe(gulp.dest('dist/www/audio')),
  71. gulp.src('www/scss/**/*.scss')
  72. .pipe(sass({includePaths: [path.join(__dirname, 'node_modules')]}).on('error', sass.logError))
  73. .pipe(autoprefixer({browsers: ['last 2 versions']}))
  74. .pipe(gulp.dest('dist/www/css')),
  75. gulp.src(js_includes)
  76. .pipe(changed('dist/www/js'))
  77. .pipe(gulp.dest('dist/www/js')),
  78. gulp.src(font_includes)
  79. .pipe(changed('dist/www/fonts'))
  80. .pipe(gulp.dest('dist/www/fonts')),
  81. ]);
  82. });
  83. gulp.task('build-config', () => {
  84. return gulp.src('config/*')
  85. .pipe(changed('dist/config'))
  86. .pipe(gulp.dest('dist/config'));
  87. });
  88. gulp.task('build-server', () => {
  89. return gulp.src('server/*')
  90. .pipe(changed('dist/server'))
  91. .pipe(gulp.dest('dist/server'));
  92. });
  93. gulp.task('build-catsoop', ['clean-catsoop'], () => {
  94. return new Promise((resolve, reject) => {
  95. spawn('./scripts/make_catsoop.py', [
  96. JSON.stringify(params),
  97. 'dist/catsoop',
  98. ], {stdio: 'inherit'})
  99. .on('exit', (code, signal) => {
  100. if (signal) reject(signal);
  101. if (code !== 0) reject(`make_catsoop.py failed with exit code ${code}`);
  102. resolve();
  103. });
  104. });
  105. });
  106. gulp.task('build', [
  107. 'build-server',
  108. 'build-config',
  109. 'build-www',
  110. 'build-catsoop',
  111. ], () => Promise.resolve());
  112. var db;
  113. gulp.task('run-db', [], () => {
  114. if (db) {
  115. return Promise.resolve();
  116. }
  117. return new Promise((resolve, reject) => {
  118. db = spawn('rethinkdb',
  119. [
  120. '--port-offset', params.RETHINKDB.PORT_OFFSET || 0,
  121. ],
  122. {stdio: 'pipe'});
  123. db.stdio[1].on('data', data => {
  124. data.toString().split('\n').forEach(line => {
  125. if (!line) return;
  126. process.stdout.write(`DB: ${line}\n`);
  127. if (/^Server ready/.test(line)) {
  128. spawn('./scripts/db_setup.js', {stdio: 'inherit'})
  129. .on('exit', (code, signal) => {
  130. if (signal) reject(signal);
  131. if (code !== 0) reject(`make_catsoop.py failed with exit code ${code}`);
  132. resolve();
  133. });
  134. }
  135. })
  136. });
  137. });
  138. });
  139. var server;
  140. gulp.task('run-server', ['build-server', 'run-db'], () => {
  141. if (server) {
  142. server.kill();
  143. }
  144. server = spawn('node', ['dist/server/index.js'], {stdio: 'inherit'});
  145. server.on('close', (code) => {
  146. if (code === 8) {
  147. gulp.log('Error detected, waiting for changes...');
  148. }
  149. });
  150. });
  151. process.on('exit', function() {
  152. if (server) {
  153. server.kill();
  154. }
  155. if (db) {
  156. db.kill();
  157. }
  158. });
  159. gulp.task('reload-config', () => {
  160. delete require.cache[require.resolve('./config/params')];
  161. params = require('./config/params');
  162. });
  163. gulp.task('start', ['clean', 'build', 'run-server'], () => {
  164. gulp.watch(['www/**/*', 'imports/*'], ['build-www']);
  165. gulp.watch(['server/*'], ['run-server']);
  166. gulp.watch(['catsoop/*'], ['build-catsoop']);
  167. gulp.watch(['config/*'], ['reload-config', 'build', 'run-server']);
  168. });
  169. gulp.task('default', ['clean', 'build', 'run-server'], () => {});