ethers.js/misc/admin/lib/npm.js

84 lines
3.2 KiB
JavaScript
Raw Normal View History

"use strict";
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 });
2021-03-31 02:55:25 +03:00
exports.publish = exports.getPackage = void 0;
2020-10-03 09:20:50 +03:00
const libnpmpublish_1 = require("libnpmpublish");
const semver_1 = __importDefault(require("semver"));
const geturl_1 = require("./geturl");
const local_1 = require("./local");
2020-10-03 09:20:50 +03:00
const log_1 = require("./log");
const cache = {};
function getPackageInfo(name) {
return __awaiter(this, void 0, void 0, function* () {
// Convert dirname to package if needed
2021-10-16 09:29:27 +03:00
name = (0, local_1.getPackage)(name).name;
if (!cache[name]) {
try {
2021-10-16 09:29:27 +03:00
const result = yield (0, geturl_1.getUrl)("https:/\/registry.npmjs.org/" + name);
cache[name] = JSON.parse(Buffer.from(result.body).toString("utf8"));
}
catch (error) {
if (error.status === 404) {
return null;
}
throw error;
}
}
return cache[name] || null;
});
}
function getPackage(name, version) {
return __awaiter(this, void 0, void 0, function* () {
const infos = yield getPackageInfo(name);
if (infos == null) {
return null;
}
if (version == null) {
const versions = Object.keys(infos.versions);
versions.sort(semver_1.default.compare);
version = versions.pop();
}
const info = infos.versions[version];
return {
dependencies: (info.dependencies || {}),
devDependencies: (info.devDependencies || {}),
2020-09-23 05:54:48 +03:00
gitHead: info.gitHead,
location: "remote",
name: info.name,
tarballHash: info.tarballHash,
version: info.version,
_ethers_nobuild: !!info._ethers_nobuild,
};
});
}
exports.getPackage = getPackage;
2020-10-03 09:20:50 +03:00
function publish(path, manifest, options) {
return __awaiter(this, void 0, void 0, function* () {
try {
2021-10-16 09:29:27 +03:00
yield (0, libnpmpublish_1.publish)(path, manifest, options);
2020-10-03 09:20:50 +03:00
}
catch (error) {
// We need an OTP
if (error.code === "EOTP") {
2021-10-16 09:29:27 +03:00
const otp = yield (0, log_1.getPrompt)(log_1.colorify.bold("Enter OTP: "));
2020-10-03 09:20:50 +03:00
options.otp = otp.replace(" ", "");
// Retry with the new OTP
return yield publish(path, manifest, options);
}
throw error;
}
});
}
exports.publish = publish;