|
| 1 | +import * as glob from "fast-glob"; |
| 2 | +import * as path from "path"; |
| 3 | + |
| 4 | +export interface EntryFileInfo { |
| 5 | + moduleName: string; |
| 6 | + fileRelativePath: string; |
| 7 | + index: number; |
| 8 | + baseDir: string; |
| 9 | +} |
| 10 | + |
| 11 | +export type EntryFileFormatterSync = ( |
| 12 | + info: EntryFileInfo, |
| 13 | +) => readonly [string, string]; |
| 14 | + |
| 15 | +export type EntryFileFormatter = ( |
| 16 | + ...args: Parameters<EntryFileFormatterSync> |
| 17 | +) => |
| 18 | + | Promise<ReturnType<EntryFileFormatterSync>> |
| 19 | + | ReturnType<EntryFileFormatterSync>; |
| 20 | + |
| 21 | +interface MatchFilesOptions { |
| 22 | + patterns: string | string[]; |
| 23 | + baseDir: string; |
| 24 | + ignore: string | string[]; |
| 25 | +} |
| 26 | + |
| 27 | +export interface GetEntryFilesOptionsCommon extends Partial<MatchFilesOptions> { |
| 28 | + keepIndexFiles?: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +export interface GetEntryFilesOptions extends GetEntryFilesOptionsCommon { |
| 32 | + formatter?: EntryFileFormatter; |
| 33 | +} |
| 34 | + |
| 35 | +export interface GetEntryFilesOptionsSync extends GetEntryFilesOptionsCommon { |
| 36 | + formatter?: EntryFileFormatterSync; |
| 37 | +} |
| 38 | + |
| 39 | +function getGlobArgs({ |
| 40 | + patterns, |
| 41 | + baseDir, |
| 42 | + ignore, |
| 43 | +}: MatchFilesOptions): Parameters<typeof glob> { |
| 44 | + return [ |
| 45 | + patterns, |
| 46 | + { |
| 47 | + cwd: baseDir, |
| 48 | + ignore: typeof ignore === "string" ? [ignore] : ignore, |
| 49 | + }, |
| 50 | + ]; |
| 51 | +} |
| 52 | + |
| 53 | +export const DEFAULT_PATTERNS_EXT = "js,jsx,ts,tsx,json"; |
| 54 | +export const DEFAULT_PATTERNS = [ |
| 55 | + `./*.{${DEFAULT_PATTERNS_EXT}}`, |
| 56 | + `./*/index.{${DEFAULT_PATTERNS_EXT}}`, |
| 57 | +]; |
| 58 | + |
| 59 | +export const DEFAULT_IGNORE = [ |
| 60 | + "**/*.d.ts", |
| 61 | + `**/*.{test,spec}.{${DEFAULT_PATTERNS_EXT}}`, |
| 62 | + `**/{__test__,__spec__}/**`, |
| 63 | +]; |
| 64 | + |
| 65 | +function getModuleNameFromFile(info: path.ParsedPath) { |
| 66 | + return path.posix.join(info.dir, info.name); |
| 67 | +} |
| 68 | + |
| 69 | +function isSubDirIndexFile(info: path.ParsedPath) { |
| 70 | + const { name } = info; |
| 71 | + return name === "index" || name.startsWith("index."); |
| 72 | +} |
| 73 | + |
| 74 | +function getModuleNamesFromFiles( |
| 75 | + files: string[], |
| 76 | + keepIndexFiles: boolean, |
| 77 | +): (readonly [moduleName: string, file: string])[] { |
| 78 | + files = files.map((f) => path.posix.normalize(f)); |
| 79 | + |
| 80 | + const infoAndFiles = files.map((f) => [path.posix.parse(f), f] as const); |
| 81 | + |
| 82 | + if (keepIndexFiles) { |
| 83 | + return infoAndFiles.map( |
| 84 | + ([info, file]) => [getModuleNameFromFile(info), file] as const, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + const dirStatus = new Map< |
| 89 | + string, |
| 90 | + { hasIndex: boolean; hasOtherFiles: boolean } |
| 91 | + >(); |
| 92 | + |
| 93 | + for (const [info] of infoAndFiles) { |
| 94 | + const { dir } = info; |
| 95 | + let status = dirStatus.get(dir); |
| 96 | + if (!status) { |
| 97 | + status = { hasIndex: false, hasOtherFiles: false }; |
| 98 | + dirStatus.set(dir, status); |
| 99 | + } |
| 100 | + |
| 101 | + if (!status.hasIndex && isSubDirIndexFile(info)) { |
| 102 | + status.hasIndex = true; |
| 103 | + } else { |
| 104 | + status.hasOtherFiles = true; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for (const subDir of dirStatus.keys()) { |
| 109 | + for (const [dir, status] of dirStatus) { |
| 110 | + if (subDir.startsWith(dir + "/")) { |
| 111 | + status.hasOtherFiles = true; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return infoAndFiles.map(([info, file]) => { |
| 117 | + const { dir } = info; |
| 118 | + const status = dirStatus.get(dir); |
| 119 | + if (dir !== "" && status && status.hasIndex && !status.hasOtherFiles) { |
| 120 | + // only process the dirs which only has one index file |
| 121 | + return [dir, file] as const; |
| 122 | + } else { |
| 123 | + return [getModuleNameFromFile(info), file] as const; |
| 124 | + } |
| 125 | + }); |
| 126 | +} |
| 127 | + |
| 128 | +function validateFormatterReturnValue( |
| 129 | + newValue: readonly [string, string], |
| 130 | + warnPromise = false, |
| 131 | +): readonly [string, string] { |
| 132 | + if ( |
| 133 | + newValue && |
| 134 | + typeof newValue[0] === "string" && |
| 135 | + typeof newValue[1] === "string" |
| 136 | + ) { |
| 137 | + return newValue; |
| 138 | + } else { |
| 139 | + throw new Error( |
| 140 | + `getEntryFilesSync formatter is expected to return a [string, string], but returned ${ |
| 141 | + warnPromise && newValue instanceof Promise |
| 142 | + ? "a Promise. Please consider using getEntryFiles" |
| 143 | + : String(newValue) |
| 144 | + }.`, |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +function tupleModuleNameFileToInfo( |
| 150 | + tuple: readonly [string, string], |
| 151 | + index: number, |
| 152 | + baseDir: string, |
| 153 | +): EntryFileInfo { |
| 154 | + return { |
| 155 | + moduleName: tuple[0], |
| 156 | + fileRelativePath: tuple[1], |
| 157 | + index, |
| 158 | + baseDir, |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +export function getEntryFilesSync({ |
| 163 | + formatter, |
| 164 | + patterns = DEFAULT_PATTERNS, |
| 165 | + baseDir = "src", |
| 166 | + ignore = DEFAULT_IGNORE, |
| 167 | + keepIndexFiles = false, |
| 168 | +}: GetEntryFilesOptionsSync = {}): Record<string, string> { |
| 169 | + const files = glob.sync( |
| 170 | + ...getGlobArgs({ |
| 171 | + patterns, |
| 172 | + baseDir, |
| 173 | + ignore, |
| 174 | + }), |
| 175 | + ); |
| 176 | + |
| 177 | + let entries = getModuleNamesFromFiles(files, keepIndexFiles); |
| 178 | + |
| 179 | + if (formatter) { |
| 180 | + entries = entries |
| 181 | + .map((tuple, i) => tupleModuleNameFileToInfo(tuple, i, baseDir)) |
| 182 | + .map((info) => { |
| 183 | + const newValue = formatter(info); |
| 184 | + return validateFormatterReturnValue(newValue); |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + const entryFiles = Object.fromEntries(entries); |
| 189 | + |
| 190 | + return entryFiles; |
| 191 | +} |
| 192 | + |
| 193 | +export async function getEntryFiles({ |
| 194 | + formatter, |
| 195 | + patterns = DEFAULT_PATTERNS, |
| 196 | + baseDir = "src", |
| 197 | + ignore = DEFAULT_IGNORE, |
| 198 | + keepIndexFiles = false, |
| 199 | +}: GetEntryFilesOptions = {}): Promise<Record<string, string>> { |
| 200 | + const files = await glob( |
| 201 | + ...getGlobArgs({ |
| 202 | + patterns, |
| 203 | + baseDir, |
| 204 | + ignore, |
| 205 | + }), |
| 206 | + ); |
| 207 | + |
| 208 | + let entries = getModuleNamesFromFiles(files, keepIndexFiles); |
| 209 | + |
| 210 | + if (formatter) { |
| 211 | + entries = await Promise.all( |
| 212 | + entries |
| 213 | + .map((tuple, i) => tupleModuleNameFileToInfo(tuple, i, baseDir)) |
| 214 | + .map(async (info) => { |
| 215 | + const newValue = await formatter(info); |
| 216 | + return validateFormatterReturnValue(newValue); |
| 217 | + }), |
| 218 | + ); |
| 219 | + } |
| 220 | + |
| 221 | + const entryFiles = Object.fromEntries(entries); |
| 222 | + |
| 223 | + return entryFiles; |
| 224 | +} |
0 commit comments