Skip to content

Commit 75513dc

Browse files
committed
Supporting comments added ; support for extensions
1 parent 4160c01 commit 75513dc

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

release-scripts/getUpdatedVersions.js

+43-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { spawn } = require("child_process");
2-
const fs = require('fs');
2+
const fs = require('fs/promises');
33
const process = require('process')
44

55
async function runProcess(command, args) {
@@ -21,60 +21,88 @@ async function runProcess(command, args) {
2121
});
2222
}
2323

24-
const VERSION_DIFF_REGEX = /^\+\s*"version":\s*"([0-9\.]+)".*$/
25-
const VERSION_DIFF_OLD_REGEX = /^\-\s*"version":\s*"([0-9\.]+)".*$/
24+
const VERSION_DIFF_REGEX = /^\+\s*"version"\s*:\s*"([^"]+)".*$/ // '+ "version": "1.4.17",'
25+
const VERSION_DIFF_OLD_REGEX = /^\-\s*"version"\s*:\s*"([^"]+)".*$/ // '- "version": "1.4.16",'
2626

2727
function formatForGithubActions(markdownString) {
28+
// Replaces all occurrences of \n with literal character '%0A'
29+
// This is needed to work around Github action output variables
2830
return markdownString.replace(/\n/g, "%0A");
2931
}
3032

33+
//extracts matching lines
3134
function extractVersionFromLines(lines, regex) {
3235
const [line] = lines
3336
.filter(line => regex.test(line));
3437

3538
const regexResult = regex.exec(line);
3639

3740
if (!regexResult || !regexResult[1]) {
38-
throw new Error("Cannot parse version for module " + module)
41+
return '???'; // could not parse version from the diff, return default value
3942
}
4043

4144
return regexResult[1];
4245
}
4346

47+
const extensionModulePaths = [
48+
'./extensions/msal-node-extensions'
49+
]
50+
51+
//returns the path of modules in lib as well as extensions path
52+
//as specified by the extensionModulePaths above.
53+
async function getModulePaths() {
54+
const libRoot = './lib';
55+
const libDirNames = await fs.readdir(libRoot);
56+
57+
const libDirPaths = libDirNames.map(lib => `${libRoot}/${lib}`); //parent dir is needed to handle the extensions case
58+
59+
return [...libDirPaths, ...extensionModulePaths];
60+
}
61+
4462
async function getBumpedModules() {
45-
const modules = fs.readdirSync('./lib');
46-
const moduleToNewVersion = {};
47-
const moduleToOldVersion = {};
63+
const modulePaths = await getModulePaths();
64+
const moduleToNewVersion = {}; // map of module name to the new version it was bumped to
65+
const moduleToOldVersion = {}; // map of module name to its old version before the bump
4866

49-
for (let i in modules) {
50-
const module = modules[i];
51-
const diff = await runProcess('git', ['diff', `./lib/${module}/package.json`]);
67+
for (let modulePath of modulePaths) {
68+
const diff = await runProcess('git', ['diff', `${modulePath}/package.json`]);
5269

53-
if (diff.trim().length < 1) continue;
70+
// skips to next module if git diff output is empty, i.e. this module was unchanged
71+
if (diff.trim().length < 1) continue;
5472

73+
//output of git diff separated by newlines for further processing
5574
const lines = diff.split("\n");
5675

5776
const newVersion = extractVersionFromLines(lines, VERSION_DIFF_REGEX);
5877
const oldVersion = extractVersionFromLines(lines, VERSION_DIFF_OLD_REGEX);
5978

79+
//Fetch the module name from the path
80+
const modulePathComponents = modulePath.split('/');
81+
const module = modulePathComponents[modulePathComponents.length - 1];
82+
83+
// adds the retrieved newer version to the map [msal-node]:1.4.2
6084
moduleToNewVersion[module] = newVersion;
85+
// adds the retrieved older version to the map [msal-node]:1.4.1
6186
moduleToOldVersion[module] = oldVersion;
6287

6388
}
6489

6590

91+
//prepare the version information for applicable modules from the maps in row format
92+
// example- | msal-browser | 1.4.16 | 1.4.17 |
93+
// | msal-node | 1.4.1 | 1.4.2 |
6694
const modList = Object.keys(moduleToOldVersion)
6795
.map(module => `| ${module} | ${moduleToOldVersion[module]} | ${moduleToNewVersion[module]} |`)
6896
.join("\n");
6997

70-
98+
// Define a Markdown table header
7199
let tableHeader = "| Module | Old Version | New Version |\n";
72100
tableHeader += "| --- | --- | --- |\n"
73101

74102

75103
return formatForGithubActions(`The following modules have had their versions bumped:\n ${tableHeader}${modList}`);
76104
};
77105

78-
(async () => {
79-
console.log(await getBumpedModules());
80-
})();
106+
getBumpedModules().then((bumpedModulesDescription) => {
107+
console.log(bumpedModulesDescription);
108+
});

0 commit comments

Comments
 (0)