watch.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * watch.js
  3. * --------
  4. * Searches for each example's `src/js/app.js` file.
  5. * Creates a new watchify instance for each `app.js`.
  6. * Changes to Uppy's source will trigger rebundling.
  7. *
  8. * Note:
  9. * Since each example is dependent on Uppy's source,
  10. * changing one source file causes the 'file changed'
  11. * notification to fire multiple times. To stop this,
  12. * files are added to a 'muted' array that is checked
  13. * before announcing a changed file. It's removed from
  14. * the array when it has been bundled.
  15. */
  16. var createStream = require('fs').createWriteStream;
  17. var glob = require('multi-glob').glob;
  18. var chalk = require('chalk');
  19. var notifier = require('node-notifier');
  20. var babelify = require('babelify');
  21. var browserify = require('browserify');
  22. var watchify = require('watchify');
  23. var src = 'src/js/app.js';
  24. var dest = 'static/js/app.js';
  25. var pattern = 'src/examples/**/' + src;
  26. // Find each app.js file with glob.
  27. // 'website/' glob is for when calling `node website/watch.js` from root.
  28. glob([pattern, 'website/' + pattern], function(err, files) {
  29. if (err) throw new Error(err);
  30. console.log('--> Watching examples..');
  31. console.log('--> Pre-building ' + files.length + ' files..')
  32. var muted = [];
  33. // Create a new watchify instance for each file.
  34. files.forEach(function(file) {
  35. var watcher = browserify(file, {
  36. cache: {},
  37. packageCache: {},
  38. plugin: [watchify]
  39. })
  40. // Aliasing for using `require('uppy')`, etc.
  41. .require('../src/index.js', { expose: 'uppy' })
  42. .require('../src/core/index.js', { expose: 'uppy/core' })
  43. .require('../src/plugins/index.js', { expose: 'uppy/plugins' })
  44. .transform(babelify);
  45. // Listeners for changes, errors, and completion.
  46. watcher
  47. .on('update', bundle)
  48. .on('error', onError)
  49. .on('log', function(msg) {
  50. console.info(chalk.green('✓ done:'), chalk.green(file), chalk.gray.dim('(' + msg + ')'));
  51. })
  52. .on('file', function(file, id, parent) {
  53. // When file completes, unmute it.
  54. muted = muted.filter(function(mutedId) {
  55. return id !== mutedId;
  56. });
  57. });
  58. // Call bundle() manually to start watch processes.
  59. bundle();
  60. /**
  61. * Creates bundle and writes it to static and public folders.
  62. * Changes to
  63. * @param {[type]} ids [description]
  64. * @return {[type]} [description]
  65. */
  66. function bundle(ids) {
  67. ids = ids || [];
  68. ids.forEach(function(id) {
  69. if (!isMuted(id, muted)) {
  70. console.info(chalk.cyan('change:'), id);
  71. muted.push(id);
  72. }
  73. });
  74. var output = file.replace(src, dest);
  75. var bundle = watcher.bundle()
  76. .on('error', onError)
  77. bundle.pipe(createStream(output));
  78. bundle.pipe(createStream(output.replace('src', 'public')));
  79. }
  80. });
  81. });
  82. /**
  83. * Logs to console and shows desktop notification on error.
  84. * Calls `this.emit(end)` to stop bundling.
  85. * @param {object} err Error object
  86. */
  87. function onError(err) {
  88. console.error(chalk.red('✗ error:'), chalk.red(err.message));
  89. notifier.notify({
  90. 'title': 'Build failed:',
  91. 'message': err.message
  92. })
  93. this.emit('end');
  94. }
  95. /**
  96. * Checks if a file has been added to muted list.
  97. * This stops single changes from logging multiple times.
  98. * @param {string} id Name of changed file
  99. * @param {Array<string>} list Muted files array
  100. * @return {Boolean} True if file is muted
  101. */
  102. function isMuted(id, list) {
  103. return list.reduce(function(prev, curr) {
  104. return prev || (curr === id);
  105. }, false);
  106. }