build-examples.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * build-examples.js
  3. * --------
  4. * Searches for each example's `js/app.es6` file.
  5. * Creates a new watchify instance for each `app.es6`.
  6. * Changes to Uppy's source will trigger rebundling.
  7. *
  8. * Run as:
  9. *
  10. * build-examples.js # to build all examples one-off
  11. * build-examples.js watch # to keep rebuilding examples with an internal watchify
  12. * build-examples.js <path> # to build just one example app.es6
  13. * build-examples.js <path> <path> # to build just one example app.es6 to a specific location
  14. *
  15. * Note:
  16. * Since each example is dependent on Uppy's source,
  17. * changing one source file causes the 'file changed'
  18. * notification to fire multiple times. To stop this,
  19. * files are added to a 'muted' array that is checked
  20. * before announcing a changed file. It's removed from
  21. * the array when it has been bundled.
  22. */
  23. var createStream = require('fs').createWriteStream
  24. var glob = require('multi-glob').glob
  25. var chalk = require('chalk')
  26. var path = require('path')
  27. var mkdirp = require('mkdirp')
  28. var notifier = require('node-notifier')
  29. var babelify = require('babelify')
  30. var hbsfy = require('hbsfy')
  31. var browserify = require('browserify')
  32. var watchify = require('watchify')
  33. var webRoot = __dirname
  34. var uppyRoot = path.dirname(webRoot)
  35. var srcPattern = webRoot + '/src/examples/**/app.es6'
  36. var dstPattern = webRoot + '/public/examples/**/app.js'
  37. var watchifyEnabled = process.argv[2] === 'watch'
  38. var browserifyPlugins = []
  39. if (watchifyEnabled) {
  40. browserifyPlugins.push(watchify)
  41. }
  42. // Instead of 'watch', build-examples.js can also take a path as cli argument.
  43. // In this case we'll only bundle the specified path/pattern
  44. if (!watchifyEnabled && process.argv[2]) {
  45. srcPattern = process.argv[2]
  46. if (process.argv[3]) {
  47. dstPattern = process.argv[3]
  48. }
  49. }
  50. // Find each app.es6 file with glob.
  51. glob(srcPattern, function (err, files) {
  52. if (err) throw new Error(err)
  53. if (watchifyEnabled) {
  54. console.log('--> Watching examples..')
  55. }
  56. var muted = []
  57. // Create a new watchify instance for each file.
  58. files.forEach(function (file) {
  59. var browseFy = browserify(file, {
  60. cache : {},
  61. packageCache: {},
  62. plugin : browserifyPlugins
  63. })
  64. // Aliasing for using `require('uppy')`, etc.
  65. browseFy
  66. .require(uppyRoot + '/src/index.js', { expose: 'uppy' })
  67. .require(uppyRoot + '/src/core/index.js', { expose: 'uppy/core' })
  68. .require(uppyRoot + '/src/plugins/index.js', { expose: 'uppy/plugins' })
  69. .transform(hbsfy)
  70. .transform(babelify)
  71. // Listeners for changes, errors, and completion.
  72. browseFy
  73. .on('update', bundle)
  74. .on('error', onError)
  75. .on('file', function (file, id, parent) {
  76. // When file completes, unmute it.
  77. muted = muted.filter(function (mutedId) {
  78. return id !== mutedId
  79. })
  80. })
  81. // Call bundle() manually to start watch processes.
  82. bundle()
  83. /**
  84. * Creates bundle and writes it to static and public folders.
  85. * Changes to
  86. * @param {[type]} ids [description]
  87. * @return {[type]} [description]
  88. */
  89. function bundle (ids) {
  90. ids = ids || []
  91. ids.forEach(function (id) {
  92. if (!isMuted(id, muted)) {
  93. console.info(chalk.cyan('change:'), id)
  94. muted.push(id)
  95. }
  96. })
  97. var exampleName = path.basename(path.dirname(file))
  98. var output = dstPattern.replace('**', exampleName)
  99. var parentDir = path.dirname(output)
  100. mkdirp.sync(parentDir)
  101. console.info(chalk.green('✓ building:'), chalk.green(path.relative(process.cwd(), file)))
  102. var bundle = browseFy.bundle()
  103. .on('error', onError)
  104. bundle.pipe(createStream(output))
  105. }
  106. })
  107. })
  108. /**
  109. * Logs to console and shows desktop notification on error.
  110. * Calls `this.emit(end)` to stop bundling.
  111. * @param {object} err Error object
  112. */
  113. function onError (err) {
  114. console.error(chalk.red('✗ error:'), chalk.red(err.message))
  115. notifier.notify({
  116. 'title' : 'Build failed:',
  117. 'message': err.message
  118. })
  119. this.emit('end')
  120. }
  121. /**
  122. * Checks if a file has been added to muted list.
  123. * This stops single changes from logging multiple times.
  124. * @param {string} id Name of changed file
  125. * @param {Array<string>} list Muted files array
  126. * @return {Boolean} True if file is muted
  127. */
  128. function isMuted (id, list) {
  129. return list.reduce(function (prev, curr) {
  130. return prev || (curr === id)
  131. }, false)
  132. }