update.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/thumbnail-generator',
  46. '@uppy/store-default',
  47. '@uppy/store-redux'
  48. ]
  49. const excludes = {
  50. '@uppy/react': ['react']
  51. }
  52. update().catch((err) => {
  53. console.error(err)
  54. process.exit(1)
  55. })
  56. async function getMinifiedSize (pkg, name) {
  57. const b = browserify(pkg)
  58. if (name !== '@uppy/core' && name !== 'uppy') {
  59. b.exclude('@uppy/core')
  60. }
  61. if (excludes[name]) {
  62. b.exclude(excludes[name])
  63. }
  64. b.plugin('tinyify')
  65. const bundle = await promisify(b.bundle).call(b)
  66. const gzipped = await gzipSize(bundle)
  67. return {
  68. minified: bundle.length,
  69. gzipped
  70. }
  71. }
  72. async function updateSizes (config) {
  73. console.info(chalk.grey('Generating bundle sizes…'))
  74. const padTarget = packages.reduce((max, cur) => Math.max(max, cur.length), 0) + 2
  75. const sizesPromise = Promise.all(
  76. packages.map(async (pkg) => {
  77. const result = await getMinifiedSize(path.join(__dirname, '../packages', pkg), pkg)
  78. console.info(chalk.green(
  79. // ✓ @uppy/pkgname: 10.0 kB min / 2.0 kB gz
  80. ` ✓ ${pkg}: ${' '.repeat(padTarget - pkg.length)}` +
  81. `${bytes(result.minified)} min`.padEnd(10) +
  82. ` / ${bytes(result.gzipped)} gz`
  83. ))
  84. return Object.assign(result, {
  85. prettyMinified: bytes(result.minified),
  86. prettyGzipped: bytes(result.gzipped)
  87. })
  88. })
  89. ).then((list) => {
  90. const map = {}
  91. list.forEach((size, i) => {
  92. map[packages[i]] = size
  93. })
  94. return map
  95. })
  96. config.uppy_bundle_kb_sizes = await sizesPromise
  97. }
  98. async function injectBuiltFiles () {
  99. const cmds = [
  100. `mkdir -p ${path.join(webRoot, '/themes/uppy/source/uppy')}`,
  101. `cp -vfR ${path.join(uppyRoot, '/dist/*')} ${path.join(webRoot, '/themes/uppy/source/uppy/')}`
  102. ].join(' && ')
  103. const { stdout } = await promisify(exec)(cmds)
  104. stdout.trim().split('\n').forEach(function (line) {
  105. console.info(chalk.green('✓ injected: '), chalk.grey(line))
  106. })
  107. }
  108. async function readConfig () {
  109. try {
  110. const buf = await promisify(fs.readFile)(configPath, 'utf8')
  111. return YAML.safeLoad(buf)
  112. } catch (err) {
  113. return {}
  114. }
  115. }
  116. async function update () {
  117. const config = await readConfig()
  118. config.uppy_version = version
  119. config.uppy_version_anchor = version.replace(/[^\d]+/g, '')
  120. await updateSizes(config)
  121. const saveConfig = Object.assign({}, defaultConfig, config)
  122. await promisify(fs.writeFile)(configPath, YAML.safeDump(saveConfig), 'utf-8')
  123. console.info(chalk.green('✓ rewritten: '), chalk.grey(configPath))
  124. try {
  125. await injectBuiltFiles()
  126. } catch (error) {
  127. console.error(
  128. chalk.red('x failed to inject: '),
  129. chalk.grey('uppy bundle into site, because: ' + error)
  130. )
  131. process.exit(1)
  132. }
  133. }