@@ -12,7 +12,10 @@ interface Target {
1212 condition ?: string ;
1313}
1414
15- const selfLocation = join ( __dirname , "../../" ) ;
15+ const selfLocation = join (
16+ __dirname ,
17+ __dirname . endsWith ( "build" ) ? "../../" : "./"
18+ ) ;
1619
1720async function materializeFunctionInFile ( source : SourceFile , target : Target ) {
1821 const localPath = relative ( cwd ( ) , source . getFilePath ( ) ) ;
@@ -47,27 +50,30 @@ async function materializeFunctionInFile(source: SourceFile, target: Target) {
4750 // prettier-ignore
4851 source . addStatements ( `import { materialize } from "${ selfLocation } "` ) ;
4952 // prettier-ignore
50- source . addStatements ( `materialize(${ func . getName ( ) } , ${ target . condition ?? "undefined" } ).then(console.log).catch((e:any) => console.error(e.message))` ) ;
53+ source . addStatements ( `materialize/*remove*/ (${ func . getName ( ) } , ${ target . condition ?? "undefined" } ).then(console.log).catch((e:any) => console.error(e.message))` ) ;
5154 await source . save ( ) ;
55+ await source . refreshFromFileSystem ( ) ;
5256
5357 let error : string | undefined = undefined ;
5458 try {
5559 const result = await execAsync ( `ts-node ${ source . getFilePath ( ) } ` ) ;
56-
57- await source . refreshFromFileSystem ( ) ;
58-
5960 try {
6061 const data = extractResponse ( result . stdout ) ;
6162 func . setBodyText ( `/* __materialized__ */\nreturn ${ data } ` ) ;
6263 } catch ( e : any ) {
6364 error = result . stderr . trim ( ) ;
6465 }
6566 } catch ( e : any ) {
66- await source . refreshFromFileSystem ( ) ;
6767 error = e . message . trim ( ) ;
6868 }
6969
70- source . removeStatement ( source . getStatements ( ) . length - 1 ) ;
70+ const st = source . getStatements ( ) ;
71+ st . forEach ( ( s ) => {
72+ if ( s . getText ( ) . startsWith ( "materialize/*remove*/(" ) ) {
73+ s . remove ( ) ;
74+ }
75+ } ) ;
76+
7177 source . organizeImports ( ) ;
7278 await source . save ( ) ;
7379 log ( `${ error ?? "completed" } ` ) ;
0 commit comments