Skip to content

Commit

Permalink
[Main] Add automated ability to reset the npm latest tag to a specifi…
Browse files Browse the repository at this point in the history
…c build (#2127)

- Related to #2126
  • Loading branch information
MSNev committed Aug 10, 2023
1 parent f45ad22 commit 0e06711
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tools/release-tools/npm_publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
107 changes: 107 additions & 0 deletions tools/release-tools/npm_set_latest.js
Original file line number Diff line number Diff line change
@@ -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 + " <group> ");
console.log("--------------------------");
console.log(" <group> - 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);
}
11 changes: 10 additions & 1 deletion tools/release-tools/setVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 0e06711

Please sign in to comment.