@ -1,14 +1,19 @@
// @ts-check
import process from 'node:process' ;
import fs from 'node:fs' ;
import * as acorn from 'acorn' ;
import { walk } from 'zimmerframe' ;
import * as esrap from 'esrap' ;
const DIR = '../../documentation/docs/98-reference/.generated' ;
const watch = process . argv . includes ( '-w' ) ;
function run ( ) {
/** @type {Record<string, Record<string, { messages: string[], details: string | null }>>} */
const messages = { } ;
const seen = new Set ( ) ;
const DIR = '../../documentation/docs/98-reference/.generated' ;
fs . rmSync ( DIR , { force : true , recursive : true } ) ;
fs . mkdirSync ( DIR ) ;
@ -54,6 +59,7 @@ for (const category of fs.readdirSync('messages')) {
}
sorted . sort ( ( a , b ) => ( a . code < b . code ? - 1 : 1 ) ) ;
fs . writeFileSync (
` messages/ ${ category } / ${ file } ` ,
sorted . map ( ( x ) => x . _ . trim ( ) ) . join ( '\n\n' ) + '\n'
@ -65,7 +71,10 @@ for (const category of fs.readdirSync('messages')) {
'<!-- This file is generated by scripts/process-messages/index.js. Do not edit! -->\n\n' +
Object . entries ( messages [ category ] )
. map ( ( [ code , { messages , details } ] ) => {
const chunks = [ ` ### ${ code } ` , ... messages . map ( ( message ) => '```\n' + message + '\n```' ) ] ;
const chunks = [
` ### ${ code } ` ,
... messages . map ( ( message ) => '```\n' + message + '\n```' )
] ;
if ( details ) {
chunks . push ( details ) ;
@ -407,3 +416,26 @@ transform('client-errors', 'src/internal/client/errors.js');
transform ( 'server-errors' , 'src/internal/server/errors.js' ) ;
transform ( 'shared-errors' , 'src/internal/shared/errors.js' ) ;
transform ( 'shared-warnings' , 'src/internal/shared/warnings.js' ) ;
}
if ( watch ) {
let running = false ;
let timeout ;
fs . watch ( 'messages' , { recursive : true } , ( type , file ) => {
if ( running ) {
timeout ? ? = setTimeout ( ( ) => {
running = false ;
timeout = null ;
} ) ;
} else {
running = true ;
// eslint-disable-next-line no-console
console . log ( 'Regenerating messages...' ) ;
run ( ) ;
}
} ) ;
}
run ( ) ;