snyk-nodejs-lockfile-parser
Version:
Generate a dep tree given a lockfile
40 lines • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPnpmLockfileParser = getPnpmLockfileParser;
const lockfile_v6_1 = require("./lockfile-v6");
const lockfile_v5_1 = require("./lockfile-v5");
const lockfile_v9_1 = require("./lockfile-v9");
const error_catalog_nodejs_public_1 = require("@snyk/error-catalog-nodejs-public");
const utils_1 = require("../../../utils");
function getPnpmLockfileParser(pnpmLockContent, lockfileVersion, workspaceArgs) {
// In case of no dependencies, pnpm@7 (lokfile version 5)
// does not create a lockfile at `pnpm install`
// so if there is no lockfile content, default to lockfile version 5
if (!pnpmLockContent) {
return new lockfile_v5_1.LockfileV5Parser(pnpmLockContent, workspaceArgs);
}
// pnpm 11+ may prepend an env/config document to pnpm-lock.yaml;
// only the dependency document is parsed. An env-only lockfile has no
// dependency document and is treated the same as no lockfile.
const mainDocument = (0, utils_1.extractPnpmMainDocument)(pnpmLockContent);
if (!mainDocument) {
return new lockfile_v5_1.LockfileV5Parser(undefined, workspaceArgs);
}
const rawPnpmLock = (0, utils_1.loadYamlMappingOrThrow)(mainDocument, 'pnpm-lock.yaml');
const version = rawPnpmLock.lockfileVersion;
const versionStr = typeof version === 'string' ? version : '';
if (lockfileVersion === utils_1.NodeLockfileVersion.PnpmLockV5 ||
versionStr.startsWith('5')) {
return new lockfile_v5_1.LockfileV5Parser(rawPnpmLock, workspaceArgs);
}
if (lockfileVersion === utils_1.NodeLockfileVersion.PnpmLockV6 ||
versionStr.startsWith('6')) {
return new lockfile_v6_1.LockfileV6Parser(rawPnpmLock, workspaceArgs);
}
if (lockfileVersion === utils_1.NodeLockfileVersion.PnpmLockV9 ||
versionStr.startsWith('9')) {
return new lockfile_v9_1.LockfileV9Parser(rawPnpmLock, workspaceArgs);
}
throw new error_catalog_nodejs_public_1.OpenSourceEcosystems.PnpmUnsupportedLockfileVersionError(`The pnpm-lock.yaml lockfile version ${lockfileVersion ?? version} is not supported`);
}
//# sourceMappingURL=index.js.map