Guest User

Untitled

a guest
Mar 24th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. const fs = require('fs');
  2.  
  3. Array.prototype.remove = function(data) {
  4. const dataIdx = this.indexOf(data)
  5. if(dataIdx >= 0) {
  6. this.splice(dataIdx ,1);
  7. }
  8. return this.length;
  9. }
  10.  
  11. const extractFileNameByPath = path => path.replace(/^.*[\\\/]/, '')
  12. const extractIndexFileNameByPath = path => {
  13. return path.split('/').slice(-2)[0] + '.js'
  14. }
  15.  
  16. const isTarget = str => str.endsWith(TARGET_FORMAT)
  17. const isDir = str => !str.includes('.')
  18.  
  19. const fetchJsFilePathByDir = dir => fs.readdirSync(dir)
  20. .filter( file => isTarget(file) )
  21. .map( fileName => `${dir}/${fileName}`)
  22.  
  23.  
  24. const fetchDirInDir = dir => fs.readdirSync(dir)
  25. .filter( file => isDir(file) )
  26. .map( fileName => `${dir}/${fileName}`)
  27.  
  28.  
  29. const processDir = dir => {
  30. let resultJsList = []
  31. const _dirs = fetchDirInDir(dir)
  32. const _files = fetchJsFilePathByDir(dir)
  33.  
  34. resultJsList = [..._files]
  35.  
  36. _dirs.forEach(__d => {
  37. resultJsList = [...processDir(__d), ...resultJsList]
  38. })
  39.  
  40. return resultJsList
  41. }
  42.  
  43.  
  44. const TARGET_FORMAT = '.js'
  45. const COMPONENTS_ROOT = './src/components'
  46. const TEST_COMPONENTS_ROOT = './__tests__/components'
  47.  
  48. const com_list = processDir(COMPONENTS_ROOT)
  49. const test_list = processDir(TEST_COMPONENTS_ROOT)
  50.  
  51. console.log(`There are ${com_list.length} ${TARGET_FORMAT} files in ${COMPONENTS_ROOT}`)
  52. console.log(`There are ${test_list.length} ${TARGET_FORMAT} files in ${TEST_COMPONENTS_ROOT}`)
  53. const searchInTestList = fn => {
  54. for(let i =0 ;i<test_list.length; i++){
  55. if(test_list[i].includes(fn))
  56. return test_list[i]
  57. }
  58. return false
  59. }
  60.  
  61.  
  62.  
  63. const non_covered_com = [...com_list]
  64. const non_covered_test = [...test_list]
  65.  
  66. com_list.forEach( filePath => {
  67. const fn = extractFileNameByPath(filePath)
  68.  
  69. if(fn !=='index.js') {
  70. const existedTestFilePath = searchInTestList(fn)
  71. if(existedTestFilePath) {
  72. non_covered_com.remove(filePath)
  73. non_covered_test.remove(existedTestFilePath)
  74. }
  75. } else {
  76. const _fn = extractIndexFileNameByPath(filePath)
  77. const existedTestFilePath = searchInTestList(_fn)
  78. if(existedTestFilePath) {
  79. non_covered_com.remove(filePath)
  80. non_covered_test.remove(existedTestFilePath)
  81. }
  82. }
  83. })
  84.  
  85. console.log(non_covered_com.length, ' non_covered_com: ', non_covered_com)
  86. console.log(non_covered_test.length, ' non_covered_test: ', non_covered_test)
Add Comment
Please, Sign In to add comment