update.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const fs = require('fs')
  2. const path = require('path')
  3. const chalk = require('chalk')
  4. const { exec } = require('child_process')
  5. const YAML = require('js-yaml')
  6. const { promisify } = require('util')
  7. const gzipSize = require('gzip-size')
  8. const bytes = require('pretty-bytes')
  9. const browserify = require('browserify')
  10. const webRoot = __dirname
  11. const uppyRoot = path.join(__dirname, '../packages/uppy')
  12. const configPath = path.join(webRoot, '/themes/uppy/_config.yml')
  13. const { version } = require(path.join(uppyRoot, '/package.json'))
  14. const defaultConfig = {
  15. comment: 'Auto updated by update.js',
  16. uppy_version_anchor: '001',
  17. uppy_version: '0.0.1',
  18. uppy_bundle_kb_sizes: {},
  19. config: {}
  20. }
  21. // Keeping a whitelist so utils etc are excluded
  22. // It may be easier to maintain a blacklist instead
  23. const packages = [
  24. 'uppy',
  25. '@uppy/core',
  26. '@uppy/dashboard',
  27. '@uppy/drag-drop',
  28. '@uppy/file-input',
  29. '@uppy/webcam',
  30. '@uppy/dropbox',
  31. '@uppy/google-drive',
  32. '@uppy/instagram',
  33. '@uppy/url',
  34. '@uppy/tus',
  35. '@uppy/xhr-upload',
  36. '@uppy/aws-s3',
  37. '@uppy/aws-s3-multipart',
  38. '@uppy/status-bar',
  39. '@uppy/progress-bar',
  40. '@uppy/informer',
  41. '@uppy/transloadit',
  42. '@uppy/form',
  43. '@uppy/golden-retriever',
  44. '@uppy/react',
  45. '@uppy/store-default',
  46. '@uppy/store-redux'
  47. ]
  48. const excludes = {
  49. '@uppy/react': ['react']
  50. }
  51. update().catch((err) => {
  52. console.error(err)
  53. process.exit(1)
  54. })
  55. async function getMinifiedSize (pkg, name) {
  56. const b = browserify(pkg)
  57. if (name !== '@uppy/core' && name !== 'uppy') {
  58. b.exclude('@uppy/core')
  59. }
  60. if (excludes[name]) {
  61. b.exclude(excludes[name])
  62. }
  63. b.plugin('tinyify')
  64. const bundle = await promisify(b.bundle).call(b)
  65. const gzipped = await gzipSize(bundle)
  66. return {
  67. minified: bundle.length,
  68. gzipped
  69. }
  70. }
  71. async function updateSizes (config) {
  72. console.info(chalk.grey('Generating bundle sizes…'))
  73. const padTarget = packages.reduce((max, cur) => Math.max(max, cur.length), 0) + 2
  74. const sizesPromise = Promise.all(
  75. packages.map(async (pkg) => {
  76. const result = await getMinifiedSize(path.join(__dirname, '../packages', pkg), pkg)
  77. console.info(chalk.green(
  78. // ✓ @uppy/pkgname: 10.0 kB min / 2.0 kB gz
  79. ` ✓ ${pkg}: ${' '.repeat(padTarget - pkg.length)}` +
  80. `${bytes(result.minified)} min`.padEnd(10) +
  81. ` / ${bytes(result.gzipped)} gz`
  82. ))
  83. return Object.assign(result, {
  84. prettyMinified: bytes(result.minified),
  85. prettyGzipped: bytes(result.gzipped)
  86. })
  87. })
  88. ).then((list) => {
  89. const map = {}
  90. list.forEach((size, i) => {
  91. map[packages[i]] = size
  92. })
  93. return map
  94. })
  95. config.uppy_bundle_kb_sizes = await sizesPromise
  96. }
  97. async function injectBuiltFiles () {
  98. const cmds = [
  99. `mkdir -p ${path.join(webRoot, '/themes/uppy/source/uppy')}`,
  100. `cp -vfR ${path.join(uppyRoot, '/dist/*')} ${path.join(webRoot, '/themes/uppy/source/uppy/')}`
  101. ].join(' && ')
  102. const { stdout } = await promisify(exec)(cmds)
  103. stdout.trim().split('\n').forEach(function (line) {
  104. console.info(chalk.green('✓ injected: '), chalk.grey(line))
  105. })
  106. }
  107. async function update () {
  108. const buf = await promisify(fs.readFile)(configPath, 'utf8')
  109. const config = YAML.safeLoad(buf)
  110. config.uppy_version = version
  111. config.uppy_version_anchor = version.replace(/[^\d]+/g, '')
  112. await updateSizes(config)
  113. const saveConfig = Object.assign({}, defaultConfig, config)
  114. await promisify(fs.writeFile)(configPath, YAML.safeDump(saveConfig), 'utf-8')
  115. console.info(chalk.green('✓ rewritten: '), chalk.grey(configPath))
  116. try {
  117. await injectBuiltFiles()
  118. } catch (error) {
  119. console.error(
  120. chalk.red('x failed to inject: '),
  121. chalk.grey('uppy bundle into site, because: ' + error)
  122. )
  123. process.exit(1)
  124. }
  125. }