@@ -4,7 +4,7 @@ import to from 'await-to-js'
4
4
import FormData from 'form-data'
5
5
import * as zkgapi from '@hyperoracle/cle-api-test'
6
6
import webjson from '@hyperoracle/cle-lib-test/test/weblib/weblib.json'
7
- import { createOnNonexist , fromHexString , loadYamlFromPath } from '../utils'
7
+ import { createOnNonexist , fromHexString , isTsFile , loadYamlFromPath } from '../utils'
8
8
import { logger } from '../logger'
9
9
10
10
export interface CompileOptions {
@@ -48,12 +48,15 @@ async function compileBasic(options: CompileOptions) {
48
48
return false
49
49
}
50
50
51
+ const paths = getFileTreeByDir ( path . dirname ( mappingPath ) )
52
+ const relativePaths = getRelativePaths ( path . dirname ( mappingPath ) , paths )
53
+ const fileMap = getFileContentsByFilePaths ( relativePaths , path . dirname ( mappingPath ) )
51
54
const res = await zkgapi . compile ( { cleYaml : yaml } , {
52
55
...webjson ,
53
- 'mapping.ts' : getMappingContent ( mappingPath ) ,
56
+ ... fileMap ,
54
57
} , { isLocal : local } )
55
58
56
- if ( res . error || res . stderr ) {
59
+ if ( res . error ) {
57
60
logger . error ( `[-] COMPILATION ERROR. ${ res . error ?. message } ` )
58
61
logger . error ( `[-] ${ res . stderr . toString ( ) } ` )
59
62
return false
@@ -132,6 +135,36 @@ function logCompileResult(wasmPath: string, watPath: string): void {
132
135
logger . info ( '[+] COMPILATION SUCCESS!' + '\n' )
133
136
}
134
137
135
- function getMappingContent ( filepath : string ) {
136
- return fs . readFileSync ( filepath , 'utf-8' )
138
+ function getFileTreeByDir ( dir : string ) {
139
+ const fileTree : string [ ] = [ ]
140
+ const files = fs . readdirSync ( dir )
141
+ for ( const file of files ) {
142
+ const filePath = path . join ( dir , file )
143
+ if ( fs . statSync ( filePath ) . isDirectory ( ) ) {
144
+ const subFiles = getFileTreeByDir ( filePath )
145
+ fileTree . push ( ...subFiles )
146
+ }
147
+ else if ( isTsFile ( file ) ) {
148
+ fileTree . push ( filePath )
149
+ }
150
+ }
151
+ return fileTree
152
+ }
153
+
154
+ function getRelativePaths ( dir : string , filePaths : string [ ] ) : string [ ] {
155
+ const relativePaths : string [ ] = [ ]
156
+ for ( const filePath of filePaths ) {
157
+ const relativePath = path . relative ( dir , filePath )
158
+ relativePaths . push ( relativePath )
159
+ }
160
+ return relativePaths
161
+ }
162
+
163
+ function getFileContentsByFilePaths ( filePaths : string [ ] , basePath : string ) {
164
+ const fileContents : Record < string , string > = { }
165
+ for ( const filePath of filePaths ) {
166
+ const fileContent = fs . readFileSync ( path . join ( basePath , filePath ) , 'utf-8' )
167
+ Reflect . set ( fileContents , filePath , fileContent )
168
+ }
169
+ return fileContents
137
170
}
0 commit comments