diff --git a/package.json b/package.json index e3a82586..58a876e6 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "ai-min": "node common/scripts/install-run-rush.js ai-min", "ai-restore": "node common/scripts/install-run-rush.js ai-restore", "npm-pack": "node common/scripts/install-run-rush.js npm-pack --verbose", - "npm-publish": "node ./tools/release-tools/npm_publish.js" + "npm-publish": "node ./tools/release-tools/npm_publish.js", + "npm-set-latest": "node ./tools/release-tools/npm_set_latest.js" }, "repository": { "type": "git", diff --git a/tools/release-tools/npm_publish.js b/tools/release-tools/npm_publish.js index 1e069259..ab377d92 100644 --- a/tools/release-tools/npm_publish.js +++ b/tools/release-tools/npm_publish.js @@ -106,7 +106,7 @@ if (parseArgs()) { throw new Error("!!! NPM Package not found [" + npmPackageName + "]"); } - console.log(`npm pacakage present ${npmPackageName}`); + console.log(`npm package present ${npmPackageName}`); let npmCmd = `npm publish ${npmPackageName} --access public ${dryRun}`; console.log(`Running: \"${npmCmd}\"`); child_process.execSync(npmCmd); diff --git a/tools/release-tools/npm_set_latest.js b/tools/release-tools/npm_set_latest.js new file mode 100644 index 00000000..2ddbca94 --- /dev/null +++ b/tools/release-tools/npm_set_latest.js @@ -0,0 +1,107 @@ +const fs = require("fs"); +const child_process = require("child_process"); + +const packageGroupDef = "./tools/release-tools/package_groups.json"; +let packageGroup; +let isTest = false; + +function showHelp() { + var scriptParts; + var scriptName = process.argv[1]; + if (scriptName.indexOf("\\") !== -1) { + scriptParts = scriptName.split("\\"); + scriptName = scriptParts[scriptParts.length - 1]; + } else if (scriptName.indexOf("/") !== -1) { + scriptParts = scriptName.split("/"); + scriptName = scriptParts[scriptParts.length - 1]; + } + + console.log(""); + console.log(scriptName + " "); + console.log("--------------------------"); + console.log(" - Identifies the group to publish, identifies folders"); +} + +function parseArgs() { + if (process.argv.length < 2) { + console.error("!!! Invalid number of arguments -- " + process.argv.length); + return false; + } + + let idx = 2; + while (idx < process.argv.length) { + let theArg = process.argv[idx]; + if (theArg.startsWith("-")) { + if (theArg === "-test") { + isTest = true; + } else { + console.error("!!! Unknown switch [" + theArg + "] detected"); + return false; + } + } else if (!packageGroup) { + packageGroup = theArg; + } else { + console.error("!!! Invalid Argument [" + theArg + "] detected"); + return false; + } + + idx++; + } + + return true; +} + +function removeTrailingComma(text) { + return text.replace(/,(\s*[}\],])/g, "$1"); +} + +function removeComments(text) { + return text.replace(/^\s*\/\/\s.*$/gm, ""); +} + +function getPackageJson(packageJsonFile) { + var packageText = removeTrailingComma(fs.readFileSync(packageJsonFile, "utf-8")); + + return JSON.parse(packageText); +} + +function getGroupProjects() { + if (!fs.existsSync(packageGroupDef)) { + console.error("!!! Unable to locate package group definitions [" + packageGroupDef + "]"); + throw new Error("!!! Unable to locate package group definitions."); + } + + var groupText = removeComments(removeTrailingComma(fs.readFileSync(packageGroupDef, "utf-8"))); + + let groupJson = JSON.parse(groupText); + return groupJson[packageGroup] || []; +} + +if (parseArgs()) { + var packages = getGroupProjects(); + + console.log(`Set latest tag [${packageGroup}] packages => ${packages.length}`); + packages.forEach((packageRoot) => { + let packageJsonFile = packageRoot + "/package.json"; + + if (!fs.existsSync(packageJsonFile)) { + console.error("!!! Source package.json doesn't exist [" + packageJsonFile + "]"); + throw new Error("!!! Source package.json doesn't exist [" + packageJsonFile + "]"); + } + + let packageJson = getPackageJson(packageJsonFile); + + console.log("\n\n##################################################################"); + console.log("Setting latest tag - " + packageJson.name); + console.log("##################################################################"); + + let npmCmd = `npm dist-tag add ${packageJson.name}@${packageJson.version} latest`; + console.log(`Running: \"${npmCmd}\"`); + if (!isTest) { + child_process.execSync(npmCmd); + } + }); +} else { + showHelp(); + process.exit(1); +} diff --git a/tools/release-tools/setVersion.js b/tools/release-tools/setVersion.js index 9440da74..dde02a3f 100644 --- a/tools/release-tools/setVersion.js +++ b/tools/release-tools/setVersion.js @@ -393,6 +393,7 @@ function shouldProcess(name) { function updatePublishConfig(package, newVersion) { let details = getVersionDetails(newVersion); + let majorVersion = package.version.split(".")[0]; if (!details.type || details.type === "release") { if (package.publishConfig && package.publishConfig.tag) { @@ -405,7 +406,15 @@ function updatePublishConfig(package, newVersion) { } // Set the publishing tag - package.publishConfig.tag = details.type; + if (details.type === "nightly" || details.type === "dev" || details.type === "beta" || details.type === "alpha") { + console.log(` Type - [${details.type}] - ${majorVersion}`); + package.publishConfig.tag = details.type + (majorVersion !== "0" ? majorVersion : ""); + } else { + console.log(` Type - [${details.type}]`); + package.publishConfig.tag = details.type; + } + + console.log(` Tag - [${package.publishConfig.tag}]`); } if (package.publishConfig && Object.keys(package.publishConfig).length === 0) {