| 139 | * @returns {Promise<string[]>} |
| 140 | */ |
| 141 | export async function packageFiles(path: string): Promise<string[]> { |
| 142 | const { files = DEFAULT_GLOBS, main, bin } = require(`${path}/package.json`) |
| 143 | |
| 144 | const allFiles: string[] = files.concat( |
| 145 | FORCED_GLOBS, |
| 146 | main ?? [], |
| 147 | Object.values(bin ?? {}) |
| 148 | ) |
| 149 | const isGlob = (f) => f.includes('*') || f.startsWith('!') |
| 150 | const simpleFiles = allFiles |
| 151 | .filter((f) => !isGlob(f) && existsSync(join(path, f))) |
| 152 | .map((f) => f.replace(/^\.\//, '')) |
| 153 | const globFiles = allFiles.filter(isGlob) |
| 154 | const globbedFiles = await glob( |
| 155 | `+(${globFiles.filter((f) => !f.startsWith('!')).join('|')})`, |
| 156 | { |
| 157 | cwd: path, |
| 158 | ignore: `+(${globFiles |
| 159 | .filter((f) => f.startsWith('!')) |
| 160 | .map((f) => f.slice(1)) |
| 161 | .join('|')})`, |
| 162 | } |
| 163 | ) |
| 164 | const packageFiles = [...globbedFiles, ...simpleFiles].sort() |
| 165 | const set = new Set() |
| 166 | return packageFiles.filter((f) => { |
| 167 | if (set.has(f)) return false |
| 168 | // We add the full path, but check for parent directories too. |
| 169 | // This catches the case where the whole directory is added and then a single file from the directory. |
| 170 | // The sorting before ensures that the directory comes before the files inside of the directory. |
| 171 | while (f.includes('/')) { |
| 172 | f = f.replace(/\/[^/]*$/, '') |
| 173 | if (set.has(f)) return false |
| 174 | } |
| 175 | set.add(f) |
| 176 | return true |
| 177 | }) |
| 178 | } |