MCPcopy Create free account
hub / github.com/epicweb-dev/full-stack-foundations / dirsAreTheSame

Function dirsAreTheSame

epicshop/fix.js:158–199  ·  view source on GitHub ↗
(dir1, dir2, exclude = [])

Source from the content-addressed store, hash-verified

156}
157
158async function dirsAreTheSame(dir1, dir2, exclude = []) {
159 const dir1Exists = exists(dir1)
160 const dir2Exists = exists(dir2)
161 // if they both don't exist, they're the same
162 if (!dir1Exists && !dir2Exists) return true
163
164 // if only one doesn't exist, they'd different
165 if (dir1Exists !== dir2Exists) return false
166
167 // Read the content of both directories
168 const dir1Contents = await readDir(dir1)
169 const dir2Contents = await readDir(dir2)
170
171 // Filter the content based on the exclude array
172 const dir1FilteredContents = dir1Contents.filter(
173 filename => !exclude.some(regex => regex.test(filename)),
174 )
175 const dir2FilteredContents = dir2Contents.filter(
176 filename => !exclude.some(regex => regex.test(filename)),
177 )
178
179 // Check if the number of files and directories are the same
180 if (dir1FilteredContents.length !== dir2FilteredContents.length) return false
181
182 // Check the content recursively
183 for (const item of dir1FilteredContents) {
184 const itemPath1 = path.join(dir1, item)
185 const itemPath2 = path.join(dir2, item)
186
187 // Check if the item is a file and compare the content
188 if (isFile(itemPath1) && isFile(itemPath2)) {
189 if (!isSameFile(itemPath1, itemPath2)) return false
190 } else {
191 // If the item is a directory, call the function recursively
192 const result = await dirsAreTheSame(itemPath1, itemPath2, exclude)
193 if (!result) return false
194 }
195 }
196
197 // If no differences were found, return true
198 return true
199}
200
201async function isNewer(maybeOlder, maybeNewer, exclude = []) {
202 const olderStat = await getNewestStat(maybeOlder, exclude)

Callers 1

updateDataDbFunction · 0.85

Calls 4

existsFunction · 0.85
readDirFunction · 0.85
isFileFunction · 0.85
isSameFileFunction · 0.85

Tested by

no test coverage detected