update.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 touch = require('touch')
  11. const webRoot = __dirname
  12. const uppyRoot = path.join(__dirname, '../packages/uppy')
  13. const configPath = path.join(webRoot, '/themes/uppy/_config.yml')
  14. const { version } = require(path.join(uppyRoot, '/package.json'))
  15. const defaultConfig = {
  16. comment: 'Auto updated by update.js',
  17. uppy_version_anchor: '001',
  18. uppy_version: '0.0.1',
  19. uppy_bundle_kb_sizes: {},
  20. config: {}
  21. }
  22. // Keeping a whitelist so utils etc are excluded
  23. // It may be easier to maintain a blacklist instead
  24. const packages = [
  25. 'uppy',
  26. '@uppy/core',
  27. '@uppy/dashboard',
  28. '@uppy/drag-drop',
  29. '@uppy/file-input',
  30. '@uppy/webcam',
  31. '@uppy/dropbox',
  32. '@uppy/google-drive',
  33. '@uppy/instagram',
  34. '@uppy/url',
  35. '@uppy/tus',
  36. '@uppy/xhr-upload',
  37. '@uppy/aws-s3',
  38. '@uppy/aws-s3-multipart',
  39. '@uppy/status-bar',
  40. '@uppy/progress-bar',
  41. '@uppy/informer',
  42. '@uppy/transloadit',
  43. '@uppy/form',
  44. '@uppy/golden-retriever',
  45. '@uppy/react',
  46. '@uppy/thumbnail-generator',
  47. '@uppy/store-default',
  48. '@uppy/store-redux'
  49. ]
  50. const excludes = {
  51. '@uppy/react': ['react']
  52. }
  53. update().catch((err) => {
  54. console.error(err)
  55. process.exit(1)
  56. })
  57. async function getMinifiedSize (pkg, name) {
  58. const b = browserify(pkg)
  59. if (name !== '@uppy/core' && name !== 'uppy') {
  60. b.exclude('@uppy/core')
  61. }
  62. if (excludes[name]) {
  63. b.exclude(excludes[name])
  64. }
  65. b.plugin('tinyify')
  66. const bundle = await promisify(b.bundle).call(b)
  67. const gzipped = await gzipSize(bundle)
  68. return {
  69. minified: bundle.length,
  70. gzipped
  71. }
  72. }
  73. async function updateSizes (config) {
  74. console.info(chalk.grey('Generating bundle sizes…'))
  75. const padTarget = packages.reduce((max, cur) => Math.max(max, cur.length), 0) + 2
  76. const sizesPromise = Promise.all(
  77. packages.map(async (pkg) => {
  78. const result = await getMinifiedSize(path.join(__dirname, '../packages', pkg), pkg)
  79. console.info(chalk.green(
  80. // ✓ @uppy/pkgname: 10.0 kB min / 2.0 kB gz
  81. ` ✓ ${pkg}: ${' '.repeat(padTarget - pkg.length)}` +
  82. `${bytes(result.minified)} min`.padEnd(10) +
  83. ` / ${bytes(result.gzipped)} gz`
  84. ))
  85. return Object.assign(result, {
  86. prettyMinified: bytes(result.minified),
  87. prettyGzipped: bytes(result.gzipped)
  88. })
  89. })
  90. ).then((list) => {
  91. const map = {}
  92. list.forEach((size, i) => {
  93. map[packages[i]] = size
  94. })
  95. return map
  96. })
  97. config.uppy_bundle_kb_sizes = await sizesPromise
  98. }
  99. async function injectBuiltFiles () {
  100. const cmds = [
  101. `mkdir -p ${path.join(webRoot, '/themes/uppy/source/uppy')}`,
  102. `cp -vfR ${path.join(uppyRoot, '/dist/*')} ${path.join(webRoot, '/themes/uppy/source/uppy/')}`
  103. ].join(' && ')
  104. const { stdout } = await promisify(exec)(cmds)
  105. stdout.trim().split('\n').forEach(function (line) {
  106. console.info(chalk.green('✓ injected: '), chalk.grey(line))
  107. })
  108. }
  109. async function injectMarkdown () {
  110. let sources = {
  111. '.github/ISSUE_TEMPLATE/integration_help.md': `src/_template/integration_help.md`,
  112. '.github/CONTRIBUTING.md': `src/_template/contributing.md`
  113. }
  114. for (let src in sources) {
  115. let dst = sources[src]
  116. // strip yaml frontmatter:
  117. let srcpath = path.join(uppyRoot, `/../../${src}`)
  118. let dstpath = path.join(webRoot, dst)
  119. let parts = fs.readFileSync(srcpath, 'utf-8').split(/---\s*\n/)
  120. if (parts.length >= 3) {
  121. parts.shift()
  122. parts.shift()
  123. }
  124. let content = `<!-- WARNING! This file was injected. Please edit in "${src}" instead and run "${path.basename(__filename)}" -->\n\n`
  125. content += parts.join('---\n')
  126. fs.writeFileSync(dstpath, content, 'utf-8')
  127. console.info(chalk.green(`✓ injected: `), chalk.grey(srcpath))
  128. }
  129. touch(path.join(webRoot, `/src/support.md`))
  130. }
  131. async function readConfig () {
  132. try {
  133. const buf = await promisify(fs.readFile)(configPath, 'utf8')
  134. return YAML.safeLoad(buf)
  135. } catch (err) {
  136. return {}
  137. }
  138. }
  139. async function update () {
  140. const config = await readConfig()
  141. await injectMarkdown()
  142. config.uppy_version = version
  143. config.uppy_version_anchor = version.replace(/[^\d]+/g, '')
  144. await updateSizes(config)
  145. const saveConfig = Object.assign({}, defaultConfig, config)
  146. await promisify(fs.writeFile)(configPath, YAML.safeDump(saveConfig), 'utf-8')
  147. console.info(chalk.green('✓ rewritten: '), chalk.grey(configPath))
  148. try {
  149. await injectBuiltFiles()
  150. } catch (error) {
  151. console.error(
  152. chalk.red('x failed to inject: '),
  153. chalk.grey('uppy bundle into site, because: ' + error)
  154. )
  155. process.exit(1)
  156. }
  157. }