From 06d9e085dc18d5533c0bcbe4015acd6c48473c00 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Tue, 19 Dec 2023 12:46:28 +0000 Subject: [PATCH] chore: extract nodejs file manager --- .../noir/file-manager/nodejs-file-manager.ts | 34 +++++++++++++++++++ yarn-project/noir-compiler/src/index.ts | 25 ++------------ 2 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 yarn-project/noir-compiler/src/compile/noir/file-manager/nodejs-file-manager.ts diff --git a/yarn-project/noir-compiler/src/compile/noir/file-manager/nodejs-file-manager.ts b/yarn-project/noir-compiler/src/compile/noir/file-manager/nodejs-file-manager.ts new file mode 100644 index 00000000000..d001e0c072e --- /dev/null +++ b/yarn-project/noir-compiler/src/compile/noir/file-manager/nodejs-file-manager.ts @@ -0,0 +1,34 @@ +import { existsSync } from 'node:fs'; +import * as fs from 'node:fs/promises'; + +import { FileManager } from './file-manager.js'; + +/** + * Creates a new FileManager instance based on nodejs fs + * @param dataDir - where to store files + */ +export function createNodejsFileManager(dataDir: string): FileManager { + return new FileManager( + { + ...fs, + ...{ + // ExistsSync is not available in the fs/promises module + existsSync, + // This is added here because the node types are not compatible with the FileSystem type for mkdir + // Typescripts tries to use a different variant of the function that is not the one that has the optional options. + mkdir: async ( + dir: string, + opts?: { + /** + * Traverse child directories + */ + recursive: boolean; + }, + ) => { + await fs.mkdir(dir, opts); + }, + }, + }, + dataDir, + ); +} diff --git a/yarn-project/noir-compiler/src/index.ts b/yarn-project/noir-compiler/src/index.ts index a417e809501..976c7eaf69d 100644 --- a/yarn-project/noir-compiler/src/index.ts +++ b/yarn-project/noir-compiler/src/index.ts @@ -1,11 +1,9 @@ import { ContractArtifact } from '@aztec/foundation/abi'; -import { existsSync } from 'node:fs'; -import * as fs from 'node:fs/promises'; import { join, resolve } from 'path'; import { CompileOpts, NargoContractCompiler } from './compile/nargo.js'; -import { FileManager } from './compile/noir/file-manager/file-manager.js'; +import { createNodejsFileManager } from './compile/noir/file-manager/nodejs-file-manager.js'; import { NoirWasmCompileOptions, NoirWasmContractCompiler } from './compile/noir/noir-wasm-compiler.js'; import { generateArtifact, generateContractArtifact } from './contract-interface-gen/abi.js'; import { ProgramArtifact } from './noir_artifact.js'; @@ -42,26 +40,7 @@ export async function compileUsingNoirWasm( opts: NoirWasmCompileOptions, ): Promise<(ContractArtifact | ProgramArtifact)[]> { const cacheRoot = process.env.XDG_CACHE_HOME ?? join(process.env.HOME ?? '', '.cache'); - const fileManager = new FileManager( - { - ...fs, - ...{ - existsSync, - mkdir: async ( - dir: string, - opts?: { - /** - * Traverse child directories - */ - recursive: boolean; - }, - ) => { - await fs.mkdir(dir, opts); - }, - }, - }, - join(cacheRoot, 'aztec-noir-compiler'), - ); + const fileManager = createNodejsFileManager(join(cacheRoot, 'aztec-noir-compiler')); const compiler = await NoirWasmContractCompiler.new(fileManager, resolve(projectPath), opts); const artifacts = await compiler.compile(); return artifacts.map(artifact => {