Skip to content

Commit 72bb489

Browse files
committed
feat(compile): support multiple files
1 parent 8c106e1 commit 72bb489

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

packages/cle-cli/src/commands/compile.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import to from 'await-to-js'
44
import FormData from 'form-data'
55
import * as zkgapi from '@hyperoracle/cle-api-test'
66
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'
88
import { logger } from '../logger'
99

1010
export interface CompileOptions {
@@ -48,12 +48,15 @@ async function compileBasic(options: CompileOptions) {
4848
return false
4949
}
5050

51+
const paths = getFileTreeByDir(path.dirname(mappingPath))
52+
const relativePaths = getRelativePaths(path.dirname(mappingPath), paths)
53+
const fileMap = getFileContentsByFilePaths(relativePaths, path.dirname(mappingPath))
5154
const res = await zkgapi.compile({ cleYaml: yaml }, {
5255
...webjson,
53-
'mapping.ts': getMappingContent(mappingPath),
56+
...fileMap,
5457
}, { isLocal: local })
5558

56-
if (res.error || res.stderr) {
59+
if (res.error) {
5760
logger.error(`[-] COMPILATION ERROR. ${res.error?.message}`)
5861
logger.error(`[-] ${res.stderr.toString()}`)
5962
return false
@@ -132,6 +135,36 @@ function logCompileResult(wasmPath: string, watPath: string): void {
132135
logger.info('[+] COMPILATION SUCCESS!' + '\n')
133136
}
134137

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
137170
}

0 commit comments

Comments
 (0)