UNPKG

@lerna/publish

Version:

Publish packages in the current project

245 lines (244 loc) 8.07 kB
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 package_exports = {}; __export(package_exports, { Package: () => Package }); module.exports = __toCommonJS(package_exports); var import_load_json_file = __toESM(require("load-json-file")); var import_npm_package_arg = __toESM(require("npm-package-arg")); var import_path = __toESM(require("path")); var import_write_pkg = __toESM(require("write-pkg")); const PKG = Symbol("pkg"); const _location = Symbol("location"); const _resolved = Symbol("resolved"); const _rootPath = Symbol("rootPath"); const _scripts = Symbol("scripts"); const _contents = Symbol("contents"); function binSafeName({ name, scope }) { return scope ? name.substring(scope.length + 1) : name; } function shallowCopy(json) { return Object.keys(json).reduce((obj, key) => { const val = json[key]; if (Array.isArray(val)) { obj[key] = val.slice(); } else if (val && typeof val === "object") { obj[key] = Object.assign({}, val); } else { obj[key] = val; } return obj; }, {}); } class Package { name; [PKG]; [_location]; [_resolved]; [_rootPath]; [_scripts]; [_contents]; /** * Create a Package instance from parameters, possibly reusing existing instance. * @param ref A path to a package.json file, Package instance, or JSON object * @param [dir] If `ref` is a JSON object, this is the location of the manifest */ static lazy(ref, dir = ".") { if (typeof ref === "string") { const location = import_path.default.resolve(import_path.default.basename(ref) === "package.json" ? import_path.default.dirname(ref) : ref); const manifest = import_load_json_file.default.sync(import_path.default.join(location, "package.json")); return new Package(manifest, location); } if ("__isLernaPackage" in ref) { return ref; } return new Package(ref, dir); } constructor(pkg, location, rootPath = location) { const resolved = import_npm_package_arg.default.resolve(pkg.name, `file:${import_path.default.relative(rootPath, location)}`, rootPath); this.name = pkg.name; this[PKG] = pkg; Object.defineProperty(this, PKG, { enumerable: false, writable: true }); this[_location] = location; this[_resolved] = resolved; this[_rootPath] = rootPath; this[_scripts] = { ...pkg.scripts }; } // readonly getters get location() { return this[_location]; } get private() { return Boolean(this[PKG].private); } set private(isPrivate) { this[PKG].private = isPrivate; } get resolved() { return this[_resolved]; } get rootPath() { return this[_rootPath]; } get scripts() { return this[_scripts]; } get bin() { const pkg = this[PKG]; return typeof pkg.bin === "string" ? { // See note on function implementation // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore [binSafeName(this.resolved)]: pkg.bin } : Object.assign({}, pkg.bin); } get binLocation() { return import_path.default.join(this.location, "node_modules", ".bin"); } get manifestLocation() { return import_path.default.join(this.location, "package.json"); } get nodeModulesLocation() { return import_path.default.join(this.location, "node_modules"); } // eslint-disable-next-line class-methods-use-this get __isLernaPackage() { return true; } // accessors get version() { return this[PKG].version; } set version(version) { this[PKG].version = version; } get contents() { if (this[_contents]) { return this[_contents]; } if (this[PKG].publishConfig && this[PKG].publishConfig.directory) { return import_path.default.join(this.location, this[PKG].publishConfig.directory); } return this.location; } set contents(subDirectory) { this[_contents] = import_path.default.join(this.location, subDirectory); } // "live" collections get dependencies() { return this[PKG].dependencies; } get devDependencies() { return this[PKG].devDependencies; } get optionalDependencies() { return this[PKG].optionalDependencies; } get peerDependencies() { return this[PKG].peerDependencies; } /** * Map-like retrieval of arbitrary values */ get(key) { return this[PKG][key]; } /** * Map-like storage of arbitrary values */ set(key, val) { this[PKG][key] = val; return this; } /** * Provide shallow copy for munging elsewhere */ toJSON() { return shallowCopy(this[PKG]); } /** * Refresh internal state from disk (e.g., changed by external lifecycles) */ refresh() { return (0, import_load_json_file.default)(this.manifestLocation).then((pkg) => { this[PKG] = pkg; return this; }); } /** * Write manifest changes to disk * @returns {Promise} resolves when write finished */ serialize() { return (0, import_write_pkg.default)(this.manifestLocation, this[PKG]).then(() => this); } /** * Mutate local dependency spec according to type * @param resolved npa metadata * @param depVersion semver * @param savePrefix npm_config_save_prefix */ updateLocalDependency(resolved, depVersion, savePrefix, options = { retainWorkspacePrefix: true }) { const depName = resolved.name; let depCollection = this.dependencies; if (!depCollection || !depCollection[depName]) { depCollection = this.optionalDependencies; } if (!depCollection || !depCollection[depName]) { depCollection = this.devDependencies; } if (resolved.workspaceSpec && options.retainWorkspacePrefix) { if (!resolved.workspaceAlias) { const workspacePrefix = resolved.workspaceSpec.match(/^(workspace:[*~^]?)/)[0]; depCollection[depName] = `${workspacePrefix}${depVersion}`; } } else if (resolved.registry || resolved.type === "directory") { depCollection[depName] = `${savePrefix}${depVersion}`; } else if (resolved.gitCommittish) { const [tagPrefix] = /^\D*/.exec(resolved.gitCommittish); const { hosted } = resolved; hosted.committish = `${tagPrefix}${depVersion}`; depCollection[depName] = hosted.toString({ noGitPlus: false, noCommittish: false }); } else if (resolved.gitRange) { const { hosted } = resolved; hosted.committish = `semver:${savePrefix}${depVersion}`; depCollection[depName] = hosted.toString({ noGitPlus: false, noCommittish: false }); } } /** * Remove the private property, effectively making the package public. */ removePrivate() { delete this[PKG].private; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Package });