diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5dabb89 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "sarif-viewer.connectToGithubCodeScanning": "off" +} \ No newline at end of file diff --git a/package.json b/package.json index 4cb9024..06831f4 100644 --- a/package.json +++ b/package.json @@ -1,81 +1,83 @@ { - "name": "@microsoft/applicationinsights-angularplugin-js", - "version": "15.3.4", - "description": "Microsoft Application Insights Angular plugin", - "main": "dist/bundles/microsoft-applicationinsights-angularplugin-js.umd.js", - "module": "dist/fesm2015/microsoft-applicationinsights-angularplugin-js.js", - "types": "dist/microsoft-applicationinsights-angularplugin-js.d.ts", - "metadata": "dist/microsoft-applicationinsights-angularplugin-js.metadata.json", - "repository": { - "type": "git", - "url": "https://github.com/microsoft/applicationinsights-angularplugin-js" - }, - "scripts": { - "ng": "ng", - "start": "ng serve", - "rebuild": "npm run build && npm run test", - "build": "npm run lintfix && ng build --configuration production", - "test": "ng test --watch=false --browsers=ChromeHeadless --code-coverage=true", - "watch": "ng test", - "lint": "ng lint", - "lintfix": "ng lint --fix", - "clean": "git clean -xdf", - "gh-status": "node ./tools/status-tools/github-status.js", - "npm-package": "node ./tools/release-tools/npm-package.js" - }, - "devDependencies": { - "@angular-devkit/build-angular": "^15.2.9", - "@angular-eslint/builder": "15.0.0", - "@angular-eslint/eslint-plugin": "15.0.0", - "@angular-eslint/eslint-plugin-template": "15.0.0", - "@angular-eslint/schematics": "15.0.0", - "@angular-eslint/template-parser": "15.0.0", - "@angular/animations": "^15.2.9", - "@angular/cli": "^15.2.9", - "@angular/common": "^15.2.9", - "@angular/compiler": "^15.2.9", - "@angular/compiler-cli": "^15.2.9", - "@angular/core": "^15.2.9", - "@angular/forms": "^15.2.9", - "@angular/platform-browser": "^15.2.9", - "@angular/platform-browser-dynamic": "^15.2.9", - "@angular/router": "^15.2.9", - "@microsoft/applicationinsights-analytics-js": "^3.3.4", - "@types/jasmine": "~3.6.0", - "@types/node": "^12.20.55", - "@typescript-eslint/eslint-plugin": "^5.62.0", - "@typescript-eslint/parser": "^5.62.0", - "chromium": "^3.0.3", - "eslint": "^8.50.0", - "eslint-plugin-import": "^2.28.1", - "eslint-plugin-jsdoc": "^46.8.2", - "eslint-plugin-prefer-arrow": "^1.2.3", - "jasmine-core": "~3.6.0", - "jasmine-spec-reporter": "~5.0.0", - "karma": "~6.3.17", - "karma-chrome-launcher": "~3.1.0", - "karma-coverage": "~2.0.3", - "karma-jasmine": "~4.0.0", - "ng-packagr": "^15.2.2", - "protractor": "~7.0.0", - "puppeteer": "^22.12.1", - "rxjs": "~6.6.0", - "ts-node": "~8.3.0", - "tslib": "^2.6.2", - "typescript": "~4.9.5", - "zone.js": "^0.11.8" - }, - "dependencies": { - "@microsoft/applicationinsights-common": "^3.3.4", - "@microsoft/applicationinsights-core-js": "^3.3.4", - "@microsoft/applicationinsights-properties-js": "^3.3.4", - "@microsoft/applicationinsights-shims": "^3.0.1", - "tslib": "^2.6.2" - }, - "peerDependencies": { - "@angular/common": ">= 14.0.3", - "@angular/core": ">= 14.0.3", - "@microsoft/applicationinsights-analytics-js": "^3.3.4" - }, - "license": "MIT" + "name": "@microsoft/applicationinsights-angularplugin-js", + "version": "15.3.4", + "description": "Microsoft Application Insights Angular plugin", + "main": "dist/bundles/microsoft-applicationinsights-angularplugin-js.umd.js", + "module": "dist/fesm2015/microsoft-applicationinsights-angularplugin-js.js", + "types": "dist/microsoft-applicationinsights-angularplugin-js.d.ts", + "metadata": "dist/microsoft-applicationinsights-angularplugin-js.metadata.json", + "repository": { + "type": "git", + "url": "https://github.com/microsoft/applicationinsights-angularplugin-js" + }, + "scripts": { + "ng": "ng", + "start": "ng serve", + "rebuild": "npm run build && npm run test", + "build": "npm run lintfix && ng build --configuration production", + "test": "ng test --watch=false --browsers=ChromeHeadless --code-coverage=true", + "watch": "ng test", + "lint": "ng lint", + "lintfix": "ng lint --fix", + "clean": "git clean -xdf", + "gh-status": "node ./tools/status-tools/github-status.js", + "npm-package": "node ./tools/release-tools/npm-package.js", + "setVersion": "cd ./tools/release-tools && npm install && cd ../.. && node ./tools/release-tools/setVersion.js", + "setAiVersion": "cd ./tools/release-tools && npm install && cd ../.. && node ./tools/release-tools/setAiVersion.js" + }, + "devDependencies": { + "@angular-devkit/build-angular": "^15.2.9", + "@angular-eslint/builder": "15.0.0", + "@angular-eslint/eslint-plugin": "15.0.0", + "@angular-eslint/eslint-plugin-template": "15.0.0", + "@angular-eslint/schematics": "15.0.0", + "@angular-eslint/template-parser": "15.0.0", + "@angular/animations": "^15.2.9", + "@angular/cli": "^15.2.9", + "@angular/common": "^15.2.9", + "@angular/compiler": "^15.2.9", + "@angular/compiler-cli": "^15.2.9", + "@angular/core": "^15.2.9", + "@angular/forms": "^15.2.9", + "@angular/platform-browser": "^15.2.9", + "@angular/platform-browser-dynamic": "^15.2.9", + "@angular/router": "^15.2.9", + "@microsoft/applicationinsights-analytics-js": "^3.3.4", + "@types/jasmine": "~3.6.0", + "@types/node": "^12.20.55", + "@typescript-eslint/eslint-plugin": "^5.62.0", + "@typescript-eslint/parser": "^5.62.0", + "chromium": "^3.0.3", + "eslint": "^8.50.0", + "eslint-plugin-import": "^2.28.1", + "eslint-plugin-jsdoc": "^46.8.2", + "eslint-plugin-prefer-arrow": "^1.2.3", + "jasmine-core": "~3.6.0", + "jasmine-spec-reporter": "~5.0.0", + "karma": "~6.3.17", + "karma-chrome-launcher": "~3.1.0", + "karma-coverage": "~2.0.3", + "karma-jasmine": "~4.0.0", + "ng-packagr": "^15.2.2", + "protractor": "~7.0.0", + "puppeteer": "^22.12.1", + "rxjs": "~6.6.0", + "ts-node": "~8.3.0", + "tslib": "^2.6.2", + "typescript": "~4.9.5", + "zone.js": "^0.11.8" + }, + "dependencies": { + "@microsoft/applicationinsights-common": "^3.3.4", + "@microsoft/applicationinsights-core-js": "^3.3.4", + "@microsoft/applicationinsights-properties-js": "^3.3.4", + "@microsoft/applicationinsights-shims": "^3.0.1", + "tslib": "^2.6.2" + }, + "peerDependencies": { + "@angular/common": ">= 14.0.3", + "@angular/core": ">= 14.0.3", + "@microsoft/applicationinsights-analytics-js": "^3.3.4" + }, + "license": "MIT" } diff --git a/projects/applicationinsights-angularplugin-js/package.json b/projects/applicationinsights-angularplugin-js/package.json index f54badf..8ea4c21 100644 --- a/projects/applicationinsights-angularplugin-js/package.json +++ b/projects/applicationinsights-angularplugin-js/package.json @@ -1,30 +1,30 @@ { - "name": "@microsoft/applicationinsights-angularplugin-js", - "version": "15.3.4", - "description": "Microsoft Application Insights Angular plugin", - "author": "Microsoft Application Insights Team", - "license": "MIT", - "repository": { - "type": "git", - "url": "https://github.com/microsoft/applicationinsights-angularplugin-js" - }, - "bugs": { - "url": "https://github.com/microsoft/applicationinsights-angularplugin-js/issues" - }, - "homepage": "https://github.com/microsoft/applicationinsights-angularplugin-js#readme", - "dependencies": { - "@microsoft/applicationinsights-shims": "^3.0.1", - "@microsoft/applicationinsights-common": "^3.3.4", - "@microsoft/applicationinsights-core-js": "^3.3.4", - "@microsoft/applicationinsights-properties-js": "^3.3.4", - "tslib": "^2.0.0" - }, - "devDependencies": { - "@microsoft/applicationinsights-analytics-js": "^3.3.4" - }, - "peerDependencies": { - "@microsoft/applicationinsights-analytics-js": "^3.3.4", - "@angular/common": ">= 14.0.3", - "@angular/core": ">= 14.0.3" - } + "name": "@microsoft/applicationinsights-angularplugin-js", + "version": "15.3.4", + "description": "Microsoft Application Insights Angular plugin", + "author": "Microsoft Application Insights Team", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/microsoft/applicationinsights-angularplugin-js" + }, + "bugs": { + "url": "https://github.com/microsoft/applicationinsights-angularplugin-js/issues" + }, + "homepage": "https://github.com/microsoft/applicationinsights-angularplugin-js#readme", + "dependencies": { + "@microsoft/applicationinsights-shims": "^3.0.1", + "@microsoft/applicationinsights-common": "^3.3.4", + "@microsoft/applicationinsights-core-js": "^3.3.4", + "@microsoft/applicationinsights-properties-js": "^3.3.4", + "tslib": "^2.0.0" + }, + "devDependencies": { + "@microsoft/applicationinsights-analytics-js": "^3.3.4" + }, + "peerDependencies": { + "@microsoft/applicationinsights-analytics-js": "^3.3.4", + "@angular/common": ">= 14.0.3", + "@angular/core": ">= 14.0.3" + } } diff --git a/sample/applicationinsights-angularplugin-sample/package.json b/sample/applicationinsights-angularplugin-sample/package.json index 3034694..ccab7bf 100644 --- a/sample/applicationinsights-angularplugin-sample/package.json +++ b/sample/applicationinsights-angularplugin-sample/package.json @@ -1,60 +1,60 @@ { - "name": "applicationinsights-angularplugin-sample", - "version": "0.3.0", - "description": "Microsoft Applicationinsights Angularplugin Sample App", - "author": "Microsoft Application Insights Team", - "license": "MIT", - "repository": { - "type": "git", - "url": "https://github.com/microsoft/applicationinsights-angularplugin-js" - }, - "bugs": { - "url": "https://github.com/microsoft/applicationinsights-angularplugin-js/issues" - }, - "homepage": "https://github.com/microsoft/applicationinsights-angularplugin-js#readme", - "scripts": { - "ng": "ng", - "start": "ng serve", - "build": "ng build", - "rebuild": "ng build", - "watch": "ng build --watch --configuration development", - "test": "ng test --watch=false --browsers=ChromeHeadless --code-coverage=true", - "e2e": "ng e2e" - }, - "private": true, - "dependencies": { - "@angular/animations": "^15.2.9", - "@angular/common": "^15.2.9", - "@angular/compiler": "^15.2.9", - "@angular/core": "^15.2.9", - "@angular/forms": "^15.2.9", - "@angular/platform-browser": "^15.2.9", - "@angular/platform-browser-dynamic": "^15.2.9", - "@angular/router": "^15.2.9", - "@microsoft/applicationinsights-angularplugin-js": "^15.3.4", - "@microsoft/applicationinsights-web": "^3.3.4", - "angular-in-memory-web-api": "~0.15.0", - "rxjs": "~7.5.0", - "tslib": "^2.3.0", - "zone.js": "~0.11.4" - }, - "devDependencies": { - "@angular-devkit/build-angular": "^15.2.9", - "@angular/cli": "^15.2.9", - "@angular/compiler-cli": "^15.2.9", - "@types/jasmine": "~4.0.0", - "@types/node": "^16.11.35", - "copyfiles": "^2.4.1", - "jasmine-core": "~4.3.0", - "jasmine-marbles": "~0.9.2", - "jasmine-spec-reporter": "~7.0.0", - "karma": "~6.4.0", - "karma-chrome-launcher": "~3.1.0", - "karma-coverage": "~2.2.0", - "karma-jasmine": "~5.1.0", - "karma-jasmine-html-reporter": "~2.0.0", - "protractor": "~7.0.0", - "ts-node": "~10.9.0", - "typescript": "~4.9.5" - } + "name": "applicationinsights-angularplugin-sample", + "version": "0.3.0", + "description": "Microsoft Applicationinsights Angularplugin Sample App", + "author": "Microsoft Application Insights Team", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/microsoft/applicationinsights-angularplugin-js" + }, + "bugs": { + "url": "https://github.com/microsoft/applicationinsights-angularplugin-js/issues" + }, + "homepage": "https://github.com/microsoft/applicationinsights-angularplugin-js#readme", + "scripts": { + "ng": "ng", + "start": "ng serve", + "build": "ng build", + "rebuild": "ng build", + "watch": "ng build --watch --configuration development", + "test": "ng test --watch=false --browsers=ChromeHeadless --code-coverage=true", + "e2e": "ng e2e" + }, + "private": true, + "dependencies": { + "@angular/animations": "^15.2.9", + "@angular/common": "^15.2.9", + "@angular/compiler": "^15.2.9", + "@angular/core": "^15.2.9", + "@angular/forms": "^15.2.9", + "@angular/platform-browser": "^15.2.9", + "@angular/platform-browser-dynamic": "^15.2.9", + "@angular/router": "^15.2.9", + "@microsoft/applicationinsights-angularplugin-js": "^15.3.4", + "@microsoft/applicationinsights-web": "^3.3.4", + "angular-in-memory-web-api": "~0.15.0", + "rxjs": "~7.5.0", + "tslib": "^2.3.0", + "zone.js": "~0.11.4" + }, + "devDependencies": { + "@angular-devkit/build-angular": "^15.2.9", + "@angular/cli": "^15.2.9", + "@angular/compiler-cli": "^15.2.9", + "@types/jasmine": "~4.0.0", + "@types/node": "^16.11.35", + "copyfiles": "^2.4.1", + "jasmine-core": "~4.3.0", + "jasmine-marbles": "~0.9.2", + "jasmine-spec-reporter": "~7.0.0", + "karma": "~6.4.0", + "karma-chrome-launcher": "~3.1.0", + "karma-coverage": "~2.2.0", + "karma-jasmine": "~5.1.0", + "karma-jasmine-html-reporter": "~2.0.0", + "protractor": "~7.0.0", + "ts-node": "~10.9.0", + "typescript": "~4.9.5" + } } diff --git a/tools/release-tools/package.json b/tools/release-tools/package.json new file mode 100644 index 0000000..ff08d33 --- /dev/null +++ b/tools/release-tools/package.json @@ -0,0 +1,28 @@ +{ + "name": "applicationinsights-angularplugin-js-release-tools", + "version": "15.3.4", + "author": "Microsoft Application Insights Team", + "description": "Microsoft Application Insights Angular plugin release tools", + "homepage": "https://github.com/microsoft/applicationinsights-angularplugin-js#readme", + "sideEffects": false, + "scripts": { + "update": "rush update", + "build": "", + "rebuild": "", + "test": "", + "updateVer": "node ./tools/release-tools/updateVersion.js" + }, + "keywords": [ + "1DS", + "Js", + "SDK" + ], + "repository": { + "type": "git", + "url": "https://github.com/microsoft/applicationinsights-angularplugin-js" + }, + "devDependencies": { + "grunt": "^1.5.3", + "globby": "^11.0.0" + } +} \ No newline at end of file diff --git a/tools/release-tools/setAiVersion.js b/tools/release-tools/setAiVersion.js new file mode 100644 index 0000000..6068d8c --- /dev/null +++ b/tools/release-tools/setAiVersion.js @@ -0,0 +1,174 @@ +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 + " [ [-test]"); + console.log("--------------------------"); + console.log(" - 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("sample/") !== -1) { + return true; + } + + if (name.indexOf("applicationinsights-angularplugin-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; + } + + if (name.indexOf("applicationinsights-angularplugin-js") !== -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(); +} diff --git a/tools/release-tools/setVersion.js b/tools/release-tools/setVersion.js new file mode 100644 index 0000000..154ea63 --- /dev/null +++ b/tools/release-tools/setVersion.js @@ -0,0 +1,489 @@ +const fs = require("fs"); +const globby = require("globby"); + +let newVer = null; +let autoInc = null; +let buildNum = null; +let preRel = null; +let isRelease = false; +let testOnly = null; +let updateAll = true; + +const theVersion = require(process.cwd() + "/version.json"); +const orgPkgVersions = {}; + +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 + " [|-patch|-minor|-major|-next] [-dev|-alpha|-beta|-release] [-bld ######] [-test]"); + console.log("--------------------------"); + console.log(" - Identifies the version to set for all packages, must start with x.y.z"); + console.log(" -patch - Increment the current version to the next patch number (x.y.z => x.y.[z+1]"); + console.log(" -minor - Increment the current version to the next minor number (x.y.z => x.[y+1].0"); + console.log(" -major - Increment the current version to the next major number (x.y.z => [x+1].0.0"); + console.log(" -next - Increment the current version to the next value (patch, minor or major) based on the 'next' value in version.json"); + console.log(" -dev - Add the 'dev' pre-release to the number (x.y.z => x.y.z-dev)"); + console.log(" -alpha - Add the 'alpha' pre-release to the number (x.y.z => x.y.z-alpha)"); + console.log(" -beta - Add the 'beta' pre-release to the number (x.y.z => x.y.z-beta)"); + console.log(" -release - Remove any existing pre-release tags (x.y.z-prerel => x.y.z)"); + console.log(" -bld ###### - Append the provided build number to the version (x.y.z => x.y.z-[prerel].######) [prerel] defaults to dev if not defined"); + console.log(" -pre ###### - Set the pre-release to the provided value (x.y.z => x.y.z-[prerel])"); + console.log(" -react - Update only the react packages"); + console.log(" -reactNative - Update only the react native packages"); + console.log(" -test - Scan all of the package.json files and log the changes, but DON'T update the files"); +} + +function setPreRelVer(name) { + if (name && name.indexOf("-") !== -1) { + console.error("Invalid pre-release value -- [" + name + "] -- the name cannot contain a '-'"); + return false; + } + + preRel = name; + let defaultVer = theVersion[name]; + if (defaultVer) { + const idx = defaultVer.indexOf("-"); + if (idx == -1) { + newVer = defaultVer; + } else { + newVer = defaultVer.substring(0, idx); + preRel = defaultVer.substring(idx); + } + + if (newVer) { + updateAll = false; // We have mixed versions so we can't update all of them if we have a version# + } + } + + return true; +} + + +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 (!newVer && theArg === "-patch") { + console.log("Patch existing version"); + autoInc = "patch"; + } else if (!newVer && theArg === "-minor") { + console.log("Increment minor existing version"); + autoInc = "minor"; + } else if (!newVer && theArg === "-major") { + console.log("Increment major existing version"); + autoInc = "major"; + } else if (!newVer && theArg === "-next") { + console.log("Increment existing version based on autoInc"); + autoInc = "next"; + } else if (!isRelease && !preRel && theArg === "-dev") { + if (!setPreRelVer("dev")) { + return false; + } + } else if (!isRelease && !preRel && theArg === "-alpha") { + preRel = "alpha"; + if (!autoInc && !newVer) { + autoInc = "next"; + } else if (!setPreRelVer("alpha")) { + return false; + } + } else if (!isRelease && !preRel && theArg === "-beta") { + preRel = "beta"; + if (!autoInc && !newVer) { + autoInc = "next"; + } else if (!setPreRelVer("beta")) { + return false; + } + } else if (!isRelease && !preRel && theArg === "-release") { + isRelease = true; + preRel = ""; + } else if (!isRelease && !preRel && theArg === "-pre") { + if (!setPreRelVer(process.argv[idx + 1] || "pre")) { + return false; + } + idx++; + } else if (theArg === "-bld") { + buildNum = (process.argv[idx + 1] || ""); + idx++; + } else if (theArg === "-test") { + testOnly = true; + } else if (!isRelease && !newVer && !autoInc) { + let theParts = theArg.split("."); + if(theParts.length < 3) { + console.error("!!! The provided version [" + theArg + "] appears invalid"); + return false; + } + + newVer = theArg; + updateAll = false; // We have mixed versions so we can't update all of them if we have a version# + } else { + console.error("!!! Invalid Argument [" + theArg + "] detected"); + return false; + } + + idx ++; + } + + if (buildNum && !preRel && !isRelease) { + preRel = "dev"; + } + + // If no version, pre-release tag or auto version increment is defined default to "dev" pre-release + if (!newVer && !autoInc && !isRelease) { + const newPreRel = preRel; + if (!setPreRelVer("dev")) { + return false; + } + + if (newPreRel) { + preRel = newPreRel; + } + } + + return true; +} + +function updateVersions() { + // Get the configured next release, default to "patch" + const verNext = theVersion.next = theVersion.next || "patch"; + const rootVersion = require(process.cwd() + "/package.json"); + let newVersion = calculateVersion(rootVersion.version, verNext); + if (newVersion) { + console.log("New version [" + theVersion.release + "] => [" + newVersion + "]"); + if (updateAll) { + theVersion.release = newVersion; + } + } + + let packages = theVersion.pkgs = theVersion.pkgs || {}; + const keys = Object.keys(packages); + if (keys) { + for (let lp = 0; lp < keys.length; lp++) { + const value = keys[lp]; + + let packageDef = packages[value] = packages[value] || {}; + + if (fs.existsSync(process.cwd() + "//" + packageDef.package)) { + const thePackage = require(process.cwd() + "//" + packageDef.package); + const orgVersion = thePackage.version; + if (shouldUpdatePackage(value)) { + orgPkgVersions[value] = orgVersion; + let pkgNext = packageDef.next || verNext; + packageDef.release = calculateVersion(orgVersion, pkgNext); + console.log(" - " + value + ":[" + orgVersion + "] => [" + packages[value].release + "]"); + } else { + console.log(" - " + value + ":[" + orgVersion + "] => Skipping"); + } + } else { + console.log(" - " + value + ":[-- Missing package.json --] => Will remove package definition"); + delete packages[value]; + } + } + } + + return newVersion; +} + +function getNewPackageVersion(package, packageFilename) { + const packageName = package.name; + let packages = theVersion.pkgs = theVersion.pkgs || {}; + if (!packages[packageName]) { + let packageDef = packages[packageName] = packages[packageName] || {}; + packageDef.package = packageFilename; + + if (package.version) { + orgPkgVersions[packageName] = package.version; + + // We are not currently tracking this package so calculate based on the current package.version value + if (shouldUpdatePackage(package.name)) { + packageDef.release = calculateVersion(package.version, theVersion.next); + } else { + packageDef.release = package.version; + } + } else { + packageDef.release = theVersion.release; + } + } + + return packages[packageName].release; +} + +function calculateVersion(rootVersion, pkgAutoInc) { + + let preRelParts = (rootVersion || "0.0.0").split("-"); + let postfix = preRelParts.length > 1 && preRelParts[1] ? ("-" + preRelParts[1]) : ""; + let parts = preRelParts[0].split("."); + + if (parts.length < 3) { + console.error("!!! Package version [" + rootVersion + "] doesn't look correct"); + parts = ["0", "0", "0"]; + } + + let newVersion; + + if (newVer) { + newVersion = newVer; + postfix = ""; + } else { + if (autoInc !== "next") { + pkgAutoInc = autoInc; + } + + if (pkgAutoInc == "patch") { + parts[2]++; + } else if (pkgAutoInc == "minor") { + parts[1]++; + parts[2] = 0; + } else if (pkgAutoInc == "major") { + parts[0]++; + parts[1] = 0; + parts[2] = 0; + } else if (isRelease) { + // Don't update the numbers just remove the preRel + postfix = ""; + } + + newVersion = parts[0] + "." + parts[1] + "." + parts[2]; + } + + if (buildNum && !buildNum.startsWith(".") && !buildNum.startsWith("+")) { + buildNum = "." + buildNum; + } + + if (buildNum && isRelease) { + preRel = "rc"; + } + + if (preRel) { + postfix = preRel; + if (!postfix.startsWith("-")) { + postfix = "-" + preRel; + } + } + + return newVersion + (postfix || "") + (buildNum || ""); +} + +function getVersionDetails(theVersion) { + let parts = (theVersion || "0.0.0").split("+", 2); + + let details = { + full: theVersion, + ver: parts[0], + bldNum: parts.length === 2 ? parts[1] : "" + } + + let version = details.ver; + let preRelParts = version.split("-", 2); + details.preRel = preRelParts.length === 2 ? preRelParts[1] : ""; + + let type = "release"; + if (preRelParts[1]) { + // Remove all other possible separators + type = preRelParts[1].split(".")[0].split("-")[0]; + } + + details.type = type; + + return details; +} + +function shouldUpdatePackage(name) { + if (name.indexOf("-angularplugin-js") !== -1 || name.indexOf("-angularplugin-sample") !== -1) { + return true; + } else { + return updateAll; + } +} + +function shouldProcess(name) { + + if (name.indexOf("node_modules/") !== -1) { + return false; + } + + if (name.indexOf("common/temp") !== -1) { + return false; + } + + if (name.indexOf("legacy") !== -1) { + return false; + } + + if (name.indexOf("-angularplugin-js") !== -1 || name.indexOf("-angularplugin-sample") !== -1) { + return true; + } + + if (name === "package.json") { + return updateAll; + } + + return false; +} + +function updatePublishConfig(package, newVersion) { + let details = getVersionDetails(newVersion); + + if (!details.type || details.type === "release") { + if (package.publishConfig && package.publishConfig.tag) { + // remove any previous tag + delete package.publishConfig.tag; + } + } else { + if (!package.publishConfig) { + package.publishConfig = {}; + } + + // Set the publishing tag + package.publishConfig.tag = details.type; + } + + if (package.publishConfig && Object.keys(package.publishConfig).length === 0) { + // Nothing left so remove it + delete package.publishConfig; + } +} + +function updateDependencies(target, orgVersion, newVersion) { + if (target) { + Object.keys(target).forEach((value) => { + if (value.indexOf("@microsoft/applicationinsights-") !== -1 && + value.indexOf("@microsoft/applicationinsights-rollup") === -1) { + + let version = target[value]; + if (theVersion.pkgs[value]) { + let pkgVersion = theVersion.pkgs[value].release; + if (version.startsWith("^")) { + target[value] = "^" + pkgVersion; + } else if (version.startsWith("~")) { + target[value] = "~" + pkgVersion; + } else { + target[value] = pkgVersion; + } + } else { + if (version === orgVersion) { + target[value] = newVersion; + } + else if (version === "^" + orgVersion) { + target[value] = "^" + newVersion; + } + else if (version === "~" + orgVersion) { + target[value] = "~" + newVersion; + } + } + } + }); + } +} + +function updateVersion(src, orgVersion, newVersion) { + if (src) { + src = src.replace("\"javascript:" + orgVersion + "\"", "\"javascript:" + newVersion + "\""); + src = src.replace("\"" + orgVersion + "\"", "\"" + newVersion + "\""); + src = src.replace("\"#version#\"", "\"" + newVersion + "\""); + } + + return src; +} + +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); + let currentVersion = package.version; + let newVersion = getNewPackageVersion(package, theFilename); + + if (newVersion && currentVersion != newVersion) { + console.log(" Name - " + package.name + " Version: " + currentVersion + " => " + newVersion); + //fs.renameSync(inputFile, inputFile + ".org"); + package.version = newVersion; + updatePublishConfig(package, newVersion); + updateDependencies(package.dependencies, currentVersion, newVersion); + updateDependencies(package.peerDependencies, currentVersion, newVersion); + updateDependencies(package.devDependencies, currentVersion, newVersion); + + if (!testOnly) { + // Rewrite the file + const newContent = JSON.stringify(package, null, 4) + "\n"; + fs.writeFileSync(theFilename, newContent); + changed = true; + } + + if (theFilename.indexOf("/package.json") !== -1) { + let srcFolder = theFilename.replace("/package.json", "/**/*.ts", "/**/*.tsx", "/**/*.html"); + console.log(" - Checking source files: " + srcFolder); + const tsFiles = globby.sync(srcFolder); + tsFiles.map(sourceFile => { + // Don't update node_modules + if (shouldProcess(sourceFile)) { + + var src = fs.readFileSync(sourceFile, "utf8"); + var orgSrc = src; + + src = updateVersion(src, currentVersion, newVersion); + + // Rewrite the file + if (orgSrc != src && !testOnly) { + console.log(" Updating: " + sourceFile); + src = src.trim(); + fs.writeFileSync(sourceFile, src); + } + } + }); + } + } else { + console.log(" Name - " + package.name + " Version: " + currentVersion + " => Skipped"); + } + } + }); + + return changed; +}; + +if (parseArgs()) { + let changed = false; + let newVersion = updateVersions(); + if (newVersion) { + if (setPackageJsonRelease()) { + console.log("Version updated, now run 'npm run update'"); + changed = true; + } else { + console.warn("Nothing Changed!!!"); + } + + if (!testOnly && changed) { + console.log("Updating version file"); + if (autoInc) { + // We did an automatic update so reset to patch for the next one. + theVersion.next = "patch"; + } + // Rewrite the file + const newContent = JSON.stringify(theVersion, null, 4) + "\n"; + fs.writeFileSync(process.cwd() + "/version.json", newContent); + } + + } else { + console.error("Failed to identify the new version number"); + } +} else { + showHelp(); +} diff --git a/version.json b/version.json new file mode 100644 index 0000000..251b2c9 --- /dev/null +++ b/version.json @@ -0,0 +1,16 @@ +{ + "description": "The release value identifies the base version that will be applied via the tools/release-tools/setVersion.js", + "usage": "When creating a new release you should update this value directly or via the eg. 'npm run setVersion -- 3.2.0' or 'npm run setVersion -- -patch' or 'npm run setVersion -- -minor'", + "release": "15.3.4", + "next": "patch", + "pkgs": { + "@microsoft/applicationinsights-angularplugin-js": { + "package": "package.json", + "release": "15.3.4" + }, + "applicationinsights-angularplugin-sample": { + "package": "sample/applicationinsights-angularplugin-sample/package.json", + "release": "0.3.0" + } + } +}