Created
May 24, 2020 09:07
-
-
Save bt4R9/78fb8b804b4e86b34825e57bb3f97567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as glob from 'glob'; | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import * as util from 'util'; | |
const readFile = util.promisify(fs.readFile); | |
const EXP_RE = /(specs\.[-_A-Za-z0-9]+\.[-_A-Za-z0-9]+)/g; | |
export const searchProjectExperiment = () => { | |
return new Promise((resolve, reject) => { | |
glob('./**/*.?(ts|tsx)', {ignore: ['./node_modules/**/*']}, (err, files) => { | |
if (err) { | |
reject(err); | |
return; | |
} | |
const promises: Promise<{file: string, matches: string[]}>[] = files.map(file => { | |
return new Promise((res) => { | |
async function analyzeContent() { | |
try { | |
const content = await readFile(path.join(file), {encoding: 'utf8'}); | |
const matches = content.match(EXP_RE); | |
res({file, matches}); | |
} catch (e) { | |
res({file, matches: []}); | |
} | |
} | |
analyzeContent(); | |
}); | |
}); | |
Promise.all(promises) | |
.then(results => { | |
const matches = results.map(result => result.matches); | |
const result = matches.flat().filter(e => Boolean(e)); | |
resolve(new Set(result)); | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment