generate-test.mjs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env node
  2. /* eslint-disable no-console, import/no-extraneous-dependencies */
  3. import prompts from 'prompts'
  4. import fs from 'node:fs/promises'
  5. import dedent from 'dedent'
  6. const packageNames = await fs.readdir(new URL('../packages/@uppy', import.meta.url))
  7. const unwantedPackages = ['core', 'companion', 'redux-dev-tools', 'utils']
  8. const { name } = await prompts({
  9. type: 'text',
  10. name: 'name',
  11. message: 'What should the name of the test be (e.g `dashboard-tus`)?',
  12. validate: (value) => /^[a-z|-]+$/i.test(value),
  13. })
  14. const { packages } = await prompts({
  15. type: 'multiselect',
  16. name: 'packages',
  17. message: 'What packages do you want to test?',
  18. hint: '@uppy/core is automatically included',
  19. choices: packageNames
  20. .filter((pkg) => !unwantedPackages.includes(pkg))
  21. .map((pkg) => ({ title: pkg, value: pkg })),
  22. })
  23. const camelcase = (str) => str
  24. .toLowerCase()
  25. .replace(/([-][a-z])/g, (group) => group.toUpperCase().replace('-', ''))
  26. const testUrl = new URL(`cypress/integration/${name}.spec.ts`, import.meta.url)
  27. const test = dedent`
  28. describe('${name}', () => {
  29. beforeEach(() => {
  30. cy.visit('/${name}')
  31. })
  32. })
  33. `
  34. const htmlUrl = new URL(`clients/${name}/index.html`, import.meta.url)
  35. const html = dedent`
  36. <!doctype html>
  37. <html lang="en">
  38. <head>
  39. <meta charset="utf-8"/>
  40. <title>${name}</title>
  41. <script defer type="module" src="app.js"></script>
  42. </head>
  43. <body>
  44. <div id="app"></div>
  45. </body>
  46. </html>
  47. `
  48. const appUrl = new URL(`clients/${name}/app.js`, import.meta.url)
  49. // dedent is acting weird for this one but this formatting fixes it.
  50. const app = dedent`
  51. import Uppy from '@uppy/core'
  52. ${packages.map((pgk) => `import ${camelcase(pgk)} from '@uppy/${pgk}'`).join('\n')}
  53. const uppy = new Uppy()
  54. ${packages.map((pkg) => `.use(${camelcase(pkg)})`).join('\n\t')}
  55. // Keep this here to access uppy in tests
  56. window.uppy = uppy
  57. `
  58. await fs.writeFile(testUrl, test)
  59. await fs.mkdir(new URL(`clients/${name}`, import.meta.url))
  60. await fs.writeFile(htmlUrl, html)
  61. await fs.writeFile(appUrl, app)
  62. const homeUrl = new URL('clients/index.html', import.meta.url)
  63. const home = await fs.readFile(homeUrl, 'utf8')
  64. const newHome = home.replace(
  65. '</ul>',
  66. `\t<li><a href="${name}/index.html">${name}</a></li>\n\t\t\t</ul>`,
  67. )
  68. await fs.writeFile(homeUrl, newHome)
  69. const prettyPath = (url) => url.toString().split('uppy', 2)[1]
  70. console.log(`✅ Generated ${prettyPath(testUrl)}`)
  71. console.log(`✅ Generated ${prettyPath(htmlUrl)}`)
  72. console.log(`✅ Generated ${prettyPath(appUrl)}`)
  73. console.log(`✅ Updated ${prettyPath(homeUrl)}`)