From a1cdaf8c37ac5dad171490dfd3607a20fa6fa55f Mon Sep 17 00:00:00 2001 From: Evan Sangaline Date: Wed, 14 Feb 2024 15:20:52 -0600 Subject: [PATCH] Refactor the workspace loading into a function. --- .../src/script-templates/sindri/utils.ts | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/libs/remix-ws-templates/src/script-templates/sindri/utils.ts b/libs/remix-ws-templates/src/script-templates/sindri/utils.ts index 3f8245cf68..21016b6abe 100644 --- a/libs/remix-ws-templates/src/script-templates/sindri/utils.ts +++ b/libs/remix-ws-templates/src/script-templates/sindri/utils.ts @@ -22,24 +22,14 @@ const getSindriManifest = async () => { 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. - * @returns {CircuitInfoResponse} compiled circuit + * @param {RegExp | null} pathRegex - A regular expression to limit the included files to those + * 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 => { - await authorize() - const sindriManifest = await getSindriManifest() - - // Create a map from file paths to `File` objects for all files in the workspace. +export const getWorkspaceFilesByPath = async (pathRegex: RegExp | null = null): Promise<{[path: string]: File}> => { const filesByPath: {[path: string]: File} = {} interface 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) while (childQueue.length > 0) { 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) } if ('children' in child) { 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. if (sindriManifest.circuitType === 'circom') {