export async function findUpPackageJsonDir()

in src/core/utils/file.ts [42:62]


export async function findUpPackageJsonDir(rootPath: string, startPath: string): Promise<string | undefined> {
  if (!rootPath || !startPath) {
    return undefined;
  }

  rootPath = rootPath === "." || rootPath === `.${path.sep}` ? "" : rootPath;
  startPath = path.join(rootPath, startPath);
  const rootPathLength = rootPath.split(/[/\\]/).filter((c) => c).length;
  const find = async (components: string[]): Promise<string | undefined> => {
    if (components.length === 0 || components.length < rootPathLength) {
      return undefined;
    }

    const dir = path.join(...components);
    const packageFile = path.join(dir, "package.json");
    return (await pathExists(packageFile)) ? dir : find(components.slice(0, -1));
  };

  const components = startPath.split(/[/\\]/).filter((c) => c);
  return find(components);
}