inject.js 4.9 KB

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