MCPcopy Create free account
hub / github.com/callstackincubator/polygen / CodegenContext

Class CodegenContext

packages/codegen/src/codegen/context.ts:14–113  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12 * Contains methods to progressively build the codegen state.
13 */
14export class CodegenContext {
15 /**
16 * List of generated modules
17 */
18 public modules: W2CGeneratedModule[] = [];
19
20 /**
21 * List of imported modules
22 *
23 * Generated by processing imports of generated modules
24 */
25 public importedModules: Map<string, W2CExternModule> = new Map();
26
27 /**
28 * Adds a module to the context.
29 *
30 * This method should be used to add new modules to the context.
31 * Before creating a new W2CModule, module body is scanned for imports and
32 * all imported modules are added to the context into `importedModules`.
33 *
34 * This way, when creating the module, all imported modules can be resolved.
35 */
36 public async addModule(
37 module: ResolvedModule
38 ): Promise<[W2CGeneratedModule, W2CExternModule[]]> {
39 const moduleContents = await fs.readFile(module.resolvedPath, {
40 encoding: null,
41 });
42 const checksum = computeChecksumBuffer(moduleContents.buffer);
43 const moduleBody = new Module(moduleContents.buffer as ArrayBuffer);
44 const importedModules = this.processImportedModules(moduleBody);
45
46 const generatedModule = new W2CGeneratedModule(
47 this,
48 moduleBody,
49 checksum,
50 module.resolvedPath,
51 module
52 );
53 this.modules.push(generatedModule);
54
55 return [generatedModule, importedModules];
56 }
57
58 /**
59 * Gets an imported module by name.
60 *
61 * If the module is not found, an error is thrown.
62 *
63 * @param name Name of the imported module
64 */
65 public getImportedModule(name: string): W2CExternModule {
66 const externModule = this.importedModules.get(name);
67 if (!externModule) {
68 throw new Error(`Imported Module ${name} not found`);
69 }
70 return externModule;
71 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected