tests-index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env node
  2. const path = require('path')
  3. const { exec, execSync } = require('child_process')
  4. const { writeFile } = require('./util')
  5. const glob = require('glob')
  6. const chokidar = require('chokidar')
  7. // let cmdArgs = process.argv.slice(2)
  8. // let isWatching = cmdArgs.indexOf('--watch') !== -1
  9. let tmpDir = path.join(__dirname, '../../tmp')
  10. exports.buildTestIndex = (watch) => {
  11. return _buildTestIndex().then(() => watch && watchChanges())
  12. }
  13. function watchChanges() {
  14. let dirs = glob.sync('tsc-output/packages*/__tests__/src', { cwd: tmpDir })
  15. console.log('[test-index] Watching for changes...', dirs)
  16. let watcher = chokidar.watch(dirs, {
  17. cwd: tmpDir,
  18. ignoreInitial: true
  19. })
  20. watcher.on('all', (event, path) => { // TODO: debounce?
  21. if (path.match(/\.js$/)) {
  22. console.log('[test-index] ' + event + ': ' + path)
  23. _buildTestIndex()
  24. }
  25. })
  26. process.on('SIGINT', function() {
  27. console.log('[test-index] No longer watching for changes.')
  28. watcher.close()
  29. })
  30. }
  31. function _buildTestIndex() {
  32. return new Promise((resolve, reject) => {
  33. exec(
  34. 'find tsc-output/packages*/__tests__/src -mindepth 2 -name \'*.js\' -print0 | ' +
  35. 'xargs -0 grep -E "(fdescribe|fit)\\("',
  36. {
  37. cwd: tmpDir,
  38. encoding: 'utf8'
  39. },
  40. function(error, stdout, stderr) {
  41. let files
  42. if (error && stderr) { // means there was a real error
  43. reject(new Error(stderr))
  44. } else {
  45. if (error) { // means there were no files that matched
  46. let findOutput = execSync('find tsc-output/packages*/__tests__/src -mindepth 2 -name \'*.js\'', {
  47. cwd: tmpDir,
  48. encoding: 'utf8'
  49. })
  50. findOutput = findOutput.trim()
  51. files = !findOutput ? [] : findOutput.split('\n')
  52. files = uniqStrs(files)
  53. console.log(`[test-index] Compiling all ${files.length} test files.`)
  54. } else {
  55. stdout = stdout.trim()
  56. let lines = !stdout ? [] : stdout.split('\n')
  57. files = lines.map((line) => line.trim().split(':')[0]) // TODO: do a max split of 1
  58. files = uniqStrs(files)
  59. console.log(
  60. '[test-index] Compiling only test files that have fdescribe/fit:\n' +
  61. files.map((file) => ` - ${file}`).join('\n')
  62. )
  63. }
  64. let mainFiles = glob.sync('tsc-output/packages*/__tests__/src/main.js', {
  65. cwd: tmpDir
  66. })
  67. files = mainFiles.concat(files)
  68. let code =
  69. files.map(
  70. (file) => `import ${JSON.stringify('./' + file)}`
  71. ).join('\n') +
  72. '\n'
  73. writeFile(
  74. path.join(tmpDir, 'tests-index.js'),
  75. code
  76. ).then(resolve)
  77. }
  78. }
  79. )
  80. })
  81. }
  82. function uniqStrs(a) {
  83. let hash = {}
  84. for (let item of a) {
  85. hash[item] = true
  86. }
  87. return Object.keys(hash)
  88. }