MCPcopy
hub / github.com/subquery/subql / codegen

Function codegen

packages/cli/src/controller/codegen-controller.ts:189–282  ·  view source on GitHub ↗
(projectPath: string, fileNames: string[] = [DEFAULT_MANIFEST])

Source from the content-addressed store, hash-verified

187
188//1. Prepare models directory and load schema
189export async function codegen(projectPath: string, fileNames: string[] = [DEFAULT_MANIFEST]): Promise<void> {
190 const modelDir = path.join(projectPath, MODEL_ROOT_DIR);
191 const interfacesPath = path.join(projectPath, TYPE_ROOT_DIR, `interfaces.ts`);
192 await prepareDirPath(modelDir, true);
193 await prepareDirPath(interfacesPath, false);
194 const plainManifests = fileNames.map((fileName) => {
195 const project = loadFromJsonOrYaml(getManifestPath(projectPath, fileName)) as ProjectManifestV1_0_0 & {
196 networkFamily: NETWORK_FAMILY;
197 };
198 project.networkFamily = getProjectNetwork(project);
199 return project;
200 });
201
202 const expectKeys = ['datasources', 'templates'];
203
204 const customDatasources = plainManifests.flatMap((plainManifest) => {
205 return Object.keys(plainManifest)
206 .filter((key) => !expectKeys.includes(key))
207 .map((dsKey) => {
208 const value = (plainManifest as any)[dsKey];
209 if (typeof value === 'object' && value) {
210 return !!Object.keys(value).find((d) => d === 'assets') && value;
211 }
212 })
213 .filter(Boolean);
214 });
215
216 const schema = getSchemaPath(projectPath, fileNames[0]);
217 await generateSchemaModels(projectPath, schema);
218
219 let datasources = plainManifests.reduce((prev, current) => {
220 return prev.concat(current.dataSources);
221 }, [] as BaseDataSource[]);
222
223 const templates = plainManifests.reduce((prev, current) => {
224 if (current.templates && current.templates.length !== 0) {
225 return prev.concat(current.templates);
226 }
227 return prev;
228 }, [] as TemplateKind[]);
229
230 if (templates.length !== 0) {
231 await generateDatasourceTemplates(projectPath, templates);
232 datasources = datasources.concat(templates as DatasourceKind[]);
233 }
234
235 if (customDatasources.length !== 0) {
236 datasources = datasources.concat(customDatasources);
237 }
238
239 const cosmosManifests = plainManifests.filter((m) => m.networkFamily === NETWORK_FAMILY.cosmos);
240 if (cosmosManifests.length > 0) {
241 const cosmosModule = loadDependency(NETWORK_FAMILY.cosmos, projectPath);
242 await cosmosModule.projectCodegen(
243 plainManifests,
244 projectPath,
245 prepareDirPath,
246 renderTemplate,

Callers 3

codegenAdapterFunction · 0.90

Calls 12

prepareDirPathFunction · 0.90
loadFromJsonOrYamlFunction · 0.90
getManifestPathFunction · 0.90
getProjectNetworkFunction · 0.90
getSchemaPathFunction · 0.90
loadDependencyFunction · 0.90
renderTemplateFunction · 0.90
generateSchemaModelsFunction · 0.85
mapMethod · 0.80
findMethod · 0.65
logMethod · 0.45

Tested by

no test coverage detected