1
1
const { spawn } = require ( "child_process" ) ;
2
- const fs = require ( 'fs' ) ;
2
+ const fs = require ( 'fs/promises ' ) ;
3
3
const process = require ( 'process' )
4
4
5
5
async function runProcess ( command , args ) {
@@ -21,60 +21,88 @@ async function runProcess(command, args) {
21
21
} ) ;
22
22
}
23
23
24
- const VERSION_DIFF_REGEX = / ^ \+ \s * " v e r s i o n " : \s * " ( [ 0 - 9 \. ] + ) " .* $ /
25
- const VERSION_DIFF_OLD_REGEX = / ^ \- \s * " v e r s i o n " : \s * " ( [ 0 - 9 \. ] + ) " .* $ /
24
+ const VERSION_DIFF_REGEX = / ^ \+ \s * " v e r s i o n " \s * : \s * " ( [ ^ " ] + ) " .* $ / // '+ "version": "1.4.17",'
25
+ const VERSION_DIFF_OLD_REGEX = / ^ \- \s * " v e r s i o n " \s * : \s * " ( [ ^ " ] + ) " .* $ / // '- "version": "1.4.16",'
26
26
27
27
function formatForGithubActions ( markdownString ) {
28
+ // Replaces all occurrences of \n with literal character '%0A'
29
+ // This is needed to work around Github action output variables
28
30
return markdownString . replace ( / \n / g, "%0A" ) ;
29
31
}
30
32
33
+ //extracts matching lines
31
34
function extractVersionFromLines ( lines , regex ) {
32
35
const [ line ] = lines
33
36
. filter ( line => regex . test ( line ) ) ;
34
37
35
38
const regexResult = regex . exec ( line ) ;
36
39
37
40
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
39
42
}
40
43
41
44
return regexResult [ 1 ] ;
42
45
}
43
46
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
+
44
62
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
48
66
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` ] ) ;
52
69
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 ;
54
72
73
+ //output of git diff separated by newlines for further processing
55
74
const lines = diff . split ( "\n" ) ;
56
75
57
76
const newVersion = extractVersionFromLines ( lines , VERSION_DIFF_REGEX ) ;
58
77
const oldVersion = extractVersionFromLines ( lines , VERSION_DIFF_OLD_REGEX ) ;
59
78
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
60
84
moduleToNewVersion [ module ] = newVersion ;
85
+ // adds the retrieved older version to the map [msal-node]:1.4.1
61
86
moduleToOldVersion [ module ] = oldVersion ;
62
87
63
88
}
64
89
65
90
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 |
66
94
const modList = Object . keys ( moduleToOldVersion )
67
95
. map ( module => `| ${ module } | ${ moduleToOldVersion [ module ] } | ${ moduleToNewVersion [ module ] } |` )
68
96
. join ( "\n" ) ;
69
97
70
-
98
+ // Define a Markdown table header
71
99
let tableHeader = "| Module | Old Version | New Version |\n" ;
72
100
tableHeader += "| --- | --- | --- |\n"
73
101
74
102
75
103
return formatForGithubActions ( `The following modules have had their versions bumped:\n ${ tableHeader } ${ modList } ` ) ;
76
104
} ;
77
105
78
- ( async ( ) => {
79
- console . log ( await getBumpedModules ( ) ) ;
80
- } ) ( ) ;
106
+ getBumpedModules ( ) . then ( ( bumpedModulesDescription ) => {
107
+ console . log ( bumpedModulesDescription ) ;
108
+ } ) ;
0 commit comments