16 lines
431 B
TypeScript
16 lines
431 B
TypeScript
|
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;
|
||
|
};
|