Skip to content

Commit 4199d30

Browse files
committed
Added more options to findFileBySuffix(), refactored logic for readability
1 parent eebbc9b commit 4199d30

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

utils/bump/bump-utils.mjs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,37 @@ export function bumpDateVer({ filePath, verbose = true } = {}) { // bumps YYYY.M
4141
return { oldVer, newVer }
4242
}
4343

44-
export function findFileBySuffix({ suffix, dir = global.monorepoRoot, verbose = true } = {}) {
44+
export function findFileBySuffix({
45+
suffix, // string filename ending to search for (e.g., '.user.js', 'manifest.json')
46+
dir = global.monorepoRoot, // string dir to start search from
47+
verbose = true, // boolean to log found files to console
48+
recursive = true, // boolean to search subdirs recursively
49+
dotFolders = false, // boolean to include hidden folders
50+
dotFiles = false, // boolean to include hidden files
51+
ignoreFiles = [] // array of filenames to exclude from results
52+
} = {}) {
53+
4554
if (!suffix) throw new Error(`'suffix' option required by findFileBySuffix()`)
46-
const foundFiles = []
4755
if (!dir && !global.monorepoRoot) {
4856
dir = path.dirname(fileURLToPath(import.meta.url))
4957
while (!fs.existsSync(path.join(dir, 'package.json'))) dir = path.dirname(dir)
5058
global.monorepoRoot = dir
5159
}
52-
dir = path.resolve(dir)
60+
61+
const foundFiles = []
5362
;(function search(currentDir) {
54-
fs.readdirSync(currentDir).forEach(entry => {
55-
if (/^(?:\.|node_modules$)/.test(entry)) return
56-
const entryPath = path.join(currentDir, entry)
57-
if (fs.statSync(entryPath).isDirectory()) search(entryPath)
58-
else if (entry.endsWith(suffix)) { if (verbose) console.log(entryPath) ; foundFiles.push(entryPath) }
59-
})
60-
})(dir)
63+
for (const entry of fs.readdirSync(currentDir)) {
64+
if (entry.startsWith('.') && !dotFolders && fs.statSync(path.join(currentDir, entry)).isDirectory())
65+
continue // skip dotfolders if disabled
66+
if (entry == 'node_modules') continue
67+
const entryPath = path.join(currentDir, entry), stat = fs.statSync(entryPath)
68+
if (stat.isDirectory() && recursive) search(entryPath)
69+
else if (stat.isFile() && entry.endsWith(suffix)
70+
&& (dotFiles || !entry.startsWith('.'))
71+
&& !ignoreFiles.includes(entry)
72+
) { foundFiles.push(entryPath) ; if (verbose) console.log(entryPath) }
73+
}
74+
})(path.resolve(dir))
6175
return foundFiles
6276
}
6377

0 commit comments

Comments
 (0)