UNPKG

eyeglass

Version:
52 lines 1.7 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const perf_1 = require("../util/perf"); const path = __importStar(require("path")); const URI_1 = require("./URI"); let PACKAGE_JSON = "package.json"; function getPackageData(pkgPath) { try { return require(pkgPath); } catch (e) { /* istanbul ignore next - not really worth writing a test for */ return null; } } exports.getPackageData = getPackageData; function getPackage(dir) { let pkgPath = getPackagePath(dir); return { path: pkgPath, data: getPackageData(pkgPath) }; } exports.getPackage = getPackage; function getPackagePath(dir) { dir = URI_1.URI.system(dir); return (path.basename(dir) === PACKAGE_JSON) ? dir : path.join(dir, PACKAGE_JSON); } exports.getPackagePath = getPackagePath; function findNearestPackage(dir) { let originalDir = dir; dir = URI_1.URI.system(dir); let prevDir; while (dir !== prevDir) { if (perf_1.existsSync(getPackagePath(dir))) { return dir; } prevDir = dir; dir = path.dirname(dir); } /* istanbul ignore next - should never happen and not possible to test */ throw new Error(`No package.json file found in directory ${originalDir} or in any of the parent directories.`); } exports.findNearestPackage = findNearestPackage; //# sourceMappingURL=package.js.map