Skip to content

Instantly share code, notes, and snippets.

@bt4R9
Created May 24, 2020 09:07
Show Gist options
  • Save bt4R9/78fb8b804b4e86b34825e57bb3f97567 to your computer and use it in GitHub Desktop.
Save bt4R9/78fb8b804b4e86b34825e57bb3f97567 to your computer and use it in GitHub Desktop.
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