"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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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.
*/
match() {
return __awaiter(this, void 0, void 0, function* () {
if (!configuration_1.default.strict) {
return true;
}
if (!(yield this.packageExist())) {
return true;
}
const [operator, version] = this.version.split(" ");
if (yield this.packageLockExist()) {
const packageVersion = yield this.npmPackageVersion();
return compare_versions_1.default.compare(packageVersion, version, operator);
}
if (yield this.yarnLockExist()) {
const packageVersion = yield 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;
}
}
npmPackageVersion() {
return __awaiter(this, void 0, void 0, function* () {
const packageLockTree = yield 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]}`];
}
yarnPackageVersion() {
return __awaiter(this, void 0, void 0, function* () {
const packageTree = yield this.packageTree();
const yarnLockTree = yield 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"));
}
packageTree() {
return __awaiter(this, void 0, void 0, function* () {
const content = yield 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());
}
packageExist() {
return __awaiter(this, void 0, void 0, function* () {
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"));
}
packageLockTree() {
return __awaiter(this, void 0, void 0, function* () {
const content = yield 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());
}
packageLockExist() {
return __awaiter(this, void 0, void 0, function* () {
return yield (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"));
}
yarnLockTree() {
return __awaiter(this, void 0, void 0, function* () {
const content = yield 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());
}
yarnLockExist() {
return __awaiter(this, void 0, void 0, function* () {
return yield (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;