Skip to content

Commit

Permalink
feat: findUpFile to node utils
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Nov 24, 2020
1 parent 57e1d6f commit bfcde64
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/core/src/node-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'fs';
import path from 'path';

export const findUpFile = (
filePath: string,
fileName: string | string[],
levels: number = 10,
): string | null => {
const files = fs.readdirSync(filePath);
if (levels === 0) {
return null;
}
const arFiles: string[] = Array.isArray(fileName) ? fileName : [fileName];
const pckg = files.find(file => arFiles.includes(file));
if (pckg) {
return path.resolve(filePath, pckg);
}
return findUpFile(path.resolve(filePath, '..'), fileName, levels - 1);
};

0 comments on commit bfcde64

Please sign in to comment.