@lerna/publish
Version:
Publish packages in the current project
253 lines (252 loc) • 9.07 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var conf_exports = {};
__export(conf_exports, {
Conf: () => Conf
});
module.exports = __toCommonJS(conf_exports);
var import_assert = __toESM(require("assert"));
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_env_replace = require("./env-replace");
var import_find_prefix = require("./find-prefix");
var import_parse_field = require("./parse-field");
var import_nerf_dart = require("./nerf-dart");
const { ConfigChain } = require("config-chain");
class Conf extends ConfigChain {
root;
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L208-L222
constructor(base) {
super(base);
this.root = base;
}
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L332-L342
add(data, marker) {
try {
for (const x of Object.keys(data)) {
const newKey = (0, import_env_replace.envReplace)(x);
const newField = (0, import_parse_field.parseField)(data[x], newKey);
delete data[x];
data[newKey] = newField;
}
} catch (err) {
throw err;
}
return super.add(data, marker);
}
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L312-L325
addFile(file, name = file) {
const marker = { __source__: name };
this["sources"][name] = { path: file, type: "ini" };
this["push"](marker);
this["_await"]();
try {
const contents = import_fs.default.readFileSync(file, "utf8");
this["addString"](contents, file, "ini", marker);
} catch (err) {
this["add"]({}, marker);
}
return this;
}
// https://github.com/npm/npm/blob/latest/lib/config/core.js#L344-L360
addEnv(env = process.env) {
const conf = {};
Object.keys(env).filter((x) => /^npm_config_/i.test(x)).forEach((x) => {
if (!env[x]) {
return;
}
const p = x.toLowerCase().replace(/^npm_config_/, "").replace(/(?!^)_/g, "-");
conf[p] = env[x];
});
return super.addEnv("", conf, "env");
}
// https://github.com/npm/npm/blob/latest/lib/config/load-prefix.js
loadPrefix() {
const cli = this["list"][0];
Object.defineProperty(this, "prefix", {
enumerable: true,
set: (prefix) => {
const g = this["get"]("global");
this[g ? "globalPrefix" : "localPrefix"] = prefix;
},
get: () => {
const g = this["get"]("global");
return g ? this["globalPrefix"] : this["localPrefix"];
}
});
Object.defineProperty(this, "globalPrefix", {
enumerable: true,
set: (prefix) => {
this["set"]("prefix", prefix);
},
get: () => import_path.default.resolve(this["get"]("prefix"))
});
let p;
Object.defineProperty(this, "localPrefix", {
enumerable: true,
set: (prefix) => {
p = prefix;
},
get: () => p
});
if (Object.prototype.hasOwnProperty.call(cli, "prefix")) {
p = import_path.default.resolve(cli.prefix);
} else {
try {
p = (0, import_find_prefix.findPrefix)(process.cwd());
} catch (err) {
throw err;
}
}
return p;
}
// https://github.com/npm/npm/blob/latest/lib/config/load-cafile.js
loadCAFile(file) {
if (!file) {
return;
}
try {
const contents = import_fs.default.readFileSync(file, "utf8");
const delim = "-----END CERTIFICATE-----";
const output = contents.split(delim).filter((x) => Boolean(x.trim())).map((x) => x.trimLeft() + delim);
this["set"]("ca", output);
} catch (err) {
if (err.code === "ENOENT") {
return;
}
throw err;
}
}
// https://github.com/npm/npm/blob/latest/lib/config/set-user.js
loadUser() {
const defConf = this.root;
if (this["get"]("global")) {
return;
}
if (process.env["SUDO_UID"]) {
defConf.user = Number(process.env["SUDO_UID"]);
return;
}
const prefix = import_path.default.resolve(this["get"]("prefix"));
try {
const stats = import_fs.default.statSync(prefix);
defConf.user = stats.uid;
} catch (err) {
if (err.code === "ENOENT") {
return;
}
throw err;
}
}
// https://github.com/npm/npm/blob/24ec9f2/lib/config/get-credentials-by-uri.js
getCredentialsByURI(uri) {
(0, import_assert.default)(uri && typeof uri === "string", "registry URL is required");
const nerfed = (0, import_nerf_dart.toNerfDart)(uri);
const defnerf = (0, import_nerf_dart.toNerfDart)(this["get"]("registry"));
const c = {
scope: nerfed,
token: void 0,
password: void 0,
username: void 0,
email: void 0,
auth: void 0,
alwaysAuth: void 0
};
if (this["get"](`${nerfed}:always-auth`) !== void 0) {
const val = this["get"](`${nerfed}:always-auth`);
c.alwaysAuth = val === "false" ? false : !!val;
} else if (this["get"]("always-auth") !== void 0) {
c.alwaysAuth = this["get"]("always-auth");
}
if (this["get"](`${nerfed}:_authToken`)) {
c.token = this["get"](`${nerfed}:_authToken`);
return c;
}
let authDef = this["get"]("_auth");
let userDef = this["get"]("username");
let passDef = this["get"]("_password");
if (authDef && !(userDef && passDef)) {
authDef = Buffer.from(authDef, "base64").toString();
authDef = authDef.split(":");
userDef = authDef.shift();
passDef = authDef.join(":");
}
if (this["get"](`${nerfed}:_password`)) {
c.password = Buffer.from(this["get"](`${nerfed}:_password`), "base64").toString("utf8");
} else if (nerfed === defnerf && passDef) {
c.password = passDef;
}
if (this["get"](`${nerfed}:username`)) {
c.username = this["get"](`${nerfed}:username`);
} else if (nerfed === defnerf && userDef) {
c.username = userDef;
}
if (this["get"](`${nerfed}:email`)) {
c.email = this["get"](`${nerfed}:email`);
} else if (this["get"]("email")) {
c.email = this["get"]("email");
}
if (c.username && c.password) {
c.auth = Buffer.from(`${c.username}:${c.password}`).toString("base64");
}
return c;
}
// https://github.com/npm/npm/blob/24ec9f2/lib/config/set-credentials-by-uri.js
setCredentialsByURI(uri, c) {
(0, import_assert.default)(uri && typeof uri === "string", "registry URL is required");
(0, import_assert.default)(c && typeof c === "object", "credentials are required");
const nerfed = (0, import_nerf_dart.toNerfDart)(uri);
if (c.token) {
this["set"](`${nerfed}:_authToken`, c.token, "user");
this["del"](`${nerfed}:_password`, "user");
this["del"](`${nerfed}:username`, "user");
this["del"](`${nerfed}:email`, "user");
this["del"](`${nerfed}:always-auth`, "user");
} else if (c.username || c.password || c.email) {
(0, import_assert.default)(c.username, "must include username");
(0, import_assert.default)(c.password, "must include password");
(0, import_assert.default)(c.email, "must include email address");
this["del"](`${nerfed}:_authToken`, "user");
const encoded = Buffer.from(c.password, "utf8").toString("base64");
this["set"](`${nerfed}:_password`, encoded, "user");
this["set"](`${nerfed}:username`, c.username, "user");
this["set"](`${nerfed}:email`, c.email, "user");
if (c.alwaysAuth !== void 0) {
this["set"](`${nerfed}:always-auth`, c.alwaysAuth, "user");
} else {
this["del"](`${nerfed}:always-auth`, "user");
}
} else {
throw new Error("No credentials to set.");
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Conf
});