11const actionUtils = require ( '../action-utils.js' ) ;
2+ const fs = require ( 'fs' ) ;
3+ const path = require ( 'path' ) ;
24const prevPathPrefix = "prev/" ;
35const linePrefix = "#L" ;
46const separator = " | " ;
57
6- modifiedFilesDict = { } ;
8+ modifiedFilesPathToLearningPathFile = { } ;
79modifiedFilesUrlToFileName = { } ;
810
911var outOfSync = new Set ( ) ;
@@ -13,14 +15,6 @@ var modifiedFiles = new Set();
1315
1416const oldNewLinkSeparator = ' -> ' ;
1517
16- let modifiedFilesToCommit = [ ] ;
17-
18- function AppendModifiedFilesToCommit ( path , core )
19- {
20- modifiedFilesToCommit . push ( path )
21- core . setOutput ( 'modifiedFilesToCommit' , modifiedFilesToCommit . join ( ' ' ) )
22- }
23-
2418function ReplaceOldWithNewText ( content , oldText , newText )
2519{
2620 return content . replaceAll ( oldText , newText ) ;
@@ -30,14 +24,14 @@ function UpdateModifiedFiles(fileName, path, learningPathFile)
3024{
3125 modifiedFilesUrlToFileName [ path ] = fileName ;
3226
33- modifiedFilesDict [ path ] = modifiedFilesDict [ path ] ? modifiedFilesDict [ path ] : new Set ( ) ; ;
34- modifiedFilesDict [ path ] . add ( learningPathFile ) ;
27+ modifiedFilesPathToLearningPathFile [ path ] = modifiedFilesPathToLearningPathFile [ path ] ? modifiedFilesPathToLearningPathFile [ path ] : new Set ( ) ; ;
28+ modifiedFilesPathToLearningPathFile [ path ] . add ( learningPathFile ) ;
3529
3630 modifiedFiles = new Set ( ) ;
37- for ( currPath in modifiedFilesDict )
31+ for ( currPath in modifiedFilesPathToLearningPathFile )
3832 {
3933 const fileName = modifiedFilesUrlToFileName [ currPath ] ;
40- modifiedFiles . add ( AssembleModifiedFilesOutput ( fileName , currPath , Array . from ( modifiedFilesDict [ currPath ] ) ) ) ;
34+ modifiedFiles . add ( AssembleModifiedFilesOutput ( fileName , currPath , Array . from ( modifiedFilesPathToLearningPathFile [ currPath ] ) ) ) ;
4135 }
4236}
4337
@@ -92,7 +86,7 @@ function AppendLineNumber(text, lineNumber)
9286
9387function CheckForEndOfLink ( str , startIndex )
9488{
95- const illegalCharIndex = str . substr ( startIndex ) . search ( "[(), '\`\"\}\{]|\. " ) ; // This regex isn't perfect, but should cover most cases.
89+ const illegalCharIndex = str . substr ( startIndex ) . search ( "[(), '\`\"\}\{]|\. |\.\n " ) ; // This regex isn't perfect, but should cover most cases.
9690 return illegalCharIndex ;
9791}
9892
@@ -103,7 +97,7 @@ function StripLineNumber(link, linePrefixIndex)
10397
10498function GetContent ( path ) {
10599 try {
106- return actionUtils . readFileSync ( path )
100+ return fs . readFileSync ( path , 'utf8' ) ;
107101 }
108102 catch ( error ) { }
109103
@@ -112,21 +106,17 @@ function GetContent(path) {
112106
113107function ConstructOutputText ( core )
114108{
115- var manuallyReviewSection = "<h2>Manually Review:</h2>" + Array . from ( manuallyReview ) . join ( "<br />" ) + "<br />" ;
116- if ( manuallyReview . size === 0 ) { manuallyReviewSection = "" ; }
109+ var body = "" ;
117110
118- var outOfSyncSection = "<h2>Links With Out Of Sync Commit Hashes:</h2>" + Array . from ( outOfSync ) . join ( "<br />" ) + "<br />" ;
119- if ( outOfSync . size === 0 ) { outOfSyncSection = "" ; }
111+ if ( manuallyReview . size > 0 ) { body += "<h2>Manually Review:</h2>" + Array . from ( manuallyReview ) . join ( "<br />" ) + "<br />" ; }
120112
121- var suggestionsSection = "<h2>Auto-Applied Suggestions:</h2>" + Array . from ( suggestions ) . join ( "<br />" ) + "<br />" ;
122- if ( suggestions . size === 0 ) { suggestionsSection = "" ; }
113+ if ( outOfSync . size > 0 ) { body += "<h2>Links With Out Of Sync Commit Hashes:</h2>" + Array . from ( outOfSync ) . join ( "<br />" ) + "<br />" ; }
123114
124- var modifiedFilesSection = "<h2>Modified Files:</h2>" + Array . from ( modifiedFiles ) . join ( "<br />" ) + "<br />" ;
125- if ( modifiedFiles . size === 0 ) { modifiedFilesSection = "" ; }
115+ if ( suggestions . size > 0 ) { body += "<h2>Auto-Applied Suggestions:</h2>" + Array . from ( suggestions ) . join ( "<br />" ) + "<br />" ; }
126116
127- var body = modifiedFilesSection + manuallyReviewSection + outOfSyncSection + suggestionsSection ;
128- console . log ( "body=" + body ) ;
117+ if ( modifiedFiles . size > 0 ) { body += "<h2>Modified Files:</h2>" + Array . from ( modifiedFiles ) . join ( "<br />" ) + "<br />" ; }
129118
119+ console . log ( "body=" + body ) ;
130120 core . setOutput ( 'outputText' , body ) ;
131121}
132122
@@ -142,11 +132,13 @@ function ValidateLinks(learningPathContents, repoURLToSearch, modifiedPRFiles, l
142132 {
143133 // Clean up the link, determine if it has a line number suffix
144134 const endOfLink = startOfLink + CheckForEndOfLink ( learningPathContents , startOfLink )
135+
145136 const link = learningPathContents . substring ( startOfLink , endOfLink ) ;
146137
147138 if ( excludeLinksArray . some ( excludeLink => link . toLowerCase ( ) . includes ( excludeLink ) ) ) { continue ; }
148139
149140 const pathStartIndex = link . indexOf ( sourceDirectoryName ) ;
141+
150142 if ( pathStartIndex === - 1 ) { continue }
151143
152144 if ( ! link . includes ( oldHash ) )
@@ -225,10 +217,10 @@ const main = async () => {
225217 if ( changedFilePaths === null || changedFilePaths . trim ( ) === "" ) { return }
226218
227219 // Scan each file in the learningPaths directory
228- actionUtils . readdir ( learningPathDirectory , ( _ , files ) => {
220+ fs . readdir ( learningPathDirectory , ( _ , files ) => {
229221 files . forEach ( learningPathFile => {
230222 try {
231- const learningPathContents = GetContent ( learningPathDirectory + "/" + learningPathFile )
223+ const learningPathContents = GetContent ( path . join ( learningPathDirectory , learningPathFile ) )
232224 if ( learningPathContents )
233225 {
234226 ValidateLinks ( learningPathContents , repoURLToSearch , changedFilePaths . split ( ' ' ) , learningPathFile , oldHash , newHash , sourceDirectoryName , excludeLinksArray )
@@ -241,17 +233,15 @@ const main = async () => {
241233 } ) ;
242234 } ) ;
243235
244- actionUtils . writeFileSync ( learningPathHashFile , newHash ) ;
245- AppendModifiedFilesToCommit ( learningPathHashFile , core )
236+ fs . writeFileSync ( learningPathHashFile , newHash ) ;
246237
247238 // Scan each file in the learningPaths directory
248- actionUtils . readdir ( learningPathDirectory , ( _ , files ) => {
239+ fs . readdir ( learningPathDirectory , ( _ , files ) => {
240+
249241 files . forEach ( learningPathFile => {
250242 try {
251- const fullPath = learningPathDirectory + "/" + learningPathFile
252- const content = actionUtils . readFileSync ( fullPath )
253-
254- var replacedContent = content
243+ const fullPath = path . join ( learningPathDirectory , learningPathFile )
244+ let content = fs . readFileSync ( fullPath , 'utf8' )
255245
256246 let suggestionsArray = Array . from ( suggestions ) ;
257247 if ( suggestionsArray && suggestionsArray . length > 0 ) {
@@ -261,17 +251,12 @@ const main = async () => {
261251 var newLink = suggestionArray [ 1 ]
262252 oldLink = oldLink . substring ( oldLink . indexOf ( '(' ) + 1 , oldLink . lastIndexOf ( ')' ) )
263253 newLink = newLink . substring ( newLink . indexOf ( '(' ) + 1 , newLink . lastIndexOf ( ')' ) )
264- replacedContent = ReplaceOldWithNewText ( replacedContent , oldLink , newLink )
254+ content = ReplaceOldWithNewText ( content , oldLink , newLink )
265255 } )
266256 }
267257
268- replacedContent = ReplaceOldWithNewText ( replacedContent , oldHash , newHash )
269-
270- actionUtils . writeFileSync ( fullPath , replacedContent ) ;
271-
272- if ( content !== replacedContent ) {
273- AppendModifiedFilesToCommit ( fullPath , core )
274- }
258+ content = ReplaceOldWithNewText ( content , oldHash , newHash )
259+ fs . writeFileSync ( fullPath , content ) ;
275260 } catch ( error ) {
276261 console . log ( "Error: " + error )
277262 console . log ( "Could not find learning path file: " + learningPathFile )
0 commit comments