Skip to content

Commit

Permalink
Add AI version update script to assist with automated integration tes…
Browse files Browse the repository at this point in the history
…ting (#127)
  • Loading branch information
MSNev authored Nov 1, 2024
1 parent 4c71fff commit 3d372bc
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"rupdate": "node common/scripts/install-run-rush.js update --recheck --purge --full",
"serve": "grunt serve",
"setVersion": "node ./tools/release-tools/setVersion.js",
"setAiVersion": "node ./tools/release-tools/setAiVersion.js",
"purge": "node common/scripts/install-run-rush.js purge",
"fullClean": "git clean -xdf && npm install && rush update --recheck --full",
"fullCleanBuild": "npm run fullClean && npm run rebuild",
Expand Down
12 changes: 6 additions & 6 deletions tools/release-tools/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "applicationinsights-js-release-tools",
"version": "3.1.3-dev",
"author": "Javascript Telemetry SDKs <[email protected]>",
"description": "1DS Web SDK",
"homepage": "https://1dsdocs.azurewebsites.net/sdk.html",
"version": "3.1.3",
"author": "Microsoft Application Insights Team",
"description": "Microsoft Application Insights React release tools",
"homepage": "https://github.com/microsoft/applicationinsights-react-js#readme",
"sideEffects": false,
"scripts": {
"update": "rush update",
Expand All @@ -19,10 +19,10 @@
],
"repository": {
"type": "git",
"url": "https://msasg.visualstudio.com/DefaultCollection/Shared%20Data/_git/1DS.JavaScript"
"url": "https://github.com/microsoft/applicationinsights-react-js"
},
"devDependencies": {
"grunt": "^1.5.3",
"globby": "^11.0.0"
}
}
}
166 changes: 166 additions & 0 deletions tools/release-tools/setAiVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
const fs = require("fs");
const globby = require("globby");

let newAiVer = null;
let testOnly = null;

const theVersion = require(process.cwd() + "/version.json");

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 + " [<newAiVersion> [-test]");
console.log("--------------------------");
console.log(" <newAiVersion> - Identifies the application insights version to set for all packages");
console.log(" -test - Scan all of the package.json files and log the changes, but DON'T update the files");
}

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 === "-test") {
testOnly = true;
} else if (!newAiVer) {
newAiVer = theArg;
} else {
console.error("!!! Invalid Argument [" + theArg + "] detected");
return false;
}

idx ++;
}

// If no version,
if (!newAiVer) {
return false;
}

return true;
}

function shouldProcess(name) {
if (name.indexOf("node_modules/") !== -1) {
return false;
}

if (name.indexOf("common/temp") !== -1) {
return false;
}

if (name.indexOf("examples/") !== -1) {
return true;
}

if (name.indexOf("applicationinsights-react-js") !== -1) {
return true;
}

if (name === "package.json") {
return true;
}

return false;
}

function shouldUpdateDependency(name) {
if (name.indexOf("@microsoft/applicationinsights-") === -1) {
return false;
}

if (name.indexOf("applicationinsights-shims") !== -1) {
return false;
}

if (name.indexOf("applicationinsights-rollup") !== -1) {
return false;
}

return true;
}

function updateDependencies(target, newVersion) {
let changed = false;
if (target) {
let isDigit = /^\d/.test(newVersion);
Object.keys(target).forEach((value) => {
if (shouldUpdateDependency(value)) {
let version = target[value];
if (version.startsWith("^") && isDigit) {
if (version !== "^" + newVersion) {
target[value] = "^" + newVersion;
}
} else if (version.startsWith("~") && isDigit) {
if (version !== "~" + newVersion) {
target[value] = "~" + newVersion;
}
} else if (version !== newVersion) {
target[value] = newVersion;
}

if (version != target[value]) {
console.log(" Updated: " + value + " \"" + version + "\" => \"" + target[value] + "\"");
changed = true;
} else {
console.log(" Skipped: " + value + " \"" + version + "\"");
}
}
});
}

return changed;
}

const setPackageJsonRelease = () => {
const files = globby.sync(["./**/package.json", "!**/node_modules/**"]);
let changed = false;
files.map(packageFile => {
// Don't update node_modules
if (shouldProcess(packageFile)) {
console.log("Loading - " + packageFile);

let theFilename = packageFile;
const package = require(process.cwd() + "\\" + theFilename);
console.log(" Name - " + package.name);

let updated = false;
updated |= updateDependencies(package.dependencies, newAiVer);
updated |= updateDependencies(package.peerDependencies, newAiVer);
updated |= updateDependencies(package.devDependencies, newAiVer);

if (updated && !testOnly) {
// Rewrite the file
const newContent = JSON.stringify(package, null, 4) + "\n";
fs.writeFileSync(theFilename, newContent);
changed = true;
}
console.log(" -------------------------------------------------------------------------------------");
}
});

return changed;
};

if (parseArgs()) {
if (setPackageJsonRelease()) {
console.log("Version updated, now run 'npm run rupdate'");
} else {
console.log("Nothing Changed");
}
} else {
showHelp();
}

0 comments on commit 3d372bc

Please sign in to comment.