puppeteer/src/util/getPackageDirectory.ts

16 lines
431 B
TypeScript
Raw Normal View History

2022-08-17 14:18:33 +00:00
import {existsSync} from 'fs';
import {dirname, join, parse} from 'path';
export const getPackageDirectory = (from: string): string => {
let found = existsSync(join(from, 'package.json'));
const root = parse(from).root;
while (!found) {
if (from === root) {
throw new Error('Cannot find package directory');
}
from = dirname(from);
found = existsSync(join(from, 'package.json'));
}
return from;
};