UNPKG

qcobjects-cli

Version:

qcobjects cli command line tool

246 lines 13.2 kB
/** * QCObjects CLI 2.5 * ________________ * * Author: Jean Machuca <correojean@gmail.com> * * Cross Browser Javascript Framework for MVC Patterns * QuickCorp/QCObjects is licensed under the * GNU Lesser General Public License v3.0 * [LICENSE] (https://github.com/QuickCorp/QCObjects/blob/master/LICENSE.txt) * * Permissions of this copyleft license are conditioned on making available * complete source code of licensed works and modifications under the same * license or the GNU GPLv3. Copyright and license notices must be preserved. * Contributors provide an express grant of patent rights. However, a larger * work using the licensed work through interfaces provided by the licensed * work may be distributed under different terms and without source code for * the larger work. * * Copyright (C) 2015 Jean Machuca,<correojean@gmail.com> * * Everyone is permitted to copy and distribute verbatim copies of this * license document, but changing it is not allowed. */ /*eslint no-unused-vars: "off"*/ /*eslint no-redeclare: "off"*/ /*eslint no-empty: "off"*/ /*eslint strict: "off"*/ /*eslint no-mixed-operators: "off"*/ /*eslint no-undef: "off"*/ "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandHandler = void 0; const node_fs_1 = __importDefault(require("node:fs")); const qcobjects_1 = require("qcobjects"); class CommandHandler extends qcobjects_1.InheritClass { choiceOption; switchCommander; constructor({ switchCommander }) { super({ switchCommander }); this.switchCommander = switchCommander; const commandHandler = this; this.choiceOption = { async v_major(filename, options) { filename = (typeof filename === "undefined") ? ("VERSION") : (filename); const versionString = this.getVersionStringFromFile(filename); const versionSuffix = this.parseVersionSuffix(versionString); const versionObject = this.parseVersionString(versionString); const major = parseInt(versionObject.major); const minor = parseInt(versionObject.minor); const patch = parseInt(versionObject.patch); const newVersion = this.buildNewVersionString({ major: major + 1, minor: minor, patch: patch }, versionSuffix); this.saveNewVersionFile(filename, newVersion); if (options.syncGit) { var commitMsg = options.commitMsg || `New Version v${newVersion}`; await this.syncGit(newVersion, commitMsg, options.syncNpm); } }, async v_minor(filename, options) { filename = (typeof filename === "undefined") ? ("VERSION") : (filename); const versionString = this.getVersionStringFromFile(filename); const versionSuffix = this.parseVersionSuffix(versionString); const versionObject = this.parseVersionString(versionString); const major = parseInt(versionObject.major); const minor = parseInt(versionObject.minor); const patch = parseInt(versionObject.patch); const newVersion = this.buildNewVersionString({ major: major, minor: minor + 1, patch: patch }, versionSuffix); this.saveNewVersionFile(filename, newVersion); if (options.syncGit) { var commitMsg = options.commitMsg || `New Version v${newVersion}`; await this.syncGit(newVersion, commitMsg, options.syncNpm); } }, async v_patch(filename, options) { filename = (typeof filename === "undefined") ? ("VERSION") : (filename); const versionString = this.getVersionStringFromFile(filename); const versionSuffix = this.parseVersionSuffix(versionString); const versionObject = this.parseVersionString(versionString); const major = parseInt(versionObject.major); const minor = parseInt(versionObject.minor); const patch = parseInt(versionObject.patch); const newVersion = this.buildNewVersionString({ major: major, minor: minor, patch: patch + 1 }, versionSuffix); this.saveNewVersionFile(filename, newVersion); if (options.syncGit) { var commitMsg = options.commitMsg || `New Version v${newVersion}`; await this.syncGit(newVersion, commitMsg, options.syncNpm); } }, v_sync(filename, options) { filename = (typeof filename === "undefined") ? ("VERSION") : (filename); var commandHandler = this; commandHandler.switchCommander.shellCommands([ "echo $(git describe)" ]).then(function (response) { const versionString = response[0].split("-")[0].slice(1).replace("\n", ""); console.log(versionString); const versionSuffix = commandHandler.parseVersionSuffix(versionString); const versionObject = commandHandler.parseVersionString(versionString); const major = parseInt(versionObject.major); const minor = parseInt(versionObject.minor); const patch = parseInt(versionObject.patch); const newVersion = commandHandler.buildNewVersionString({ major: major, minor: minor, patch: patch }, versionSuffix); commandHandler.saveNewVersionFile(filename, newVersion); var commitMsg = options.commitMsg || `Synced Version v${newVersion}`; commandHandler.switchCommander.shellCommands([ "git fetch --tags -f", `git add . && git commit -am "${commitMsg}"`, "git fetch origin --tags", "git tag -ln", `npm version "${newVersion}" --allow-same-version -m "${commitMsg}"`, "git push && git push --tags" ]).then(function (response) { console.log(response); }); }); }, v_changelog() { const commandHandler = this; commandHandler.switchCommander.shellCommands([ "git tag -ln" ]).then(function (response) { var versionTags = response[0].split("\n").map((tag) => tag.split(" ").unique()).unique().map((tag) => { return { "version": tag[0], "major": tag[0].split(".")[0], "minor": tag[0].split(".")[0] + "." + tag[0].split(".")[1], "description": tag.slice(1).join(" ").trim() }; }); var minorVersionTags = versionTags.filter((tag) => tag.version !== "").map((tag) => tag.version.split(".")[0] + "." + tag.version.split(".")[1]).unique(); var history = minorVersionTags.map((minor) => { return { "major": minor.split(".")[0], "minor": minor, "history": "\n\t- " + versionTags.filter((tag) => tag.minor === minor).map(function (tag) { return tag.description; }).filter((desc) => !desc.startsWith(minor.slice(1))).sort().unique().join("\n\t- ") }; }).map((hist) => { return `## ${hist.major} -> ${hist.minor}` + "\n" + hist.history; }).join("\n"); const subtitle = "This is an automatic Changelog history of versions generated using the command: **qcobjects v-changelog > CHANGELOG.md**"; console.log("# Changelog \n\n" + subtitle + "\n\n" + history); }); } }; switchCommander.program.command("v-major [filename]") .option("--sync-git, --git", "Sync with Git") .option("--sync-npm, --npm", "Sync with NPM") .option("-m, --commit-msg [message]", "Commit Message") .description("Semantic Versioning: Upgrade to a new major version") .action(function (args, options) { return commandHandler.choiceOption.v_major.call(commandHandler, args, options); }); switchCommander.program.command("v-minor [filename]") .option("--sync-git, --git", "Sync with Git") .option("--sync-npm, --npm", "Sync with NPM") .option("-m, --commit-msg [message]", "Commit Message") .description("Semantic Versioning: Upgrade to a new minor version") .action(function (args, options) { return commandHandler.choiceOption.v_minor.call(commandHandler, args, options); }); switchCommander.program.command("v-patch [filename]") .option("--sync-git, --git", "Sync with Git") .option("--sync-npm, --npm", "Sync with NPM") .option("-m, --commit-msg [message]", "Commit Message") .description("Semantic Versioning: Upgrade to a new patch version") .action(function (args, options) { return commandHandler.choiceOption.v_patch.call(commandHandler, args, options); }); switchCommander.program.command("v-sync [filename]") .option("-m, --commit-msg [message]", "Commit Message") .description("Semantic Versioning: Sync the version of NPM with version of GIT") .action(function (args, options) { commandHandler.choiceOption.v_sync.call(commandHandler, args, options); }); switchCommander.program.command("v-changelog") .description("Semantic Versioning: Shows a changelog using Semantic Versioning") .action(function (args, options) { commandHandler.choiceOption.v_changelog.call(commandHandler); }); } syncGit(versionString, commitMsg, syncNpm = false) { let _commands_ = ["git fetch --tags -f"]; // Commit VERSION file first so working directory is clean // (v_patch/v_minor/v_major write to VERSION before calling syncGit) _commands_.push(`git add . && git commit -am "${commitMsg}"`); if (syncNpm) { // npm version requires a clean working directory _commands_.push(`npm version "${versionString}" -m "${commitMsg}"`); } _commands_ = _commands_.concat([ "git fetch origin --tags", "git tag -ln", ]); if (!syncNpm) { _commands_.push(`git tag -a "v${versionString}" -m "${commitMsg}"`); } _commands_.push("git push && git push --tags"); return this.switchCommander.shellCommands(_commands_).then(function (response) { qcobjects_1.logger.info("Synced to Git"); qcobjects_1.logger.debug(response); }).catch(function (e) { qcobjects_1.logger.info("Something went wrong trying to sync to git"); qcobjects_1.logger.debug(e); }); } parseVersionString(versionString) { versionString = versionString.replace("\n", ""); const regexpVer = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; const versionObject = { ...versionString.match(regexpVer)?.groups }; return versionObject; } getVersionStringFromFile(filename) { let versionString; try { versionString = node_fs_1.default.readFileSync(filename).toString().replace("\n", ""); } catch (e) { versionString = "0.0.1"; } return versionString; } buildNewSemVersionString({ major, minor, patch }) { return `${major}.${minor}.${patch}`; } parseVersionSuffix(versionString) { versionString = versionString.replace("\n", ""); const versionObject = this.parseVersionString(versionString); const semVersionString = this.buildNewSemVersionString(versionObject); return versionString.replace(semVersionString, ""); } buildNewVersionString({ major, minor, patch }, suffix) { const semVersionString = this.buildNewSemVersionString({ major, minor, patch }); return `${semVersionString}${suffix}`; } saveNewVersionFile(filename, versionString) { node_fs_1.default.writeFileSync(filename, versionString); } } exports.CommandHandler = CommandHandler; (0, qcobjects_1.Package)("com.qcobjects.cli.commands.version", [ CommandHandler ]); //# sourceMappingURL=cli-commands-version.js.map