Refactor the workspace loading into a function.

pull/5370/head
Evan Sangaline 9 months ago committed by Aniket
parent dd06e22b4b
commit eb45057878
  1. 44
      libs/remix-ws-templates/src/script-templates/sindri/utils.ts

@ -22,24 +22,14 @@ const getSindriManifest = async () => {
return JSON.parse(sindriJson) return JSON.parse(sindriJson)
} }
const normalizePath = (path: string): string => {
while (path.startsWith('/') || path.startsWith('./')) {
path = path.replace(/^(\.\/|\/)/, '')
}
return path
}
/** /**
* Compile the circuit. * Create a map of file paths to `File` objects for either all or a subset of files in the workspace.
* *
* @param {string | string[] | null} tags - The tag or tags to use when compiling the circuit. * @param {RegExp | null} pathRegex - A regular expression to limit the included files to those
* @returns {CircuitInfoResponse} compiled circuit * whose paths match. If not specified, then all paths are included.
* @returns {Promise<{[path: string]: File}>} A map of file paths to `File` objects.
*/ */
export const compile = async (tags: string | string[] | null = ['latest']): CircuitInfoResponse => { export const getWorkspaceFilesByPath = async (pathRegex: RegExp | null = null): Promise<{[path: string]: File}> => {
await authorize()
const sindriManifest = await getSindriManifest()
// Create a map from file paths to `File` objects for all files in the workspace.
const filesByPath: {[path: string]: File} = {} const filesByPath: {[path: string]: File} = {}
interface Workspace { interface Workspace {
children?: Workspace children?: Workspace
@ -49,13 +39,35 @@ export const compile = async (tags: string | string[] | null = ['latest']): Circ
const childQueue: Array<[string, Workspace]> = Object.entries(workspace) const childQueue: Array<[string, Workspace]> = Object.entries(workspace)
while (childQueue.length > 0) { while (childQueue.length > 0) {
const [path, child] = childQueue.pop() const [path, child] = childQueue.pop()
if ('content' in child) { if ('content' in child && (pathRegex === null || pathRegex.test(path))) {
filesByPath[path] = new File([child.content], path) filesByPath[path] = new File([child.content], path)
} }
if ('children' in child) { if ('children' in child) {
childQueue.push(...Object.entries(child.children)) childQueue.push(...Object.entries(child.children))
} }
} }
return filesByPath
}
const normalizePath = (path: string): string => {
while (path.startsWith('/') || path.startsWith('./')) {
path = path.replace(/^(\.\/|\/)/, '')
}
return path
}
/**
* Compile the circuit.
*
* @param {string | string[] | null} tags - The tag or tags to use when compiling the circuit.
* @returns {CircuitInfoResponse} compiled circuit
*/
export const compile = async (tags: string | string[] | null = ['latest']): CircuitInfoResponse => {
await authorize()
const sindriManifest = await getSindriManifest()
// Create a map from file paths to `File` objects for all files in the workspace.
const filesByPath = getWorkspaceFilesByPath()
// Merge any of the circuit's resolved dependencies into the files at their expected import paths. // Merge any of the circuit's resolved dependencies into the files at their expected import paths.
if (sindriManifest.circuitType === 'circom') { if (sindriManifest.circuitType === 'circom') {

Loading…
Cancel
Save