|
| 1 | +// @ts-check |
| 2 | +/* eslint-disable security/detect-object-injection */ |
| 3 | +const fs = require("fs"); |
| 4 | +const path = require("path"); |
| 5 | +const pathToPosix = require("../../utl/path-to-posix"); |
| 6 | +const getExtension = require("../../utl/get-extension.js"); |
| 7 | +const meta = require("../../extract/transpile/meta"); |
| 8 | + |
| 9 | +/** |
| 10 | + * @param {string[]} pIgnorablePathElements |
| 11 | + * @returns {(string) => boolean} |
| 12 | + */ |
| 13 | +function notIgnorable(pIgnorablePathElements) { |
| 14 | + return (pPath) => { |
| 15 | + return !pIgnorablePathElements.includes(pPath); |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * @param {string} pFullPathToFile |
| 21 | + * @param {string} pBaseDirectory |
| 22 | + * @returns {boolean} |
| 23 | + */ |
| 24 | +function fileIsDirectory(pFullPathToFile, pBaseDirectory) { |
| 25 | + try { |
| 26 | + const lStat = fs.statSync(path.join(pBaseDirectory, pFullPathToFile)); |
| 27 | + return lStat.isDirectory(); |
| 28 | + } catch (pError) { |
| 29 | + return false; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @param {string} pDirectoryName |
| 35 | + * @param {{baseDir: string; ignorablePathElements: string[]}} pOptions |
| 36 | + * @returns {string[]} |
| 37 | + */ |
| 38 | +function listAllModules(pDirectoryName, { baseDir, ignorablePathElements }) { |
| 39 | + return fs |
| 40 | + .readdirSync(path.join(baseDir, pDirectoryName)) |
| 41 | + .filter(notIgnorable(ignorablePathElements)) |
| 42 | + .map((pFileName) => path.join(pDirectoryName, pFileName)) |
| 43 | + .map((pFullPathToFile) => ({ |
| 44 | + fullPathToFile: pFullPathToFile, |
| 45 | + isDirectory: fileIsDirectory(pFullPathToFile, baseDir), |
| 46 | + })) |
| 47 | + .reduce( |
| 48 | + /** |
| 49 | + * @param {string[]} pSum |
| 50 | + * @param {{fullPathToFile: string; isDirectory: boolean}} pCurrentValue |
| 51 | + * @returns {string[]} |
| 52 | + */ |
| 53 | + (pSum, { fullPathToFile, isDirectory }) => { |
| 54 | + if (isDirectory) { |
| 55 | + return pSum.concat( |
| 56 | + listAllModules(fullPathToFile, { baseDir, ignorablePathElements }) |
| 57 | + ); |
| 58 | + } |
| 59 | + return pSum.concat(fullPathToFile); |
| 60 | + }, |
| 61 | + [] |
| 62 | + ) |
| 63 | + .map((pFullPathToFile) => pathToPosix(pFullPathToFile)); |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * @param {Record<string,number>} pAll |
| 68 | + * @param {string} pExtension |
| 69 | + */ |
| 70 | +function reduceToCounts(pAll, pExtension) { |
| 71 | + if (pAll[pExtension]) { |
| 72 | + pAll[pExtension] += 1; |
| 73 | + } else { |
| 74 | + pAll[pExtension] = 1; |
| 75 | + } |
| 76 | + return pAll; |
| 77 | +} |
| 78 | + |
| 79 | +function compareByCount(pCountsObject) { |
| 80 | + return function compare(pLeft, pRight) { |
| 81 | + return pCountsObject[pRight] - pCountsObject[pLeft]; |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * @param {string[]} pDirectories |
| 87 | + * @param {{baseDir?: string; ignorablePathElements?: string[], scannableExtensions?: string[]}=} pOptions |
| 88 | + * @returns {string[]} |
| 89 | + */ |
| 90 | +module.exports = function findExtensions(pDirectories, pOptions) { |
| 91 | + const lOptions = { |
| 92 | + baseDir: process.cwd(), |
| 93 | + ignorablePathElements: [ |
| 94 | + ".git", |
| 95 | + ".husky", |
| 96 | + ".vscode", |
| 97 | + "coverage", |
| 98 | + "node_nodules", |
| 99 | + "nyc", |
| 100 | + ], |
| 101 | + scannableExtensions: meta.scannableExtensions, |
| 102 | + ...pOptions, |
| 103 | + }; |
| 104 | + |
| 105 | + const lExtensionsWithCounts = pDirectories |
| 106 | + .flatMap((pDirectory) => |
| 107 | + listAllModules(pDirectory, lOptions).map(getExtension).filter(Boolean) |
| 108 | + ) |
| 109 | + .reduce(reduceToCounts, {}); |
| 110 | + |
| 111 | + return Object.keys(lExtensionsWithCounts) |
| 112 | + .filter((pExtension) => lOptions.scannableExtensions.includes(pExtension)) |
| 113 | + .sort(compareByCount(lExtensionsWithCounts)); |
| 114 | +}; |
0 commit comments