"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
const lockfile_1 = __importDefault(require("@yarnpkg/lockfile"));
const compare_versions_1 = __importDefault(require("compare-versions"));
const configuration_1 = __importDefault(require("./configuration"));
const utils_1 = require("./utils");
/**
* NpmVersion checks and compares npm version.
*/
class NpmVersion {
/**
* Create a NpmVersion
* @param {string} name - npm name
* @param {string} version - npm version, e.g. ">= 1.0.0"
*/
constructor(name, version) {
this.name = name;
this.version = version;
}
/**
* Sync to check if the specified npm version in package-lock.json or
* yarn.lock matches npm version comparator.
* @returns {boolean} true if matches, otherwise false.
*/
matchSync() {
if (!configuration_1.default.strict) {
return true;
}
if (!this.packageExistSync()) {
return true;
}
const [operator, version] = this.version.split(" ");
if (this.packageLockExistSync()) {
const packageVersion = this.npmPackageVersionSync();
return compare_versions_1.default.compare(packageVersion, version, operator);
}
if (this.yarnLockExistSync()) {
const packageVersion = this.yarnPackageVersionSync();
return compare_versions_1.default.compare(packageVersion, version, operator);
}
return true;
}
/**
* Async to check if the specified npm version in package-lock.json or
* yarn.lock matches npm version comparator.
* @async
* @returns {Promise<boolean>} true if matches, otherwise false.
*/
async match() {
if (!configuration_1.default.strict) {
return true;
}
if (!(await this.packageExist())) {
return true;
}
const [operator, version] = this.version.split(" ");
if (await this.packageLockExist()) {
const packageVersion = await this.npmPackageVersion();
return compare_versions_1.default.compare(packageVersion, version, operator);
}
if (await this.yarnLockExist()) {
const packageVersion = await this.yarnPackageVersion();
return compare_versions_1.default.compare(packageVersion, version, operator);
}
return true;
}
/**
* Get npm package version.
* @private
* @returns {string}
*/
npmPackageVersionSync() {
const packageLockTree = this.packageLockTreeSync();
if (packageLockTree.packages) {
return packageLockTree.packages[`node_modules/${this.name}`].version;
}
else {
return packageLockTree.dependencies[this.name].version;
}
}
async npmPackageVersion() {
const packageLockTree = await this.packageLockTree();
if (packageLockTree.packages) {
return packageLockTree.packages[`node_modules/${this.name}`].version;
}
else {
return packageLockTree.dependencies[this.name].version;
}
}
/**
* Get yarn package version.
* @private
* @returns {string}
*/
yarnPackageVersionSync() {
const packageTree = this.packageTreeSync();
const yarnLockTree = this.yarnLockTreeSync();
return yarnLockTree[`${this.name}@${packageTree.dependencies[this.name]}`];
}
async yarnPackageVersion() {
const packageTree = await this.packageTree();
const yarnLockTree = await this.yarnLockTree();
return yarnLockTree[`${this.name}@${packageTree.dependencies[this.name]}`];
}
/**
* Get parse result of package.json.
* @private
*/
packageTreeSync() {
return JSON.parse(fs_1.default.readFileSync(this.packagePath(), "utf-8"));
}
async packageTree() {
const content = await fs_1.default.readFileSync(this.packagePath(), "utf-8");
return JSON.parse(content);
}
/**
* Check if package.json exists
* @private
* @returns {boolean}
*/
packageExistSync() {
return (0, utils_1.isValidFileSync)(this.packagePath());
}
async packageExist() {
return (0, utils_1.isValidFile)(this.packagePath());
}
/**
* Get package.json path.
* @private
* @returns {string}
*/
packagePath() {
return path_1.default.join(configuration_1.default.rootPath, "package.json");
}
/**
* Get parse result of package-lock.json.
* @private
*/
packageLockTreeSync() {
return JSON.parse(fs_1.default.readFileSync(this.packageLockPath(), "utf-8"));
}
async packageLockTree() {
const content = await fs_1.promises.readFile(this.packageLockPath(), "utf-8");
return JSON.parse(content);
}
/**
* Check if package-lock.json exists.
* @private
* @returns {boolean}
*/
packageLockExistSync() {
return (0, utils_1.isValidFileSync)(this.packageLockPath());
}
async packageLockExist() {
return await (0, utils_1.isValidFile)(this.packageLockPath());
}
/**
* Get package-lock.json path.
* @private
* @returns {string}
*/
packageLockPath() {
return path_1.default.join(configuration_1.default.rootPath, "package-lock.json");
}
/**
* Get parse result of yarn.lock.
* @private
*/
yarnLockTreeSync() {
return lockfile_1.default.parse(fs_1.default.readFileSync(this.yarnLockPath(), "utf-8"));
}
async yarnLockTree() {
const content = await fs_1.promises.readFile(this.yarnLockPath(), "utf-8");
return lockfile_1.default.parse(content);
}
/**
* Check if yarn.lock exists.
* @private
* @returns {boolean}
*/
yarnLockExistSync() {
return (0, utils_1.isValidFileSync)(this.yarnLockPath());
}
async yarnLockExist() {
return await (0, utils_1.isValidFile)(this.yarnLockPath());
}
/**
* Get yarn.lock path.
* @private
* @returns {string}
*/
yarnLockPath() {
return path_1.default.join(configuration_1.default.rootPath, "yarn.lock");
}
}
exports.default = NpmVersion;