generate-index-iife.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { join as joinPaths } from 'path'
  2. import { fileURLToPath } from 'url'
  3. import { readFile } from 'fs/promises'
  4. import handlebars from 'handlebars'
  5. import { execCapture } from '@fullcalendar-scripts/standard/utils/exec'
  6. /*
  7. TODO: don't always display prefix when doing config.log()
  8. TODO: don't reinit rollup watcher on ANY change. Slow when not using fdescribe technique.
  9. */
  10. const thisPkgDir = joinPaths(fileURLToPath(import.meta.url), '../..')
  11. const templatePath = joinPaths(thisPkgDir, 'src/index.global.js.tpl')
  12. /*
  13. HACK: watch the transpiled directory, so bundling waits until tsc completes
  14. */
  15. export function getWatchPaths(config) {
  16. const transpileDir = joinPaths(config.pkgDir, 'dist/.tsout')
  17. return [transpileDir, templatePath]
  18. }
  19. export default async function(config) {
  20. const srcDir = joinPaths(config.pkgDir, 'src')
  21. // mindepth 2 means subdirectories
  22. let testPaths = await execCapture(
  23. 'find . -mindepth 2 -type f \\( -name \'*.ts\' -or -name \'*.tsx\' \\) -print0 | ' +
  24. 'xargs -0 grep -E "(fdescribe|fit)\\("',
  25. { cwd: srcDir },
  26. ).then(
  27. (stdout) => strToLines(stdout).map((line) => line.trim().split(':')[0]),
  28. () => {
  29. return [] // TODO: somehow look at stderr string. if empty, simply no testPaths. if populated, real error
  30. },
  31. )
  32. // the `find` command reports multiple matches per file. consolidate duplicates
  33. testPaths = uniqueStrs(testPaths)
  34. if (testPaths.length) {
  35. config.log(
  36. 'Only test files that have fdescribe/fit:\n' +
  37. testPaths.join('\n'),
  38. )
  39. } else {
  40. // mindepth 2 means subdirectories
  41. testPaths = strToLines((await execCapture(
  42. 'find . -mindepth 2 -type f \\( -name \'*.ts\' -or -name \'*.tsx\' \\)',
  43. { cwd: srcDir },
  44. )))
  45. config.log(`Using all ${testPaths.length} test files`)
  46. }
  47. const extensionlessTestPaths = testPaths.map((testPath) => testPath.replace(/\.tsx?$/, ''))
  48. const templateText = await readFile(templatePath, 'utf8')
  49. const template = handlebars.compile(templateText)
  50. const code = template({ extensionlessTestPaths })
  51. return code
  52. }
  53. function uniqueStrs(strs) {
  54. const map = {}
  55. for (const str of strs) {
  56. map[str] = true
  57. }
  58. return Object.keys(map)
  59. }
  60. function strToLines(str) {
  61. str = str.trim()
  62. return str ? str.split('\n') : []
  63. }