UNPKG

@cdwr/core

Version:

A set of core utilities for the Codeware ecosystem.

330 lines (322 loc) 11.5 kB
"use strict"; 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); // packages/core/src/release.ts var release_exports = {}; __export(release_exports, { changelogs: () => changelogs, publish: () => publish, release: () => release }); module.exports = __toCommonJS(release_exports); // packages/core/src/lib/release/changelogs.ts var import_chalk = __toESM(require("chalk"), 1); var import_release = require("nx/release"); var changelogs = async (options) => { const { versionData, dryRun, verbose } = options; console.log(`${import_chalk.default.magenta.underline("Generate changelogs")}`); try { await (0, import_release.releaseChangelog)({ versionData, dryRun, verbose }); return true; } catch (error) { console.error( `Generate changelogs: ${import_chalk.default.red(error.message)}` ); return false; } }; // packages/core/src/lib/release/publish.ts var import_child_process = require("child_process"); var import_util = require("util"); var import_devkit = require("@nx/devkit"); var import_chalk2 = __toESM(require("chalk"), 1); var import_release2 = require("nx/release"); var publish = async (options) => { const { otp, dryRun, verbose } = options; console.log(`${import_chalk2.default.magenta.underline("Publish packages")} `); try { const pm = (0, import_devkit.getPackageManagerCommand)(); const { stdout } = await (0, import_util.promisify)(import_child_process.exec)(`${pm.exec} nx run-many -t build`); console.log(stdout); const result = await (0, import_release2.releasePublish)({ dryRun, verbose, otp }); const total = Object.values(result).length; const successful = Object.values(result).filter((r) => r.code === 0).length; return { successful, total }; } catch (error) { console.error(`Publish packages: ${import_chalk2.default.red(error.message)}`); return null; } }; // packages/core/src/lib/release/release.ts var import_prompts = require("@clack/prompts"); var import_devkit3 = require("@nx/devkit"); var import_chalk4 = __toESM(require("chalk"), 1); var import_release3 = require("nx/release"); var import_simple_git = require("simple-git"); // packages/core/src/lib/utils/whoami.ts var import_npm_whoami = __toESM(require("npm-whoami"), 1); async function whoami() { return new Promise((resolve) => { (0, import_npm_whoami.default)((err, user) => { if (err || !user) { resolve(""); } else { resolve(user); } }); }); } // packages/core/src/lib/release/revert-package-json.ts var import_devkit2 = require("@nx/devkit"); var import_chalk3 = __toESM(require("chalk"), 1); var revertPackageJson = (originPackageFile, projectsVersionData) => { if (!projectsVersionData["nx-payload"].newVersion) { return; } if (!originPackageFile.devDependencies || !originPackageFile.devDependencies["@cdwr/nx-payload"]) { return; } console.log( `${import_chalk3.default.yellow(`\u26A0\uFE0F Nx auto-update workspace package.json when nx-payload has a new version. These changes will be reverted to make the update in another context. `)}` ); const packageFile = (0, import_devkit2.readJsonFile)("package.json"); if (packageFile.devDependencies) { packageFile.version = originPackageFile.version; packageFile.devDependencies["@cdwr/nx-payload"] = originPackageFile.devDependencies["@cdwr/nx-payload"]; (0, import_devkit2.writeJsonFile)("package.json", packageFile, { appendNewLine: true }); } }; // packages/core/src/lib/release/release.ts var dryRunOutro = () => (0, import_prompts.outro)( `\u{1F453} ${import_chalk4.default.green("Done!")} Nothing gets changed when running in ${import_chalk4.default.bgYellow(" preview ")} mode` ); var git = (0, import_simple_git.simpleGit)(); var release = async () => { (0, import_prompts.intro)(`Let's release some Nx Plugin packages \u{1F4E6}`); const release2 = await (0, import_prompts.group)( { mode: () => (0, import_prompts.select)({ message: "What parts of the release process do you want to run?", options: [ { value: "release", label: `Default release process \u{1F4AB}`, hint: "analyze commits, create changelog and publish" }, { value: "publish", label: "Publish a release \u{1F4E6}", hint: "release must have been pre-generated earlier" } ], initialValue: "release" }), postponePublish: ({ results: { mode: mode2 } }) => { if (mode2 === "publish") { return Promise.resolve("false"); } return (0, import_prompts.select)({ message: "Do you want GitHub Actions to publish the packages to NPM?", options: [ { value: "true", label: "Yes, let GitHub Actions do it", hint: "recommended" }, { value: "false", label: "No, publish the packages directly" } ], initialValue: "true" }); }, dryRun: () => (0, import_prompts.select)({ message: "Do you want to see a preview before making any changes?", options: [ { value: "true", label: `Yes, just a preview \u{1F913}`, hint: "recommended before the actual run" }, { value: "false", label: "No, run the selected process \u{1F680}" } ], initialValue: "true" }), otp: ({ results: { dryRun: dryRun2, mode: mode2, postponePublish: postponePublish2 } }) => { const modeT = mode2; if (dryRun2 === "false" && (modeT === "publish" || modeT === "release" && postponePublish2 === "false")) { return (0, import_prompts.text)({ message: "Enter NPM OTP code from your 2FA app:", validate: (value) => { if (!value) { return "OTP code is required"; } if (!/^\d{6}$/.test(value)) { return "OTP code must be a 6-digit number"; } return void 0; } }); } return; }, verbose: () => (0, import_prompts.confirm)({ message: "Do you want to enable verbose logging?", active: "Verbose logging", inactive: "Normal logging", initialValue: false }), confirmRelease: ({ results: { dryRun: dryRun2 } }) => { if (dryRun2 === "true") { return; } return (0, import_prompts.confirm)({ message: `\u270B You will make changes! Are you sure?`, active: "Yes, do it!", inactive: `No, I'm not ready yet` }); } }, { // On Cancel callback that wraps the group // So if the user cancels one of the prompts in the group this function will be called onCancel: () => { (0, import_prompts.cancel)("\u{1F6AB} Release cancelled."); return 0; } } ); const { confirmRelease, verbose } = release2; const mode = release2.mode; const dryRun = release2.dryRun === "true"; const otp = Number(release2.otp); const postponePublish = release2.postponePublish !== "false"; if (confirmRelease === false) { (0, import_prompts.cancel)("\u{1F6AB} Release cancelled."); return 0; } console.log(""); if (otp) { const npmUser = await whoami(); if (!npmUser) { console.log( `${import_chalk4.default.red("\u{1F6AB} Npm publish requires you to be logged in.\n")}` ); console.log( `You need to authorize using ${import_chalk4.default.yellow.bold("npm login")} and follow the instructions.` ); console.log("After a successful login, try to publish again."); return 0; } } switch (mode) { case "publish": break; case "release": { const originPackageFile = (0, import_devkit3.readJsonFile)("package.json"); console.log(`${import_chalk4.default.magenta.underline("Analyze changes")}`); const versionStatus = await (0, import_release3.releaseVersion)({ dryRun, verbose }); const projectsVersionData = versionStatus.projectsVersionData; if (!dryRun) { revertPackageJson(originPackageFile, projectsVersionData); await git.add("package.json"); } if (!await changelogs({ dryRun, verbose, versionData: projectsVersionData })) { return 1; } const newVersionFound = Object.keys(projectsVersionData).some( (project) => projectsVersionData[project].newVersion ); if (newVersionFound && postponePublish && !dryRun) { (0, import_prompts.outro)("\u{1F680} The new release will be published by GitHub Actions!"); return 0; } if (!newVersionFound) { return 0; } if (dryRun && postponePublish) { dryRunOutro(); return 0; } if (postponePublish) { return 0; } } break; } const publishStats = await publish({ dryRun, otp, verbose }); if (!publishStats) { return 1; } const { successful, total } = publishStats; if (!total) { (0, import_prompts.outro)("No packages to publish"); return 0; } const term = mode === "publish" ? "Publish" : "Release"; const termed = mode === "publish" ? "Published" : "Released"; if (dryRun) { dryRunOutro(); } else { (0, import_prompts.outro)( successful === total ? import_chalk4.default.green(`\u{1F680} ${termed} successfully!`) : successful > 0 ? import_chalk4.default.yellow( `\u{1F680} ${termed} ${successful} successfully, while ${total - successful} failed` ) : import_chalk4.default.red(`\u{1F6AB} ${term} failed`) ); } return 0; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { changelogs, publish, release });