1+ import fs from 'fs'
2+ import path from 'path'
3+
4+ function readFilesRecursively ( directory ) {
5+ const filenames = fs . readdirSync ( directory )
6+ const files = { }
7+
8+ filenames . forEach ( ( filename ) => {
9+ const filePath = path . join ( directory , filename )
10+ const fileStats = fs . statSync ( filePath )
11+
12+ if ( fileStats . isDirectory ( ) ) {
13+ const nestedFiles = readFilesRecursively ( filePath )
14+ for ( const [ nestedFilePath , nestedFileContent ] of Object . entries ( nestedFiles ) ) {
15+ files [ path . join ( filename , nestedFilePath ) ] = nestedFileContent
16+ }
17+ } else {
18+ files [ filename ] = fs . readFileSync ( filePath , 'utf-8' )
19+ }
20+ } )
21+
22+ return files
23+ }
24+
25+ function upperCaseFirstLetter ( string ) {
26+ return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 )
27+ }
28+
29+ function displayDiffs ( dir1Files , dir2Files , isOpen ) {
30+ const rollupGrouping = { }
31+ /**
32+ * Rolls up multiple files with the same diff into a single entry
33+ * @param {string } fileName
34+ * @param {string } string
35+ * @param {string } [summary]
36+ */
37+ function add ( fileName , string , summary = undefined ) {
38+ if ( summary === undefined ) {
39+ summary = string
40+ }
41+ if ( ! ( summary in rollupGrouping ) ) {
42+ rollupGrouping [ summary ] = { files : [ ] }
43+ }
44+ rollupGrouping [ summary ] . files . push ( fileName )
45+ rollupGrouping [ summary ] . string = string
46+ }
47+ for ( const [ filePath , fileContent ] of Object . entries ( dir1Files ) ) {
48+ let diffOut = ''
49+ let compareOut = undefined
50+ if ( filePath in dir2Files ) {
51+ const fileOut = fileContent
52+ const file2Out = dir2Files [ filePath ]
53+ delete dir2Files [ filePath ]
54+ if ( fileOut === file2Out ) {
55+ continue
56+ } else {
57+ compareOut = filePath . split ( '/' ) [ 0 ]
58+ diffOut = `File has changed`
59+ }
60+ } else {
61+ diffOut = '❌ File only exists in old changeset'
62+ compareOut = 'Removed Files'
63+ }
64+ add ( filePath , diffOut , compareOut )
65+ }
66+
67+ for ( const filePath of Object . keys ( dir2Files ) ) {
68+ add ( filePath , '❌ File only exists in new changeset' , 'New Files' )
69+ }
70+ const outString = Object . keys ( rollupGrouping ) . map ( key => {
71+ const rollup = rollupGrouping [ key ]
72+ let outString = ''
73+ let title = key
74+ if ( rollup . files . length ) {
75+ for ( const file of rollup . files ) {
76+ outString += `- ${ file } \n`
77+ }
78+ }
79+ outString += '\n\n' + rollup . string
80+ return renderDetails ( title , outString , isOpen )
81+ } ) . join ( '\n' )
82+ return outString
83+ }
84+
85+ function renderDetails ( section , text , isOpen ) {
86+ if ( section == 'dist' ) {
87+ section = 'apple'
88+ }
89+ const open = section !== 'integration' ? 'open' : ''
90+ return `<details ${ open } >
91+ <summary>${ upperCaseFirstLetter ( section ) } </summary>
92+ ${ text }
93+ </details>`
94+ }
95+
96+ if ( process . argv . length !== 4 ) {
97+ console . error ( 'Usage: node diff_directories.js <directory1> <directory2>' )
98+ process . exit ( 1 )
99+ }
100+
101+ const dir1 = process . argv [ 2 ]
102+ const dir2 = process . argv [ 3 ]
103+
104+ const sections = {
105+ }
106+ function sortFiles ( dirFiles , dirName ) {
107+ for ( const [ filePath , fileContent ] of Object . entries ( dirFiles ) ) {
108+ sections [ dirName ] = sections [ dirName ] || { }
109+ sections [ dirName ] [ filePath ] = fileContent
110+ }
111+ }
112+
113+ const buildDir = '/build'
114+ const sourcesOutput = '/Sources/ContentScopeScripts/'
115+ sortFiles ( readFilesRecursively ( dir1 + buildDir ) , 'dir1' )
116+ sortFiles ( readFilesRecursively ( dir2 + buildDir ) , 'dir2' )
117+ sortFiles ( readFilesRecursively ( dir1 + sourcesOutput ) , 'dir1' )
118+ sortFiles ( readFilesRecursively ( dir2 + sourcesOutput ) , 'dir2' )
119+
120+
121+ //console.log(Object.keys(files))
122+ const fileOut = displayDiffs ( sections . dir1 , sections . dir2 , true )
123+ console . log ( fileOut )
0 commit comments