2020-09-21 08:14:06 +03:00
|
|
|
|
2020-10-03 09:20:50 +03:00
|
|
|
import { Options, publish as npmPublish } from "libnpmpublish";
|
2020-09-21 08:14:06 +03:00
|
|
|
import semver from "semver";
|
|
|
|
|
|
|
|
import { getUrl } from "./geturl";
|
|
|
|
import { Package, getPackage as _getPackage } from "./local";
|
2020-10-03 09:20:50 +03:00
|
|
|
import { colorify, getPrompt } from "./log";
|
2020-09-21 08:14:06 +03:00
|
|
|
|
|
|
|
|
|
|
|
const cache: Record<string, any> = { };
|
|
|
|
|
|
|
|
async function getPackageInfo(name: string): Promise<any> {
|
|
|
|
// Convert dirname to package if needed
|
|
|
|
name = _getPackage(name).name;
|
|
|
|
|
|
|
|
if (!cache[name]) {
|
|
|
|
try {
|
2021-10-16 05:03:35 +03:00
|
|
|
const result = await getUrl("https:/\/registry.npmjs.org/" + name);
|
2020-09-21 08:14:06 +03:00
|
|
|
cache[name] = JSON.parse(Buffer.from(result.body).toString("utf8"));
|
|
|
|
} catch (error) {
|
|
|
|
if (error.status === 404) { return null; }
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cache[name] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPackage(name: string, version?: string): Promise<Package> {
|
|
|
|
const infos = await getPackageInfo(name);
|
|
|
|
if (infos == null) { return null; }
|
|
|
|
|
|
|
|
if (version == null) {
|
|
|
|
const versions = Object.keys(infos.versions);
|
|
|
|
versions.sort(semver.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,
|
2020-09-21 08:14:06 +03:00
|
|
|
location: "remote",
|
|
|
|
name: info.name,
|
|
|
|
tarballHash: info.tarballHash,
|
|
|
|
version : info.version,
|
|
|
|
_ethers_nobuild: !!info._ethers_nobuild,
|
|
|
|
};
|
|
|
|
}
|
2020-10-03 09:20:50 +03:00
|
|
|
|
|
|
|
export async function publish(path: string, manifest: any, options: Options): Promise<void> {
|
|
|
|
try {
|
|
|
|
await npmPublish(path, manifest, options);
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
// We need an OTP
|
|
|
|
if (error.code === "EOTP") {
|
|
|
|
const otp = await getPrompt(colorify.bold("Enter OTP: "));
|
|
|
|
options.otp = otp.replace(" ", "");
|
|
|
|
|
|
|
|
// Retry with the new OTP
|
|
|
|
return await publish(path, manifest, options);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|