inject.js 5.8 KB

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