-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathcodegen.ts
More file actions
116 lines (104 loc) · 3.51 KB
/
codegen.ts
File metadata and controls
116 lines (104 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import path from 'node:path';
import { Args, Command, Flags } from '@oclif/core';
import * as DataSourcesExtractor from '../command-helpers/data-sources.js';
import { getGraphIpfsUrl } from '../command-helpers/ipfs.js';
import { assertGraphTsVersion, assertManifestApiVersion } from '../command-helpers/version.js';
import debug from '../debug.js';
import Protocol from '../protocols/index.js';
import TypeGenerator from '../type-generator.js';
const codegenDebug = debug('graph-cli:codegen');
export default class CodegenCommand extends Command {
static description = 'Generates AssemblyScript types for a subgraph.';
static args = {
'subgraph-manifest': Args.string({
default: 'subgraph.yaml',
}),
};
static flags = {
help: Flags.help({
char: 'h',
}),
'output-dir': Flags.directory({
summary: 'Output directory for generated types.',
char: 'o',
default: 'generated/',
}),
'skip-migrations': Flags.boolean({
summary: 'Skip subgraph migrations.',
}),
watch: Flags.boolean({
summary: 'Regenerate types when subgraph files change.',
char: 'w',
}),
uncrashable: Flags.boolean({
summary: 'Generate Float Subgraph Uncrashable helper file.',
char: 'u',
}),
ipfs: Flags.string({
summary: 'IPFS node to use for fetching subgraph data.',
char: 'i',
default: getGraphIpfsUrl().ipfsUrl,
}),
'uncrashable-config': Flags.file({
summary: 'Directory for uncrashable config.',
aliases: ['uc'],
// TODO: using a default sets the value and therefore requires --uncrashable
// default: 'uncrashable-config.yaml',
dependsOn: ['uncrashable'],
}),
};
async run() {
const {
args: { 'subgraph-manifest': manifest },
flags: {
'output-dir': outputDir,
'skip-migrations': skipMigrations,
watch,
ipfs,
uncrashable,
'uncrashable-config': uncrashableConfig,
},
} = await this.parse(CodegenCommand);
codegenDebug('Initialized codegen manifest: %o', manifest);
let protocol;
let subgraphSources;
try {
// Checks to make sure codegen doesn't run against
// older subgraphs (both apiVersion and graph-ts version).
//
// We don't want codegen to run without these conditions
// because that would mean the CLI would generate code to
// the wrong AssemblyScript version.
await assertManifestApiVersion(manifest, '0.0.5');
await assertGraphTsVersion(path.dirname(manifest), '0.25.0');
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest);
protocol = Protocol.fromDataSources(dataSourcesAndTemplates);
subgraphSources = dataSourcesAndTemplates
.filter((ds: any) => ds.kind == 'subgraph')
.map((ds: any) => ds.source.address);
} catch (e) {
this.error(e, { exit: 1 });
}
const { ipfsUrl, warning } = getGraphIpfsUrl(ipfs);
if (warning) {
this.warn(warning);
}
const generator = new TypeGenerator({
subgraphManifest: manifest,
outputDir,
skipMigrations,
protocol,
uncrashable,
subgraphSources,
uncrashableConfig: uncrashableConfig || 'uncrashable-config.yaml',
ipfsUrl,
});
// Watch working directory for file updates or additions, trigger
// type generation (if watch argument specified)
if (watch) {
await generator.watchAndGenerateTypes();
} else if (!(await generator.generateTypes())) {
process.exitCode = 1;
}
}
}