UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

45 lines (44 loc) 1.56 kB
import fs from "node:fs/promises"; import { createRequire } from "node:module"; import path, { dirname } from "node:path"; import FSExtra from "fs-extra"; async function getAllDependencies(root, depth = 20) { if (depth === 0) return []; const pkgJsonPath = await findClosestPkgJsonPath(root); if (!pkgJsonPath) throw new Error(`Cannot find package.json from ${root}`); return await crawl(pkgJsonPath, depth - 1); } async function crawl(pkgJsonPath, depth = Number.POSITIVE_INFINITY) { const pkgJson = await FSExtra.readJson(pkgJsonPath).catch((e) => { throw new Error(`Unable to read ${pkgJsonPath}`, { cause: e }); }); let dependencies = pkgJson.dependencies ? Object.keys(pkgJson.dependencies) : []; return await Promise.all( dependencies.map(async (depName) => { try { const resolved = createRequire(dirname(pkgJsonPath)).resolve(depName), subDeps = await getAllDependencies(resolved, depth - 1); subDeps && (dependencies = [...dependencies, ...subDeps]); } catch { } }) ), [...new Set(dependencies)]; } async function findClosestPkgJsonPath(dir, predicate) { for (dir.endsWith("package.json") && (dir = path.dirname(dir)); dir; ) { const pkg = path.join(dir, "package.json"); try { if ((await fs.stat(pkg)).isFile() && (!predicate || await predicate(pkg))) return pkg; } catch { } const nextDir = path.dirname(dir); if (nextDir === dir) break; dir = nextDir; } } export { getAllDependencies }; //# sourceMappingURL=findDepsToOptimize.js.map