one
Version:
One is a new React Framework that makes Vite serve both native and web.
55 lines (54 loc) • 1.8 kB
JavaScript
import fs from "fs/promises";
import { createRequire } from "module";
import path, { dirname } from "path";
import FSExtra from "fs-extra";
async function getAllDependencies(root) {
var depth = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 20;
if (depth === 0) {
return [];
}
var 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) {
var depth = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : Number.POSITIVE_INFINITY;
var pkgJson = await FSExtra.readJson(pkgJsonPath).catch(function (e) {
throw new Error(`Unable to read ${pkgJsonPath}`, {
cause: e
});
});
var dependencies = pkgJson.dependencies ? Object.keys(pkgJson.dependencies) : [];
await Promise.all(dependencies.map(async function (depName) {
try {
var resolved = createRequire(dirname(pkgJsonPath)).resolve(depName);
var subDeps = await getAllDependencies(resolved, depth - 1);
if (subDeps) {
dependencies = [...dependencies, ...subDeps];
}
} catch (err) {}
}));
return [...new Set(dependencies)];
}
async function findClosestPkgJsonPath(dir, predicate) {
if (dir.endsWith("package.json")) {
dir = path.dirname(dir);
}
while (dir) {
var pkg = path.join(dir, "package.json");
try {
var stat = await fs.stat(pkg);
if (stat.isFile() && (!predicate || (await predicate(pkg)))) {
return pkg;
}
} catch (unused) {}
var nextDir = path.dirname(dir);
if (nextDir === dir) break;
dir = nextDir;
}
return void 0;
}
export { getAllDependencies };
//# sourceMappingURL=findDepsToOptimize.native.js.map