UNPKG

@lerna/publish

Version:

Publish packages in the current project

262 lines (261 loc) 9.81 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 project_exports = {}; __export(project_exports, { Project: () => Project, getPackages: () => getPackages, getPackagesSync: () => getPackagesSync }); module.exports = __toCommonJS(project_exports); var import_devkit = require("@nrwl/devkit"); var import_cosmiconfig = require("cosmiconfig"); var import_dedent = __toESM(require("dedent")); var import_fs = __toESM(require("fs")); var import_glob_parent = __toESM(require("glob-parent")); var import_globby = __toESM(require("globby")); var import_js_yaml = require("js-yaml"); var import_load_json_file = __toESM(require("load-json-file")); var import_npmlog = __toESM(require("npmlog")); var import_p_map = __toESM(require("p-map")); var import_path = __toESM(require("path")); var import_package = require("../package"); var import_validation_error = require("../validation-error"); var import_apply_extends = require("./apply-extends"); var import_deprecate_config = require("./deprecate-config"); var import_make_file_finder = require("./make-file-finder"); class Project { static PACKAGE_GLOB = "packages/*"; static LICENSE_GLOB = "LICEN{S,C}E{,.*}"; static getPackages(cwd) { return new Project(cwd).getPackages(); } static getPackagesSync(cwd) { return new Project(cwd).getPackagesSync(); } config; configNotFound; rootConfigLocation; rootPath; constructor(cwd) { const explorer = (0, import_cosmiconfig.cosmiconfigSync)("lerna", { searchPlaces: ["lerna.json", "package.json"], transform(obj) { if (!obj) { return { // No need to distinguish between missing and empty, // saves a lot of noisy guards elsewhere config: {}, configNotFound: true, // path.resolve(".", ...) starts from process.cwd() filepath: import_path.default.resolve(cwd || ".", "lerna.json") }; } (0, import_deprecate_config.deprecateConfig)(obj.config, obj.filepath); obj.config = (0, import_apply_extends.applyExtends)(obj.config, import_path.default.dirname(obj.filepath)); return obj; } }); let loaded; try { loaded = explorer.search(cwd); } catch (err) { if (err.name === "JSONError") { throw new import_validation_error.ValidationError(err.name, err.message); } throw err; } this.config = loaded.config; this.configNotFound = loaded.configNotFound; this.rootConfigLocation = loaded.filepath; this.rootPath = import_path.default.dirname(loaded.filepath); import_npmlog.default.verbose("rootPath", this.rootPath); } get version() { return this.config.version; } set version(val) { this.config.version = val; } get packageConfigs() { if (this.config.npmClient === "pnpm") { import_npmlog.default.verbose( "packageConfigs", "Package manager 'pnpm' detected. Resolving packages using 'pnpm-workspace.yaml'." ); const workspaces = this.pnpmWorkspaceConfig.packages; if (!workspaces) { throw new import_validation_error.ValidationError( "EWORKSPACES", "No 'packages' property found in pnpm-workspace.yaml. See https://pnpm.io/workspaces for help configuring workspaces in pnpm." ); } return workspaces; } if (this.config.useWorkspaces) { const workspaces = this.manifest.get("workspaces"); if (!workspaces) { throw new import_validation_error.ValidationError( "EWORKSPACES", import_dedent.default` Workspaces need to be defined in the root package.json. See: https://github.com/lerna/lerna/blob/master/commands/bootstrap/README.md#--use-workspaces ` ); } return workspaces.packages || workspaces; } if (this.manifest.get("workspaces")) { import_npmlog.default.warn( "EWORKSPACES", import_dedent.default` Workspaces exist in the root package.json, but Lerna is not configured to use them. To fix this and have Lerna use workspaces to resolve packages, set \`useWorkspaces: true\` in lerna.json. ` ); } if (this.config.packages) { return this.config.packages; } import_npmlog.default.warn( "EPACKAGES", `No packages defined in lerna.json. Defaulting to packages in ${Project.PACKAGE_GLOB}` ); return [Project.PACKAGE_GLOB]; } get packageParentDirs() { return this.packageConfigs.map(import_glob_parent.default).map((parentDir) => import_path.default.resolve(this.rootPath, parentDir)); } get manifest() { let manifest; try { const manifestLocation = import_path.default.join(this.rootPath, "package.json"); const packageJson = import_load_json_file.default.sync(manifestLocation); if (!packageJson.name) { packageJson.name = import_path.default.basename(import_path.default.dirname(manifestLocation)); } manifest = new import_package.Package(packageJson, this.rootPath); Object.defineProperty(this, "manifest", { value: manifest }); } catch (err) { if (err.name === "JSONError") { throw new import_validation_error.ValidationError(err.name, err.message); } } return manifest; } /** @type {PnpmWorkspaceConfig} */ get pnpmWorkspaceConfig() { let config; try { const configLocation = import_path.default.join(this.rootPath, "pnpm-workspace.yaml"); const configContent = import_fs.default.readFileSync(configLocation); config = (0, import_js_yaml.load)(configContent); Object.defineProperty(this, "pnpmWorkspaceConfig", { value: config }); } catch (err) { if (err.message.includes("ENOENT: no such file or directory")) { throw new import_validation_error.ValidationError( "ENOENT", "No pnpm-workspace.yaml found. See https://pnpm.io/workspaces for help configuring workspaces in pnpm." ); } throw new import_validation_error.ValidationError(err.name, err.message); } return config; } get licensePath() { let licensePath; try { const search = import_globby.default.sync(Project.LICENSE_GLOB, { cwd: this.rootPath, absolute: true, caseSensitiveMatch: false, // Project license is always a sibling of the root manifest deep: 0 }); licensePath = search.shift(); if (licensePath) { licensePath = import_path.default.normalize(licensePath); Object.defineProperty(this, "licensePath", { value: licensePath }); } } catch (err) { throw new import_validation_error.ValidationError(err.name, err.message); } return licensePath; } get fileFinder() { const finder = (0, import_make_file_finder.makeFileFinder)(this.rootPath, this.packageConfigs); Object.defineProperty(this, "fileFinder", { value: finder }); return finder; } /** * A promise resolving to a list of Package instances */ getPackages() { const mapper = (packageConfigPath) => (0, import_load_json_file.default)(packageConfigPath).then( (packageJson) => new import_package.Package(packageJson, import_path.default.dirname(packageConfigPath), this.rootPath) ); return this.fileFinder("package.json", (filePaths) => (0, import_p_map.default)(filePaths, mapper, { concurrency: 50 })); } /** * A list of Package instances */ getPackagesSync() { return (0, import_make_file_finder.makeSyncFileFinder)(this.rootPath, this.packageConfigs)("package.json", (packageConfigPath) => { return new import_package.Package( import_load_json_file.default.sync(packageConfigPath), import_path.default.dirname(packageConfigPath), this.rootPath ); }); } getPackageLicensePaths() { return this.fileFinder(Project.LICENSE_GLOB, null, { caseSensitiveMatch: false }); } isIndependent() { return this.version === "independent"; } serializeConfig() { (0, import_devkit.writeJsonFile)(this.rootConfigLocation, this.config, { spaces: 2 }); return this.rootConfigLocation; } } const getPackages = Project.getPackages; const getPackagesSync = Project.getPackagesSync; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Project, getPackages, getPackagesSync });