|
112 | 112 |
|
113 | 113 | import { mkdirSync, mkdtempSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs'; |
114 | 114 | import { tmpdir } from 'node:os'; |
115 | | -import { join, relative, resolve } from 'node:path'; |
| 115 | +import { dirname, join, relative, resolve } from 'node:path'; |
116 | 116 | import process from 'node:process'; |
117 | 117 |
|
118 | 118 | import { declaredDeclarationPaths, missingDeclarations } from './check-dts-emitted.mjs'; |
@@ -158,6 +158,51 @@ export function sizeOnDisk(packageDir) { |
158 | 158 | }; |
159 | 159 | } |
160 | 160 |
|
| 161 | +/** |
| 162 | + * The files an `exports` SUBPATH PATTERN target names (objectstack#25803, cutover |
| 163 | + * bill item 22). `./locales/*` -> `./dist/locales/*.d.ts` promises a FAMILY of |
| 164 | + * declarations, one per locale, and the literal path with the `*` in it is no |
| 165 | + * file at all: tested as one, `@object-ui/i18n` read as missing a declaration |
| 166 | + * its build had emitted thirteen times over. |
| 167 | + * |
| 168 | + * Node substitutes the `*` with any non-empty string, `/` included, so the |
| 169 | + * family is every file under the pattern's fixed directory whose path starts |
| 170 | + * with the text before the `*` and ends with the text after it. A pattern with |
| 171 | + * a second `*` is not one Node resolves; it expands to nothing, which reports it. |
| 172 | + * |
| 173 | + * Expanded HERE and not in the shared derivation: `check-dts-emitted.mjs` is a |
| 174 | + * build input of every package (turbo.json `build.inputs`), so changing it |
| 175 | + * re-keys every build cache, and no package that runs it declares a pattern. |
| 176 | + * |
| 177 | + * @param {string} packageDir absolute package directory |
| 178 | + * @param {string} pattern package-relative declared path containing one `*` |
| 179 | + * @returns {string[]} package-relative matches, sorted |
| 180 | + */ |
| 181 | +export function expandDeclarationPattern(packageDir, pattern) { |
| 182 | + const star = pattern.indexOf('*'); |
| 183 | + const prefix = pattern.slice(0, star); |
| 184 | + const suffix = pattern.slice(star + 1); |
| 185 | + if (suffix.includes('*')) return []; |
| 186 | + const matches = []; |
| 187 | + const walk = (rel) => { |
| 188 | + let entries; |
| 189 | + try { |
| 190 | + entries = readdirSync(join(packageDir, rel), { withFileTypes: true }); |
| 191 | + } catch { |
| 192 | + return; |
| 193 | + } |
| 194 | + for (const entry of entries) { |
| 195 | + const child = `${rel}${entry.name}`; |
| 196 | + if (entry.isDirectory()) walk(`${child}/`); |
| 197 | + else if (child.length > prefix.length + suffix.length && child.startsWith(prefix) && child.endsWith(suffix)) { |
| 198 | + matches.push(child); |
| 199 | + } |
| 200 | + } |
| 201 | + }; |
| 202 | + walk(prefix.slice(0, prefix.lastIndexOf('/') + 1)); |
| 203 | + return matches.sort(); |
| 204 | +} |
| 205 | + |
161 | 206 | /** |
162 | 207 | * The sweep itself, over an already-enumerated member list. |
163 | 208 | * |
@@ -186,7 +231,15 @@ export function sweep(root, members) { |
186 | 231 | } |
187 | 232 | checked += 1; |
188 | 233 | files += declared.length; |
189 | | - const missing = missingDeclarations(declared, sizeOnDisk(join(root, dir))); |
| 234 | + // A pattern is owed as a family: missing when nothing matches it, and each |
| 235 | + // match held to the same not-empty rule as a literal path. |
| 236 | + const packageDir = join(root, dir); |
| 237 | + const missing = missingDeclarations(declared.filter((path) => !path.includes('*')), sizeOnDisk(packageDir)); |
| 238 | + for (const pattern of declared.filter((path) => path.includes('*'))) { |
| 239 | + const family = expandDeclarationPattern(packageDir, pattern); |
| 240 | + if (family.length === 0) missing.push({ path: pattern, why: 'missing' }); |
| 241 | + else missing.push(...missingDeclarations(family, sizeOnDisk(packageDir))); |
| 242 | + } |
190 | 243 | if (missing.length > 0) findings.push({ name: typeof manifest.name === 'string' ? manifest.name : dir, dir, missing }); |
191 | 244 | } |
192 | 245 |
|
@@ -305,6 +358,7 @@ export function run(root) { |
305 | 358 | const SELF_TEST_BATTERIES = Object.freeze({ |
306 | 359 | 'the sweep, over real fixture workspaces': 12, |
307 | 360 | 'the shared derivation is the per-package guard\'s own': 2, |
| 361 | + 'an `exports` subpath pattern is owed as a family (#25803)': 6, |
308 | 362 | }); |
309 | 363 |
|
310 | 364 | // DELETING an entry silences that battery's floor exactly as effectively as |
@@ -369,7 +423,10 @@ function selfTest() { |
369 | 423 | writeFileSync(join(abs, 'package.json'), JSON.stringify(spec.manifest, null, 2)); |
370 | 424 | if (spec.dist) { |
371 | 425 | mkdirSync(join(abs, 'dist'), { recursive: true }); |
372 | | - for (const [file, contents] of Object.entries(spec.dist)) writeFileSync(join(abs, 'dist', file), contents); |
| 426 | + for (const [file, contents] of Object.entries(spec.dist)) { |
| 427 | + mkdirSync(dirname(join(abs, 'dist', file)), { recursive: true }); |
| 428 | + writeFileSync(join(abs, 'dist', file), contents); |
| 429 | + } |
373 | 430 | } |
374 | 431 | } |
375 | 432 | return root; |
@@ -493,6 +550,67 @@ function selfTest() { |
493 | 550 | prerequisiteNotMetText('x').includes('NOT a pass and NOT a finding'), |
494 | 551 | ); |
495 | 552 |
|
| 553 | + battery('an `exports` subpath pattern is owed as a family (#25803)'); |
| 554 | + // `@object-ui/i18n`'s shape: a literal entry beside a `*` pattern whose |
| 555 | + // family sits one directory down. |
| 556 | + const patternManifest = (pkgName) => ({ |
| 557 | + name: pkgName, |
| 558 | + exports: { |
| 559 | + '.': { types: './dist/index.d.ts', import: './dist/index.js' }, |
| 560 | + './locales/*': { types: './dist/locales/*.d.ts', import: './dist/locales/*.js' }, |
| 561 | + }, |
| 562 | + }); |
| 563 | + const familyRoot = fixture('family', { |
| 564 | + 'packages/i18n': { |
| 565 | + manifest: patternManifest('@fixture/i18n'), |
| 566 | + dist: { 'index.js': '', 'index.d.ts': 'export {}\n', 'locales/en.js': '', 'locales/en.d.ts': 'export {}\n', 'locales/zh.d.ts': 'export {}\n' }, |
| 567 | + }, |
| 568 | + }); |
| 569 | + eq('a pattern with matching declarations is not a finding', sweep(familyRoot, workspacePackages(familyRoot)).findings, []); |
| 570 | + eq( |
| 571 | + 'the family is every match under the fixed directory, nothing else', |
| 572 | + expandDeclarationPattern(join(familyRoot, 'packages/i18n'), 'dist/locales/*.d.ts'), |
| 573 | + ['dist/locales/en.d.ts', 'dist/locales/zh.d.ts'], |
| 574 | + ); |
| 575 | + const nestedRoot = fixture('family-nested', { |
| 576 | + 'packages/i18n': { |
| 577 | + manifest: patternManifest('@fixture/i18n'), |
| 578 | + dist: { 'index.js': '', 'index.d.ts': 'export {}\n', 'locales/zh/hans.d.ts': 'export {}\n' }, |
| 579 | + }, |
| 580 | + }); |
| 581 | + eq( |
| 582 | + 'the `*` spans `/`, the way Node substitutes it', |
| 583 | + expandDeclarationPattern(join(nestedRoot, 'packages/i18n'), 'dist/locales/*.d.ts'), |
| 584 | + ['dist/locales/zh/hans.d.ts'], |
| 585 | + ); |
| 586 | + const emptyFamilyRoot = fixture('family-empty', { |
| 587 | + 'packages/i18n': { manifest: patternManifest('@fixture/i18n'), dist: { 'index.js': '', 'index.d.ts': 'export {}\n', 'locales/en.js': '' } }, |
| 588 | + }); |
| 589 | + eq( |
| 590 | + 'a pattern nothing matches is a finding that names the pattern', |
| 591 | + sweep(emptyFamilyRoot, workspacePackages(emptyFamilyRoot)).findings.map((f) => f.missing.map((x) => `${x.why}:${x.path}`)), |
| 592 | + [['missing:dist/locales/*.d.ts']], |
| 593 | + ); |
| 594 | + const zeroByteRoot = fixture('family-zero', { |
| 595 | + 'packages/i18n': { |
| 596 | + manifest: patternManifest('@fixture/i18n'), |
| 597 | + dist: { 'index.js': '', 'index.d.ts': 'export {}\n', 'locales/en.d.ts': 'export {}\n', 'locales/zh.d.ts': '' }, |
| 598 | + }, |
| 599 | + }); |
| 600 | + eq( |
| 601 | + 'a zero-byte member of the family reds as `empty`', |
| 602 | + sweep(zeroByteRoot, workspacePackages(zeroByteRoot)).findings.map((f) => f.missing.map((x) => `${x.why}:${x.path}`)), |
| 603 | + [['empty:dist/locales/zh.d.ts']], |
| 604 | + ); |
| 605 | + eq( |
| 606 | + 'the `*` stands for a non-empty string, and a second `*` expands to nothing', |
| 607 | + [ |
| 608 | + expandDeclarationPattern(join(familyRoot, 'packages/i18n'), 'dist/locales/en.d.ts*'), |
| 609 | + expandDeclarationPattern(join(familyRoot, 'packages/i18n'), 'dist/*/*.d.ts'), |
| 610 | + ], |
| 611 | + [[], []], |
| 612 | + ); |
| 613 | + |
496 | 614 | battery('the shared derivation is the per-package guard\'s own'); |
497 | 615 | // Not a re-implementation: the sweep's answer for a manifest IS |
498 | 616 | // `declaredDeclarationPaths`'s answer, and that function is the per-package |
|
0 commit comments