@aquaori/deplens
Version:
A precise dependency analysis tool for npm and pnpm projects
136 lines • 5.97 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPnpmDependencies = getPnpmDependencies;
const fs_1 = __importDefault(require("fs"));
const js_yaml_1 = __importDefault(require("js-yaml"));
/**
* 获取pnpm项目的依赖信息
* @param args 命令行参数对象
* @param checkCount 检查计数
* @returns 依赖列表和更新后的检查计数
*/
async function getPnpmDependencies(args, checkCount) {
// 确定pnpm依赖锁文件路径
const manifestPath = `${args.path}/pnpm-lock.yaml`;
// 检查锁文件是否存在
if (!fs_1.default.existsSync(manifestPath)) {
const hint = `The ${args.path}/pnpm-lock.yaml file does not exist, so dependencies cannot be resolved.\n> If your project dependencies are managed by npm, please run deplens without the --pnpm or --pn option.`;
throw new Error(hint);
}
// 解析锁文件
const rootManifest = js_yaml_1.default.load(fs_1.default.readFileSync(manifestPath, 'utf-8'));
// 获取pnpm锁文件版本
const lockVersion = rootManifest.lockfileVersion;
// 获取不同类型的依赖
const rootProd = lockVersion == 6
? rootManifest.dependencies || {}
: rootManifest['importers']['.']?.dependencies || {};
const rootPeer = lockVersion == 6
? rootManifest.peerDependencies || {}
: rootManifest['importers']['.']?.peerDependencies || {};
const rootOpt = lockVersion == 6
? rootManifest.optionalDependencies || {}
: rootManifest['importers']['.']?.optionalDependencies || {};
const rootDev = lockVersion == 6
? rootManifest.devDependencies || {}
: rootManifest['importers']['.']?.devDependencies || {};
const depSource = lockVersion == 6
? rootManifest.packages || {}
: rootManifest.snapshots || {};
// 收集被其他包使用的依赖
const usedByOthers = new Map();
for (const [key, pkg] of Object.entries(depSource)) {
if (key === '' || !pkg || typeof pkg !== 'object')
continue;
const p = pkg;
[
p.dependencies || {},
p.peerDependencies || {},
p.optionalDependencies || {}
].forEach(obj => {
Object.entries(obj).forEach(([depName, depRange]) => {
if (typeof depRange !== 'string')
return;
const ver = depRange.replace(/\(.+?\)+/g, '');
if (!usedByOthers.has(depName))
usedByOthers.set(depName, new Set());
usedByOthers.get(depName).add(ver);
checkCount++;
});
});
}
// 构建锁文件中的依赖包列表
const lockFilePkg = [];
const rootDeclared = new Map();
Object.entries({ ...rootProd, ...rootPeer, ...rootOpt, ...rootDev })
.forEach(([name, ver]) => rootDeclared.set(name, ver));
for (const [name, version] of rootDeclared) {
let ignoreList = [];
// 处理配置文件中指定的忽略依赖
if (args['config'] !== "" || fs_1.default.existsSync(`${args.path}/deplens.config.json`)) {
const configPath = args['config'] || `${args.path}/deplens.config.json`;
const config = fs_1.default.readFileSync(configPath, 'utf8');
const ignore = JSON.parse(config).ignoreDep || [];
ignoreList = [...ignoreList, ...ignore];
}
// 处理命令行参数中指定的忽略依赖
if (args['ignoreDep'] !== "") {
const ignoreListFromArgs = args['ignoreDep'].split(',');
ignoreList = [...ignoreList, ...ignoreListFromArgs];
}
if (ignoreList.includes(name))
continue;
// 获取纯净版本号
const pureVersion = version.version.replace(/\(.+?\)+/g, '');
// 处理使用的版本列表
const usedVersions = usedByOthers.get(name);
const usedVersionsList = usedVersions || new Set();
// 检查依赖是否被使用
const isUsed = usedVersionsList.has(pureVersion);
if (!isUsed) {
let preciseVersion = "0";
if (typeof version == "string") {
preciseVersion = version.replace(/\(.+?\)+/g, '');
}
else if (typeof version == "object") {
preciseVersion = version.version.replace(/\(.+?\)+/g, '');
}
const previousPkgIndex = lockFilePkg.findIndex(dep => dep.name == name);
const previousPkg = previousPkgIndex >= 0 ? lockFilePkg[previousPkgIndex] : null;
let previousVersion = previousPkg?.version ?? [];
if (previousPkg !== null && previousVersion !== "") {
if (previousVersion.length != 0 && !previousPkg?.usage) {
previousVersion = [...previousVersion, preciseVersion];
}
else {
previousVersion = [preciseVersion];
}
}
else {
previousVersion = [preciseVersion];
}
if (previousVersion.length != 1) {
previousVersion = [previousVersion.join(" & @")];
}
if (previousPkgIndex >= 0 && previousPkg !== null) {
lockFilePkg[previousPkgIndex].version = previousVersion;
}
else {
lockFilePkg.push({
name,
type: '',
version: previousVersion,
usage: isUsed,
isDev: Object.prototype.hasOwnProperty.call(rootDev, name)
});
}
}
}
// 按名称排序
lockFilePkg.sort((a, b) => a.name.localeCompare(b.name));
return [lockFilePkg, checkCount];
}
//# sourceMappingURL=pnpm.js.map