11import { test } from "node:test" ;
22import assert from "node:assert/strict" ;
33import { execFileSync } from "node:child_process" ;
4- import { readFileSync } from "node:fs" ;
4+ import { existsSync , mkdirSync , mkdtempSync , readFileSync , rmSync , writeFileSync } from "node:fs" ;
5+ import { tmpdir } from "node:os" ;
56import { join , resolve } from "node:path" ;
67
78/**
@@ -26,23 +27,25 @@ const TEXT_FILE = /\.(mdx?|[jt]sx?|html|json)$/;
2627 * Tracked files only, via git rather than a directory walk: it is one call, and
2728 * it skips `node_modules` and build output for free because they are ignored.
2829 */
29- function trackedTextFiles ( ) : string [ ] {
30+ function trackedTextFiles ( root = ROOT ) : string [ ] {
3031 const listing = execFileSync (
3132 "git" ,
3233 [ "ls-files" , "-z" , "docs" , "scripts" , "packages" , "registry" ] ,
3334 {
34- cwd : ROOT ,
35+ cwd : root ,
3536 encoding : "utf-8" ,
3637 maxBuffer : 64 * 1024 * 1024 ,
3738 } ,
3839 ) ;
39- return listing . split ( "\0" ) . filter ( ( file ) => TEXT_FILE . test ( file ) ) ;
40+ // A tracked file can be gone from disk: catalog regeneration removes a deleted
41+ // item's pages before the publish PR untracks them.
42+ return listing . split ( "\0" ) . filter ( ( file ) => TEXT_FILE . test ( file ) && existsSync ( join ( root , file ) ) ) ;
4043}
4144
42- function pinnedReferences ( ) : { file : string ; version : string } [ ] {
45+ function pinnedReferences ( root = ROOT ) : { file : string ; version : string } [ ] {
4346 const found : { file : string ; version : string } [ ] = [ ] ;
44- for ( const file of trackedTextFiles ( ) ) {
45- const text = readFileSync ( join ( ROOT , file ) , "utf-8" ) ;
47+ for ( const file of trackedTextFiles ( root ) ) {
48+ const text = readFileSync ( join ( root , file ) , "utf-8" ) ;
4649 for ( const match of text . matchAll ( PLAYER_CDN ) ) {
4750 found . push ( { file, version : match [ 1 ] as string } ) ;
4851 }
@@ -68,3 +71,17 @@ test("every player CDN reference asks for latest", () => {
6871 . join ( "\n" ) } `,
6972 ) ;
7073} ) ;
74+
75+ test ( "a tracked file missing from disk is skipped, not read" , ( ) => {
76+ const dir = mkdtempSync ( join ( tmpdir ( ) , "player-cdn-pin-" ) ) ;
77+ try {
78+ execFileSync ( "git" , [ "init" , "-q" ] , { cwd : dir } ) ;
79+ mkdirSync ( join ( dir , "docs" ) ) ;
80+ writeFileSync ( join ( dir , "docs" , "gone.mdx" ) , "removed page" ) ;
81+ execFileSync ( "git" , [ "add" , "docs" ] , { cwd : dir } ) ;
82+ rmSync ( join ( dir , "docs" , "gone.mdx" ) ) ;
83+ assert . deepEqual ( pinnedReferences ( dir ) , [ ] ) ;
84+ } finally {
85+ rmSync ( dir , { recursive : true , force : true } ) ;
86+ }
87+ } ) ;
0 commit comments