@@ -27,9 +27,12 @@ export async function compileSchema(options: CompileOptions): Promise<void> {
2727 name . replace ( / _ ( [ a - z A - Z 0 - 9 ] ) / g, ( _ , c : string ) => c . toUpperCase ( ) ) ;
2828
2929 // Replace struct field declarations like ` created_at: ...` -> ` createdAt: ...`
30- schema = schema . replace ( / ^ ( \s * ) ( [ a - z ] [ a - z 0 - 9 _ ] * ) ( \s * : \s * ) / gim, ( _m , p1 : string , p2 : string , p3 : string ) => {
31- return `${ p1 } ${ toCamel ( p2 ) } ${ p3 } ` ;
32- } ) ;
30+ schema = schema . replace (
31+ / ^ ( \s * ) ( [ a - z ] [ a - z 0 - 9 _ ] * ) ( \s * : \s * ) / gim,
32+ ( _m , p1 : string , p2 : string , p3 : string ) => {
33+ return `${ p1 } ${ toCamel ( p2 ) } ${ p3 } ` ;
34+ } ,
35+ ) ;
3336 const outputDir = path . dirname ( outputPath ) ;
3437
3538 await fs . mkdir ( outputDir , { recursive : true } ) ;
@@ -40,9 +43,31 @@ export async function compileSchema(options: CompileOptions): Promise<void> {
4043 ...config ,
4144 } ;
4245
43- const result = transform ( schema , defaultConfig ) ;
46+ let result = transform ( schema , defaultConfig ) ;
47+
48+ result = postProcessAssert ( result ) ;
4449
4550 await fs . writeFile ( outputPath , result ) ;
4651}
4752
53+ const ASSERT_FUNCTION = `
54+ function assert(condition: boolean, message?: string): asserts condition {
55+ if (!condition) throw new Error(message ?? "Assertion failed")
56+ }
57+
58+ ` ;
59+
60+ /**
61+ * Remove Node.js assert import and inject a custom assert function
62+ */
63+ function postProcessAssert ( code : string ) : string {
64+ // Remove Node.js assert import
65+ code = code . replace ( / ^ i m p o r t a s s e r t f r o m " n o d e : a s s e r t \/ s t r i c t " / m, "" ) ;
66+
67+ // INject new assert function
68+ code += "\n" + ASSERT_FUNCTION ;
69+
70+ return code ;
71+ }
72+
4873export { type Config , transform } from "@bare-ts/tools" ;
0 commit comments