Skip to content

Instantly share code, notes, and snippets.

@wahengchang
Last active March 16, 2018 08:38
Show Gist options
  • Save wahengchang/034441466b74c0b7075d89647579c866 to your computer and use it in GitHub Desktop.
Save wahengchang/034441466b74c0b7075d89647579c866 to your computer and use it in GitHub Desktop.
It is a script of printing and counting specific file under directory
const fs = require('fs');
Array.prototype.remove = function(data) {
const dataIdx = this.indexOf(data)
if(dataIdx >= 0) {
this.splice(dataIdx ,1);
}
return this.length;
}
const extractFileNameByPath = path => path.replace(/^.*[\\\/]/, '')
const extractIndexFileNameByPath = path => {
return path.split('/').slice(-2)[0] + '.js'
}
const isTarget = str => str.endsWith(TARGET_FORMAT)
const isDir = str => !str.includes('.')
const fetchJsFilePathByDir = dir => fs.readdirSync(dir)
.filter( file => isTarget(file) )
.map( fileName => `${dir}/${fileName}`)
const fetchDirInDir = dir => fs.readdirSync(dir)
.filter( file => isDir(file) )
.map( fileName => `${dir}/${fileName}`)
const processDir = dir => {
let resultJsList = []
const _dirs = fetchDirInDir(dir)
const _files = fetchJsFilePathByDir(dir)
resultJsList = [..._files]
_dirs.forEach(__d => {
resultJsList = [...processDir(__d), ...resultJsList]
})
return resultJsList
}
const TARGET_FORMAT = '.js'
const COMPONENTS_ROOT = './src/components'
const TEST_COMPONENTS_ROOT = './__tests__/components'
const com_list = processDir(COMPONENTS_ROOT)
const test_list = processDir(TEST_COMPONENTS_ROOT)
console.log(`There are ${com_list.length} ${TARGET_FORMAT} files in ${COMPONENTS_ROOT}`)
console.log(`There are ${test_list.length} ${TARGET_FORMAT} files in ${TEST_COMPONENTS_ROOT}`)
const searchInTestList = fn => {
for(let i =0 ;i<test_list.length; i++){
if(test_list[i].includes(fn))
return test_list[i]
}
return false
}
const non_covered_com = [...com_list]
const non_covered_test = [...test_list]
com_list.forEach( filePath => {
const fn = extractFileNameByPath(filePath)
if(fn !=='index.js') {
const existedTestFilePath = searchInTestList(fn)
if(existedTestFilePath) {
non_covered_com.remove(filePath)
non_covered_test.remove(existedTestFilePath)
}
} else {
const _fn = extractIndexFileNameByPath(filePath)
const existedTestFilePath = searchInTestList(_fn)
if(existedTestFilePath) {
non_covered_com.remove(filePath)
non_covered_test.remove(existedTestFilePath)
}
}
})
console.log(non_covered_com.length, ' non_covered_com: ', non_covered_com)
console.log(non_covered_test.length, ' non_covered_test: ', non_covered_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment