Resolve noir dependencies

pull/5770/head
ioedeveloper 1 month ago committed by Aniket
parent 7e73e07a57
commit 2b190215c3
  1. 55
      apps/noir-compiler/src/app/services/noirPluginClient.ts

@ -4,6 +4,7 @@ import EventManager from 'events'
// @ts-ignore // @ts-ignore
import { compile_program, createFileManager } from '@noir-lang/noir_wasm/default' import { compile_program, createFileManager } from '@noir-lang/noir_wasm/default'
import type { FileManager } from '@noir-lang/noir_wasm/dist/node/main' import type { FileManager } from '@noir-lang/noir_wasm/dist/node/main'
import pathModule from 'path'
import { DEFAULT_TOML_CONFIG } from '../actions/constants' import { DEFAULT_TOML_CONFIG } from '../actions/constants'
export class NoirPluginClient extends PluginClient { export class NoirPluginClient extends PluginClient {
public internalEvents: EventManager public internalEvents: EventManager
@ -63,14 +64,54 @@ export class NoirPluginClient extends PluginClient {
} }
async parse(path: string, content?: string): Promise<void> { async parse(path: string, content?: string): Promise<void> {
if (!content) { if (!content) content = await this.call('fileManager', 'readFile', path)
// @ts-ignore await this.resolveDependencies(path, content)
content = await this.call('fileManager', 'readFile', path)
}
const depImports = Array.from(content.matchAll(/mod\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*(=\s*["'](.*?)["'])?\s*;/g), match => match[3] || match[1]);
console.log('depImports: ', depImports)
const fileBytes = new TextEncoder().encode(content) const fileBytes = new TextEncoder().encode(content)
this.fm.writeFile(`src/${path}`, new Blob([fileBytes]).stream()) this.fm.writeFile(`${path}`, new Blob([fileBytes]).stream())
}
async resolveDependencies (filePath: string, fileContent: string, blackPath: string[] = []): Promise<void> {
const imports = Array.from(fileContent.matchAll(/mod\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*(=\s*["'](.*?)["'])?\s*;/g), match => match[3] || match[1]);
console.log('depImports: ', imports)
for (let dep of imports) {
if (!dep.endsWith('.nr')) dep += '.nr'
if (blackPath.includes(dep)) return
let dependencyContent = ''
let path = dep.replace(/(\.\.\/)+/g, '')
// @ts-ignore
const pathExists = await this.call('fileManager', 'exists', path)
if (pathExists) {
dependencyContent = await this.call('fileManager', 'readFile', path)
} else {
let relativePath = pathModule.resolve(filePath.slice(0, filePath.lastIndexOf('/')), dep)
if (relativePath.indexOf('/') === 0) relativePath = relativePath.slice(1)
// @ts-ignore
const relativePathExists = await this.call('fileManager', 'exists', relativePath)
if (relativePathExists) {
path = relativePath
dependencyContent = await this.call('fileManager', 'readFile', relativePath)
} else {
console.error(`Dependency ${dep} not found in Remix file system`)
}
}
blackPath.push(dep)
// extract all includes from the dependency content
const depImports = Array.from(fileContent.matchAll(/mod\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*(=\s*["'](.*?)["'])?\s*;/g), match => match[3] || match[1])
if (depImports.length > 0 && dependencyContent.length > 0) {
await this.resolveDependencies(path, dependencyContent, blackPath)
const fileBytes = new TextEncoder().encode(dependencyContent)
console.log('writing file: ', `src/${dep}`)
this.fm.writeFile(`src/${dep}`, new Blob([fileBytes]).stream())
}
}
} }
} }

Loading…
Cancel
Save