license-kit
Version:
Aggregate license notes of OSS libraries used in your Node.js project, analyze & visualize OSS licenses with AI-turbocharged tooling
54 lines (53 loc) • 2.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProjectPaths = getProjectPaths;
exports.getPackageLockChecksum = getPackageLockChecksum;
exports.getLockfilePath = getLockfilePath;
const node_crypto_1 = require("node:crypto");
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
function getProjectPaths(options) {
const repoRootPath = node_path_1.default.resolve(options.root);
const packageJsonPath = node_path_1.default.join(repoRootPath, 'package.json');
if (!node_fs_1.default.existsSync(packageJsonPath)) {
console.error(`package.json not found at ${packageJsonPath}`);
process.exit(1);
}
return { packageJsonPath, repoRootPath };
}
function getPackageLockChecksum(options) {
return (0, node_crypto_1.createHash)('sha256')
.update(node_fs_1.default.readFileSync(getLockfilePath(options), 'utf8'))
.digest('hex');
}
function getLockfilePath(options) {
const { repoRootPath } = getProjectPaths(options);
let pthBase = repoRootPath;
while (pthBase !== node_path_1.default.dirname(pthBase)) {
let pth = node_path_1.default.join(pthBase, 'package-lock.json');
if (node_fs_1.default.existsSync(pth)) {
return pth;
}
pth = node_path_1.default.join(pthBase, 'pnpm-lock.yaml');
if (node_fs_1.default.existsSync(pth)) {
return pth;
}
pth = node_path_1.default.join(pthBase, 'yarn.lock');
if (node_fs_1.default.existsSync(pth)) {
return pth;
}
pth = node_path_1.default.join(pthBase, 'bun.lock');
if (node_fs_1.default.existsSync(pth)) {
return pth;
}
pth = node_path_1.default.join(pthBase, 'bun.lockb');
if (node_fs_1.default.existsSync(pth)) {
return pth;
}
pthBase = node_path_1.default.dirname(pthBase);
}
throw new Error('No lockfile found');
}