2026-01-18 18:46:18 +00:00
#!/usr/bin/env node
2025-12-09 17:51:05 +00:00
2026-01-31 21:21:09 +09:00
import { existsSync , readdirSync , readFileSync , statSync } from "node:fs" ;
import { join , relative } from "node:path" ;
2025-12-09 17:51:05 +00:00
2026-01-31 21:21:09 +09:00
process . stdout . on ( "error" , ( error ) => {
if ( error ? . code === "EPIPE" ) {
2025-12-19 18:02:30 +01:00
process . exit ( 0 ) ;
}
throw error ;
} ) ;
2026-01-31 21:21:09 +09:00
const DOCS _DIR = join ( process . cwd ( ) , "docs" ) ;
2026-01-18 22:38:09 +00:00
if ( ! existsSync ( DOCS _DIR ) ) {
2026-01-31 21:21:09 +09:00
console . error ( "docs:list: missing docs directory. Run from repo root." ) ;
2026-01-18 22:38:09 +00:00
process . exit ( 1 ) ;
}
if ( ! statSync ( DOCS _DIR ) . isDirectory ( ) ) {
2026-01-31 21:21:09 +09:00
console . error ( "docs:list: docs path is not a directory." ) ;
2026-01-18 22:38:09 +00:00
process . exit ( 1 ) ;
}
2025-12-09 17:51:05 +00:00
2026-01-31 21:21:09 +09:00
const EXCLUDED _DIRS = new Set ( [ "archive" , "research" ] ) ;
2025-12-09 17:51:05 +00:00
2026-01-18 19:15:59 +00:00
/ * *
* @ param { unknown [ ] } values
* @ returns { string [ ] }
* /
function compactStrings ( values ) {
const result = [ ] ;
2025-12-09 17:51:05 +00:00
for ( const value of values ) {
if ( value === null || value === undefined ) {
continue ;
}
2026-01-31 21:29:14 +09:00
const normalized =
typeof value === "string"
? value . trim ( )
: typeof value === "number" || typeof value === "boolean"
? String ( value ) . trim ( )
: null ;
if ( normalized ? . length > 0 ) {
2025-12-09 17:51:05 +00:00
result . push ( normalized ) ;
}
}
return result ;
}
2026-01-18 19:15:59 +00:00
/ * *
* @ param { string } dir
* @ param { string } base
* @ returns { string [ ] }
* /
function walkMarkdownFiles ( dir , base = dir ) {
2025-12-09 17:51:05 +00:00
const entries = readdirSync ( dir , { withFileTypes : true } ) ;
2026-01-18 19:15:59 +00:00
const files = [ ] ;
2025-12-09 17:51:05 +00:00
for ( const entry of entries ) {
2026-01-31 21:21:09 +09:00
if ( entry . name . startsWith ( "." ) ) {
2025-12-09 17:51:05 +00:00
continue ;
}
const fullPath = join ( dir , entry . name ) ;
if ( entry . isDirectory ( ) ) {
if ( EXCLUDED _DIRS . has ( entry . name ) ) {
continue ;
}
files . push ( ... walkMarkdownFiles ( fullPath , base ) ) ;
2026-01-31 21:21:09 +09:00
} else if ( entry . isFile ( ) && entry . name . endsWith ( ".md" ) ) {
2025-12-09 17:51:05 +00:00
files . push ( relative ( base , fullPath ) ) ;
}
}
2026-01-31 21:29:14 +09:00
return files . toSorted ( ( a , b ) => a . localeCompare ( b ) ) ;
2025-12-09 17:51:05 +00:00
}
2026-01-18 19:15:59 +00:00
/ * *
* @ param { string } fullPath
* @ returns { { summary : string | null ; readWhen : string [ ] ; error ? : string } }
* /
function extractMetadata ( fullPath ) {
2026-01-31 21:21:09 +09:00
const content = readFileSync ( fullPath , "utf8" ) ;
2025-12-09 17:51:05 +00:00
2026-01-31 21:21:09 +09:00
if ( ! content . startsWith ( "---" ) ) {
return { summary : null , readWhen : [ ] , error : "missing front matter" } ;
2025-12-09 17:51:05 +00:00
}
2026-01-31 21:21:09 +09:00
const endIndex = content . indexOf ( "\n---" , 3 ) ;
2025-12-09 17:51:05 +00:00
if ( endIndex === - 1 ) {
2026-01-31 21:21:09 +09:00
return { summary : null , readWhen : [ ] , error : "unterminated front matter" } ;
2025-12-09 17:51:05 +00:00
}
const frontMatter = content . slice ( 3 , endIndex ) . trim ( ) ;
2026-01-31 21:21:09 +09:00
const lines = frontMatter . split ( "\n" ) ;
2025-12-09 17:51:05 +00:00
2026-01-18 19:15:59 +00:00
let summaryLine = null ;
const readWhen = [ ] ;
let collectingField = null ;
2025-12-09 17:51:05 +00:00
for ( const rawLine of lines ) {
const line = rawLine . trim ( ) ;
2026-01-31 21:21:09 +09:00
if ( line . startsWith ( "summary:" ) ) {
2025-12-09 17:51:05 +00:00
summaryLine = line ;
collectingField = null ;
continue ;
}
2026-01-31 21:21:09 +09:00
if ( line . startsWith ( "read_when:" ) ) {
collectingField = "read_when" ;
const inline = line . slice ( "read_when:" . length ) . trim ( ) ;
if ( inline . startsWith ( "[" ) && inline . endsWith ( "]" ) ) {
2025-12-09 17:51:05 +00:00
try {
2026-01-18 19:15:59 +00:00
const parsed = JSON . parse ( inline . replace ( /'/g , '"' ) ) ;
2025-12-09 17:51:05 +00:00
if ( Array . isArray ( parsed ) ) {
readWhen . push ( ... compactStrings ( parsed ) ) ;
}
} catch {
// ignore malformed inline arrays
}
}
continue ;
}
2026-01-31 21:21:09 +09:00
if ( collectingField === "read_when" ) {
if ( line . startsWith ( "- " ) ) {
2025-12-09 17:51:05 +00:00
const hint = line . slice ( 2 ) . trim ( ) ;
if ( hint ) {
readWhen . push ( hint ) ;
}
2026-01-31 21:21:09 +09:00
} else if ( line === "" ) {
2025-12-09 17:51:05 +00:00
// allow blank lines inside the list
} else {
collectingField = null ;
}
}
}
if ( ! summaryLine ) {
2026-01-31 21:21:09 +09:00
return { summary : null , readWhen , error : "summary key missing" } ;
2025-12-09 17:51:05 +00:00
}
2026-01-31 21:21:09 +09:00
const summaryValue = summaryLine . slice ( "summary:" . length ) . trim ( ) ;
2025-12-09 17:51:05 +00:00
const normalized = summaryValue
2026-01-31 21:21:09 +09:00
. replace ( /^['"]|['"]$/g , "" )
. replace ( /\s+/g , " " )
2025-12-09 17:51:05 +00:00
. trim ( ) ;
if ( ! normalized ) {
2026-01-31 21:21:09 +09:00
return { summary : null , readWhen , error : "summary is empty" } ;
2025-12-09 17:51:05 +00:00
}
return { summary : normalized , readWhen } ;
}
2026-01-31 21:21:09 +09:00
console . log ( "Listing all markdown files in docs folder:" ) ;
2025-12-09 17:51:05 +00:00
const markdownFiles = walkMarkdownFiles ( DOCS _DIR ) ;
for ( const relativePath of markdownFiles ) {
const fullPath = join ( DOCS _DIR , relativePath ) ;
const { summary , readWhen , error } = extractMetadata ( fullPath ) ;
if ( summary ) {
console . log ( ` ${ relativePath } - ${ summary } ` ) ;
if ( readWhen . length > 0 ) {
2026-01-31 21:21:09 +09:00
console . log ( ` Read when: ${ readWhen . join ( "; " ) } ` ) ;
2025-12-09 17:51:05 +00:00
}
} else {
2026-01-31 21:21:09 +09:00
const reason = error ? ` - [ ${ error } ] ` : "" ;
2025-12-09 17:51:05 +00:00
console . log ( ` ${ relativePath } ${ reason } ` ) ;
}
}
console . log (
2026-01-31 21:21:09 +09:00
'\nReminder: keep docs up to date as behavior changes. When your task matches any "Read when" hint above (React hooks, cache directives, database work, tests, etc.), read that doc before coding, and suggest new coverage when it is missing.' ,
2025-12-09 17:51:05 +00:00
) ;