tests-index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. files.sort() // work around OS-dependent sorting
  54. console.log(`[test-index] Compiling all ${files.length} test files.`)
  55. } else {
  56. stdout = stdout.trim()
  57. let lines = !stdout ? [] : stdout.split('\n')
  58. files = lines.map((line) => line.trim().split(':')[0]) // TODO: do a max split of 1
  59. files = uniqStrs(files)
  60. files.sort() // work around OS-dependent sorting
  61. console.log(
  62. '[test-index] Compiling only test files that have fdescribe/fit:\n' +
  63. files.map((file) => ` - ${file}`).join('\n')
  64. )
  65. }
  66. let mainFiles = glob.sync('tsc-output/packages*/__tests__/src/main.js', {
  67. cwd: tmpDir
  68. })
  69. files = mainFiles.concat(files)
  70. let code =
  71. files.map(
  72. (file) => `import ${JSON.stringify('./' + file)}`
  73. ).join('\n') +
  74. '\n'
  75. writeFile(
  76. path.join(tmpDir, 'tests-index.js'),
  77. code
  78. ).then(resolve)
  79. }
  80. }
  81. )
  82. })
  83. }
  84. /*
  85. TODO: make unnecessary. have grep do this instead with the -l option:
  86. https://stackoverflow.com/questions/6637882/how-can-i-use-grep-to-show-just-filenames-on-linux
  87. */
  88. function uniqStrs(a) {
  89. let hash = {}
  90. for (let item of a) {
  91. hash[item] = true
  92. }
  93. return Object.keys(hash)
  94. }