@graphprotocol/graph-cli 0.37.2 → 0.37.3-alpha-20230110163550-d64bef6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/babel.config.js +3 -0
  3. package/dist/bin.js +6 -0
  4. package/dist/cli.js +32 -0
  5. package/dist/codegen/schema.js +250 -0
  6. package/dist/codegen/schema.test.js +458 -0
  7. package/dist/codegen/template.js +65 -0
  8. package/dist/codegen/types/conversions.js +359 -0
  9. package/dist/codegen/types/index.js +71 -0
  10. package/dist/codegen/types/index.test.js +563 -0
  11. package/dist/codegen/typescript.js +200 -0
  12. package/dist/codegen/util.js +46 -0
  13. package/dist/codegen/util.test.js +93 -0
  14. package/dist/command-helpers/abi.js +78 -0
  15. package/dist/command-helpers/auth.js +77 -0
  16. package/dist/command-helpers/compiler.js +66 -0
  17. package/dist/command-helpers/data-sources.js +29 -0
  18. package/dist/command-helpers/fs.js +9 -0
  19. package/dist/command-helpers/gluegun.js +49 -0
  20. package/dist/command-helpers/ipfs.js +5 -0
  21. package/dist/command-helpers/jsonrpc.js +29 -0
  22. package/dist/command-helpers/network.js +93 -0
  23. package/dist/command-helpers/network.test.js +86 -0
  24. package/dist/command-helpers/node.js +40 -0
  25. package/dist/command-helpers/scaffold.js +112 -0
  26. package/dist/command-helpers/spinner.js +84 -0
  27. package/dist/command-helpers/studio.js +13 -0
  28. package/dist/command-helpers/studio.test.js +41 -0
  29. package/dist/command-helpers/subgraph.js +24 -0
  30. package/dist/command-helpers/version.js +74 -0
  31. package/dist/command-helpers/version.test.js +179 -0
  32. package/dist/commands/add.js +190 -0
  33. package/dist/commands/auth.js +127 -0
  34. package/dist/commands/build.js +140 -0
  35. package/dist/commands/codegen.js +135 -0
  36. package/dist/commands/create.js +86 -0
  37. package/dist/commands/deploy.js +334 -0
  38. package/dist/commands/init.js +788 -0
  39. package/dist/commands/local.js +387 -0
  40. package/dist/commands/remove.js +86 -0
  41. package/dist/commands/test.js +295 -0
  42. package/dist/compiler/asc.js +83 -0
  43. package/dist/compiler/index.js +474 -0
  44. package/dist/debug.js +16 -0
  45. package/dist/migrations/mapping_api_version_0_0_1.js +78 -0
  46. package/dist/migrations/mapping_api_version_0_0_2.js +76 -0
  47. package/dist/migrations/mapping_api_version_0_0_3.js +78 -0
  48. package/dist/migrations/mapping_api_version_0_0_4.js +80 -0
  49. package/dist/migrations/mapping_api_version_0_0_5.js +80 -0
  50. package/dist/migrations/spec_version_0_0_2.js +49 -0
  51. package/dist/migrations/spec_version_0_0_3.js +54 -0
  52. package/dist/migrations/util/load-manifest.js +18 -0
  53. package/dist/migrations/util/versions.js +29 -0
  54. package/dist/migrations.js +56 -0
  55. package/dist/protocols/arweave/manifest.graphql +57 -0
  56. package/dist/protocols/arweave/scaffold/manifest.js +18 -0
  57. package/dist/protocols/arweave/scaffold/mapping.js +52 -0
  58. package/dist/protocols/arweave/subgraph.js +23 -0
  59. package/dist/protocols/cosmos/manifest.graphql +76 -0
  60. package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
  61. package/dist/protocols/cosmos/scaffold/mapping.js +38 -0
  62. package/dist/protocols/cosmos/subgraph.js +25 -0
  63. package/dist/protocols/ethereum/abi.js +140 -0
  64. package/dist/protocols/ethereum/codegen/abi.js +451 -0
  65. package/dist/protocols/ethereum/codegen/abi.test.js +328 -0
  66. package/dist/protocols/ethereum/codegen/template.js +50 -0
  67. package/dist/protocols/ethereum/contract.js +20 -0
  68. package/dist/protocols/ethereum/manifest.graphql +94 -0
  69. package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
  70. package/dist/protocols/ethereum/scaffold/mapping.js +65 -0
  71. package/dist/protocols/ethereum/subgraph.js +171 -0
  72. package/dist/protocols/ethereum/type-generator.js +125 -0
  73. package/dist/protocols/index.js +268 -0
  74. package/dist/protocols/ipfs/codegen/file_template.js +50 -0
  75. package/dist/protocols/near/contract.js +41 -0
  76. package/dist/protocols/near/manifest.graphql +64 -0
  77. package/dist/protocols/near/scaffold/manifest.js +16 -0
  78. package/dist/protocols/near/scaffold/mapping.js +38 -0
  79. package/dist/protocols/near/subgraph.js +20 -0
  80. package/dist/protocols/subgraph.js +2 -0
  81. package/dist/protocols/substreams/manifest.graphql +45 -0
  82. package/dist/protocols/substreams/scaffold/manifest.js +12 -0
  83. package/dist/protocols/substreams/subgraph.js +23 -0
  84. package/dist/scaffold/cosmos.test.js +83 -0
  85. package/dist/scaffold/ethereum.test.js +505 -0
  86. package/dist/scaffold/index.js +128 -0
  87. package/dist/scaffold/mapping.js +61 -0
  88. package/dist/scaffold/near.test.js +86 -0
  89. package/dist/scaffold/schema.js +83 -0
  90. package/dist/scaffold/tests.js +175 -0
  91. package/dist/schema.js +54 -0
  92. package/dist/subgraph.js +249 -0
  93. package/dist/type-generator.js +224 -0
  94. package/dist/validation/contract.js +46 -0
  95. package/dist/validation/index.js +8 -0
  96. package/dist/validation/manifest.js +271 -0
  97. package/dist/validation/schema.js +796 -0
  98. package/dist/validation/schema.test.js +35 -0
  99. package/dist/watcher.js +79 -0
  100. package/jest.config.js +1 -1
  101. package/package.json +16 -9
  102. package/{bin/graph → src/bin.ts} +2 -1
  103. package/src/cli.ts +36 -0
  104. package/src/codegen/schema.js +23 -22
  105. package/src/codegen/schema.test.js +33 -27
  106. package/src/codegen/{template.js → template.ts} +8 -6
  107. package/src/codegen/types/conversions.ts +382 -0
  108. package/src/codegen/types/index.js +4 -7
  109. package/src/codegen/types/index.test.js +10 -4
  110. package/src/codegen/{typescript.js → typescript.ts} +44 -26
  111. package/src/codegen/util.js +1 -1
  112. package/src/codegen/util.test.js +5 -2
  113. package/src/command-helpers/abi.js +46 -30
  114. package/src/command-helpers/auth.js +6 -9
  115. package/src/command-helpers/compiler.js +4 -6
  116. package/src/command-helpers/data-sources.js +8 -11
  117. package/src/command-helpers/fs.ts +5 -0
  118. package/src/command-helpers/gluegun.js +2 -4
  119. package/src/command-helpers/ipfs.ts +3 -0
  120. package/src/command-helpers/{jsonrpc.js → jsonrpc.ts} +11 -16
  121. package/src/command-helpers/network.js +58 -44
  122. package/src/command-helpers/network.test.js +23 -19
  123. package/src/command-helpers/node.js +3 -7
  124. package/src/command-helpers/scaffold.js +34 -25
  125. package/src/command-helpers/{spinner.js → spinner.ts} +10 -10
  126. package/src/command-helpers/studio.test.js +61 -30
  127. package/src/command-helpers/studio.ts +20 -0
  128. package/src/command-helpers/{subgraph.js → subgraph.ts} +6 -6
  129. package/src/command-helpers/version.js +11 -16
  130. package/src/command-helpers/version.test.js +111 -82
  131. package/src/commands/add.js +49 -35
  132. package/src/commands/auth.js +13 -25
  133. package/src/commands/build.js +9 -9
  134. package/src/commands/codegen.js +12 -12
  135. package/src/commands/create.js +22 -22
  136. package/src/commands/deploy.js +39 -34
  137. package/src/commands/init.js +21 -24
  138. package/src/commands/local.js +15 -14
  139. package/src/commands/remove.js +22 -22
  140. package/src/commands/test.js +86 -70
  141. package/src/compiler/{asc.js → asc.ts} +29 -16
  142. package/src/compiler/index.js +18 -18
  143. package/src/{debug.js → debug.ts} +2 -2
  144. package/src/migrations/mapping_api_version_0_0_1.js +7 -7
  145. package/src/migrations/mapping_api_version_0_0_2.js +5 -5
  146. package/src/migrations/mapping_api_version_0_0_3.js +7 -7
  147. package/src/migrations/mapping_api_version_0_0_4.js +7 -7
  148. package/src/migrations/mapping_api_version_0_0_5.js +7 -7
  149. package/src/migrations/spec_version_0_0_2.js +5 -5
  150. package/src/migrations/spec_version_0_0_3.js +5 -5
  151. package/src/migrations/util/load-manifest.js +6 -9
  152. package/src/migrations/util/versions.js +5 -10
  153. package/src/{migrations.js → migrations.ts} +14 -15
  154. package/src/protocols/arweave/scaffold/manifest.js +1 -4
  155. package/src/protocols/arweave/scaffold/mapping.js +1 -3
  156. package/src/protocols/arweave/subgraph.ts +22 -0
  157. package/src/protocols/cosmos/scaffold/manifest.js +1 -4
  158. package/src/protocols/cosmos/scaffold/mapping.js +1 -3
  159. package/src/protocols/cosmos/subgraph.js +2 -2
  160. package/src/protocols/ethereum/abi.js +7 -13
  161. package/src/protocols/ethereum/codegen/abi.js +219 -222
  162. package/src/protocols/ethereum/codegen/abi.test.js +6 -6
  163. package/src/protocols/ethereum/codegen/template.js +3 -5
  164. package/src/protocols/ethereum/contract.js +3 -2
  165. package/src/protocols/ethereum/scaffold/manifest.js +4 -7
  166. package/src/protocols/ethereum/scaffold/mapping.js +4 -11
  167. package/src/protocols/ethereum/subgraph.js +21 -20
  168. package/src/protocols/ethereum/type-generator.js +24 -30
  169. package/src/protocols/{index.js → index.ts} +65 -46
  170. package/src/protocols/ipfs/codegen/file_template.js +2 -2
  171. package/src/protocols/near/{contract.js → contract.ts} +16 -12
  172. package/src/protocols/near/scaffold/manifest.js +2 -5
  173. package/src/protocols/near/scaffold/mapping.js +1 -3
  174. package/src/protocols/near/subgraph.js +3 -6
  175. package/src/protocols/subgraph.ts +12 -0
  176. package/src/protocols/substreams/scaffold/{manifest.js → manifest.ts} +2 -7
  177. package/src/protocols/substreams/subgraph.ts +22 -0
  178. package/src/scaffold/cosmos.test.js +3 -3
  179. package/src/scaffold/ethereum.test.js +4 -4
  180. package/src/scaffold/index.js +7 -7
  181. package/src/scaffold/mapping.js +3 -3
  182. package/src/scaffold/near.test.js +3 -3
  183. package/src/scaffold/schema.js +4 -4
  184. package/src/scaffold/tests.js +72 -58
  185. package/src/schema.ts +26 -0
  186. package/src/{subgraph.js → subgraph.ts} +80 -60
  187. package/src/type-generator.js +20 -20
  188. package/src/validation/contract.js +2 -6
  189. package/src/validation/index.js +1 -1
  190. package/src/validation/manifest.js +24 -22
  191. package/src/validation/schema.test.js +1 -1
  192. package/src/validation/{schema.js → schema.ts} +268 -221
  193. package/src/{watcher.js → watcher.ts} +23 -12
  194. package/tests/cli/globalSetup.js +2 -2
  195. package/tests/cli/globalTeardown.js +2 -2
  196. package/tests/cli/init.test.js +2 -2
  197. package/tests/cli/util.js +7 -13
  198. package/tsconfig.json +18 -0
  199. package/src/cli.js +0 -38
  200. package/src/codegen/types/conversions.js +0 -329
  201. package/src/command-helpers/fs.js +0 -8
  202. package/src/command-helpers/ipfs.js +0 -6
  203. package/src/command-helpers/studio.js +0 -15
  204. package/src/protocols/arweave/subgraph.js +0 -20
  205. package/src/protocols/substreams/subgraph.js +0 -17
  206. package/src/schema.js +0 -23
@@ -0,0 +1,387 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const chalk_1 = __importDefault(require("chalk"));
7
+ const docker_compose_1 = __importDefault(require("docker-compose"));
8
+ const http_1 = __importDefault(require("http"));
9
+ const net_1 = __importDefault(require("net"));
10
+ const tmp_promise_1 = __importDefault(require("tmp-promise"));
11
+ const dockerode_1 = __importDefault(require("dockerode"));
12
+ const path_1 = __importDefault(require("path"));
13
+ const strip_ansi_1 = __importDefault(require("strip-ansi"));
14
+ const spawn = require('child_process').spawn;
15
+ const gluegun_1 = require("../command-helpers/gluegun");
16
+ const spinner_1 = require("../command-helpers/spinner");
17
+ // Clean up temporary files even when an uncaught exception occurs
18
+ tmp_promise_1.default.setGracefulCleanup();
19
+ const HELP = `
20
+ ${chalk_1.default.bold('graph local')} [options] ${chalk_1.default.bold('<local-command>')}
21
+
22
+ Options:
23
+
24
+ -h, --help Show usage information
25
+ --node-logs Print the Graph Node logs (optional)
26
+ --ethereum-logs Print the Ethereum logs (optional)
27
+ --compose-file <file> Custom Docker Compose file for additional services (optional)
28
+ --node-image <image> Custom Graph Node image to test against (default: graphprotocol/graph-node:latest)
29
+ --standalone-node <cmd> Use a standalone Graph Node outside Docker Compose (optional)
30
+ --standalone-node-args Custom arguments to be passed to the standalone Graph Node (optional)
31
+ --skip-wait-for-ipfs Don't wait for IPFS to be up at localhost:15001 (optional)
32
+ --skip-wait-for-ethereum Don't wait for Ethereum to be up at localhost:18545 (optional)
33
+ --skip-wait-for-postgres Don't wait for Postgres to be up at localhost:15432 (optional)
34
+ --timeout Time to wait for service containers. (optional, defaults to 120000 milliseconds)
35
+ `;
36
+ exports.default = {
37
+ description: 'Runs local tests against a Graph Node environment (using Ganache by default)',
38
+ run: async (toolbox) => {
39
+ // Obtain tools
40
+ let { filesystem, print } = toolbox;
41
+ // Parse CLI parameters
42
+ let { composeFile, ethereumLogs, h, help, nodeImage, nodeLogs, skipWaitForEthereum, skipWaitForIpfs, skipWaitForPostgres, standaloneNode, standaloneNodeArgs, timeout, } = toolbox.parameters.options;
43
+ // Support both short and long option variants
44
+ help = help || h;
45
+ // Extract test command
46
+ let params = (0, gluegun_1.fixParameters)(toolbox.parameters, {
47
+ ethereumLogs,
48
+ h,
49
+ help,
50
+ nodeLogs,
51
+ skipWaitForEthereum,
52
+ skipWaitForIpfs,
53
+ skipWaitForPostgres,
54
+ });
55
+ // Show help text if requested
56
+ if (help) {
57
+ print.info(HELP);
58
+ return;
59
+ }
60
+ if (params.length == 0) {
61
+ print.error(`Test command not provided as the last argument`);
62
+ process.exitCode = 1;
63
+ return;
64
+ }
65
+ let testCommand = params[0];
66
+ // Create Docker client
67
+ let docker = new dockerode_1.default();
68
+ // Obtain the Docker Compose file for services that the tests run against
69
+ composeFile =
70
+ composeFile ||
71
+ path_1.default.join(__dirname, '..', '..', 'resources', 'test', standaloneNode ? 'docker-compose-standalone-node.yml' : 'docker-compose.yml');
72
+ // parse timeout. Defaults to 120 seconds
73
+ timeout = Math.abs(parseInt(timeout)) || 120000;
74
+ if (!filesystem.exists(composeFile)) {
75
+ print.error(`Docker Compose file \`${composeFile}\` not found`);
76
+ process.exitCode = 1;
77
+ return;
78
+ }
79
+ // Create temporary directory to operate in
80
+ let { path: tempdir } = await tmp_promise_1.default.dir({ prefix: 'graph-test', unsafeCleanup: true });
81
+ try {
82
+ await configureTestEnvironment(toolbox, tempdir, composeFile, nodeImage);
83
+ }
84
+ catch (e) {
85
+ process.exitCode = 1;
86
+ return;
87
+ }
88
+ // Bring up test environment
89
+ try {
90
+ await startTestEnvironment(tempdir);
91
+ }
92
+ catch (e) {
93
+ print.error(e);
94
+ process.exitCode = 1;
95
+ return;
96
+ }
97
+ // Wait for test environment to come up
98
+ try {
99
+ await waitForTestEnvironment({
100
+ skipWaitForEthereum,
101
+ skipWaitForIpfs,
102
+ skipWaitForPostgres,
103
+ timeout,
104
+ });
105
+ }
106
+ catch (e) {
107
+ await stopTestEnvironment(tempdir);
108
+ process.exitCode = 1;
109
+ return;
110
+ }
111
+ // Bring up Graph Node separately, if a standalone node is used
112
+ let nodeProcess;
113
+ let nodeOutputChunks = [];
114
+ if (standaloneNode) {
115
+ try {
116
+ nodeProcess = await startGraphNode(standaloneNode, standaloneNodeArgs, nodeOutputChunks);
117
+ }
118
+ catch (e) {
119
+ toolbox.print.error('');
120
+ toolbox.print.error(' Graph Node');
121
+ toolbox.print.error(' ----------');
122
+ toolbox.print.error(indent(' ', Buffer.concat(nodeOutputChunks).toString('utf-8')));
123
+ toolbox.print.error('');
124
+ await stopTestEnvironment(tempdir);
125
+ process.exitCode = 1;
126
+ return;
127
+ }
128
+ }
129
+ // Wait for Graph Node to come up
130
+ try {
131
+ await waitForGraphNode(timeout);
132
+ }
133
+ catch (e) {
134
+ toolbox.print.error('');
135
+ toolbox.print.error(' Graph Node');
136
+ toolbox.print.error(' ----------');
137
+ toolbox.print.error(indent(' ', await collectGraphNodeLogs(tempdir, standaloneNode, nodeOutputChunks)));
138
+ toolbox.print.error('');
139
+ await stopTestEnvironment(tempdir);
140
+ process.exitCode = 1;
141
+ return;
142
+ }
143
+ // Run tests
144
+ let result = await runTests(testCommand);
145
+ // Bring down Graph Node, if a standalone node is used
146
+ if (nodeProcess) {
147
+ try {
148
+ await stopGraphNode(nodeProcess);
149
+ }
150
+ catch (e) {
151
+ // do nothing (the spinner already logs the problem)
152
+ }
153
+ }
154
+ if (result.exitCode == 0) {
155
+ toolbox.print.success('✔ Tests passed');
156
+ }
157
+ else {
158
+ toolbox.print.error('✖ Tests failed');
159
+ }
160
+ // Capture logs
161
+ nodeLogs =
162
+ nodeLogs || result.exitCode !== 0
163
+ ? await collectGraphNodeLogs(tempdir, standaloneNode, nodeOutputChunks)
164
+ : undefined;
165
+ ethereumLogs = ethereumLogs ? await collectEthereumLogs(tempdir) : undefined;
166
+ // Bring down the test environment
167
+ try {
168
+ await stopTestEnvironment(tempdir);
169
+ }
170
+ catch (e) {
171
+ // do nothing (the spinner already logs the problem)
172
+ }
173
+ if (nodeLogs) {
174
+ toolbox.print.info('');
175
+ toolbox.print.info(' Graph node');
176
+ toolbox.print.info(' ----------');
177
+ toolbox.print.info('');
178
+ toolbox.print.info(indent(' ', nodeLogs));
179
+ }
180
+ if (ethereumLogs) {
181
+ toolbox.print.info('');
182
+ toolbox.print.info(' Ethereum');
183
+ toolbox.print.info(' --------');
184
+ toolbox.print.info('');
185
+ toolbox.print.info(indent(' ', ethereumLogs));
186
+ }
187
+ // Always print the test output
188
+ toolbox.print.info('');
189
+ toolbox.print.info(' Output');
190
+ toolbox.print.info(' ------');
191
+ toolbox.print.info('');
192
+ toolbox.print.info(indent(' ', result.output));
193
+ // Propagate the exit code from the test run
194
+ process.exitCode = result.exitCode;
195
+ },
196
+ };
197
+ /**
198
+ * Indents all lines of a string
199
+ */
200
+ const indent = (indentation, str) => str
201
+ .split('\n')
202
+ .map(s => `${indentation}${s}`)
203
+ // Remove whitespace from empty lines
204
+ .map(s => s.replace(/^\s+$/g, ''))
205
+ .join('\n');
206
+ const configureTestEnvironment = async (toolbox, tempdir, composeFile, nodeImage) => await (0, spinner_1.withSpinner)(`Configure test environment`, `Failed to configure test environment`, `Warnings configuring test environment`, async (spinner) => {
207
+ // Temporary compose file
208
+ let tempComposeFile = path_1.default.join(tempdir, 'compose', 'docker-compose.yml');
209
+ // Copy the compose file to the temporary directory
210
+ await toolbox.filesystem.copy(composeFile, tempComposeFile);
211
+ // Substitute the graph-node image with the custom one, if appropriate
212
+ if (nodeImage) {
213
+ await toolbox.patching.replace(tempComposeFile, 'graphprotocol/graph-node:latest', nodeImage);
214
+ }
215
+ });
216
+ const waitFor = async (timeout, testFn) => {
217
+ let deadline = Date.now() + timeout;
218
+ let error = undefined;
219
+ return new Promise((resolve, reject) => {
220
+ const check = async () => {
221
+ if (Date.now() > deadline) {
222
+ reject(error);
223
+ }
224
+ else {
225
+ try {
226
+ let result = await testFn();
227
+ resolve(result);
228
+ }
229
+ catch (e) {
230
+ error = e;
231
+ setTimeout(check, 500);
232
+ }
233
+ }
234
+ };
235
+ setTimeout(check, 0);
236
+ });
237
+ };
238
+ const startTestEnvironment = async (tempdir) => await (0, spinner_1.withSpinner)(`Start test environment`, `Failed to start test environment`, `Warnings starting test environment`, async (_spinner) => {
239
+ // Bring up the test environment
240
+ await docker_compose_1.default.upAll({
241
+ cwd: path_1.default.join(tempdir, 'compose'),
242
+ });
243
+ });
244
+ const waitForTestEnvironment = async ({ skipWaitForEthereum, skipWaitForIpfs, skipWaitForPostgres, timeout, }) => await (0, spinner_1.withSpinner)(`Wait for test environment`, `Failed to wait for test environment`, `Warnings waiting for test environment`, async (spinner) => {
245
+ // Wait 10s for IPFS (if desired)
246
+ if (skipWaitForIpfs) {
247
+ (0, spinner_1.step)(spinner, 'Skip waiting for IPFS');
248
+ }
249
+ else {
250
+ await waitFor(timeout, async () => new Promise((resolve, reject) => {
251
+ http_1.default
252
+ .get('http://localhost:15001/api/v0/version', response => {
253
+ resolve();
254
+ })
255
+ .on('error', e => {
256
+ reject(new Error(`Could not connect to IPFS: ${e}`));
257
+ });
258
+ }));
259
+ (0, spinner_1.step)(spinner, 'IPFS is up');
260
+ }
261
+ // Wait 10s for Ethereum (if desired)
262
+ if (skipWaitForEthereum) {
263
+ (0, spinner_1.step)(spinner, 'Skip waiting for Ethereum');
264
+ }
265
+ else {
266
+ await waitFor(timeout, async () => new Promise((resolve, reject) => {
267
+ http_1.default
268
+ .get('http://localhost:18545', response => {
269
+ resolve();
270
+ })
271
+ .on('error', e => {
272
+ reject(new Error(`Could not connect to Ethereum: ${e}`));
273
+ });
274
+ }));
275
+ (0, spinner_1.step)(spinner, 'Ethereum is up');
276
+ }
277
+ // Wait 10s for Postgres (if desired)
278
+ if (skipWaitForPostgres) {
279
+ (0, spinner_1.step)(spinner, 'Skip waiting for Postgres');
280
+ }
281
+ else {
282
+ await waitFor(timeout, async () => new Promise((resolve, reject) => {
283
+ try {
284
+ let socket = net_1.default.connect(15432, 'localhost', () => resolve());
285
+ socket.on('error', e => reject(new Error(`Could not connect to Postgres: ${e}`)));
286
+ socket.end();
287
+ }
288
+ catch (e) {
289
+ reject(new Error(`Could not connect to Postgres: ${e}`));
290
+ }
291
+ }));
292
+ (0, spinner_1.step)(spinner, 'Postgres is up');
293
+ }
294
+ });
295
+ const stopTestEnvironment = async (tempdir) => await (0, spinner_1.withSpinner)(`Stop test environment`, `Failed to stop test environment`, `Warnings stopping test environment`, async (spinner) => {
296
+ // Our containers do not respond quickly to the SIGTERM which `down` tries before timing out
297
+ // and killing them, so speed things up by sending a SIGKILL right away.
298
+ try {
299
+ await docker_compose_1.default.kill({ cwd: path_1.default.join(tempdir, 'compose') });
300
+ }
301
+ catch (e) {
302
+ // Do nothing, we will just try to run 'down'
303
+ // to bring down the environment
304
+ }
305
+ await docker_compose_1.default.down({ cwd: path_1.default.join(tempdir, 'compose') });
306
+ });
307
+ const startGraphNode = async (standaloneNode, standaloneNodeArgs, nodeOutputChunks) => await (0, spinner_1.withSpinner)(`Start Graph node`, `Failed to start Graph node`, `Warnings starting Graph node`, async (spinner) => {
308
+ let defaultArgs = [
309
+ '--ipfs',
310
+ 'localhost:15001',
311
+ '--postgres-url',
312
+ 'postgresql://graph:let-me-in@localhost:15432/graph',
313
+ '--ethereum-rpc',
314
+ 'test:http://localhost:18545',
315
+ '--http-port',
316
+ '18000',
317
+ '--ws-port',
318
+ '18001',
319
+ '--admin-port',
320
+ '18020',
321
+ '--index-node-port',
322
+ '18030',
323
+ '--metrics-port',
324
+ '18040',
325
+ ];
326
+ let defaultEnv = {
327
+ GRAPH_LOG: 'debug',
328
+ GRAPH_MAX_API_VERSION: '0.0.5',
329
+ };
330
+ let args = standaloneNodeArgs ? standaloneNodeArgs.split(' ') : defaultArgs;
331
+ let env = { ...defaultEnv, ...process.env };
332
+ let nodeProcess = spawn(standaloneNode, args, {
333
+ cwd: process.cwd(),
334
+ env,
335
+ });
336
+ (0, spinner_1.step)(spinner, 'Graph node:', `${nodeProcess.spawnargs.join(' ')}`);
337
+ nodeProcess.stdout.on('data', data => nodeOutputChunks.push(Buffer.from(data)));
338
+ nodeProcess.stderr.on('data', data => nodeOutputChunks.push(Buffer.from(data)));
339
+ nodeProcess.on('error', e => {
340
+ nodeOutputChunks.push(Buffer.from(`${e}`, 'utf-8'));
341
+ });
342
+ // Return the node child process
343
+ return nodeProcess;
344
+ });
345
+ const waitForGraphNode = async (timeout) => await (0, spinner_1.withSpinner)(`Wait for Graph node`, `Failed to wait for Graph node`, `Warnings waiting for Graph node`, async (spinner) => {
346
+ await waitFor(timeout, async () => new Promise((resolve, reject) => {
347
+ http_1.default
348
+ .get('http://localhost:18000', { timeout }, () => resolve())
349
+ .on('error', e => reject(e));
350
+ }));
351
+ });
352
+ const stopGraphNode = async (nodeProcess) => await (0, spinner_1.withSpinner)(`Stop Graph node`, `Failed to stop Graph node`, `Warnings stopping Graph node`, async (spinner) => {
353
+ nodeProcess.kill(9);
354
+ });
355
+ const collectGraphNodeLogs = async (tempdir, standaloneNode, nodeOutputChunks) => {
356
+ if (standaloneNode) {
357
+ // Pull the logs from the captured output
358
+ return (0, strip_ansi_1.default)(Buffer.concat(nodeOutputChunks).toString('utf-8'));
359
+ }
360
+ else {
361
+ // Pull the logs from docker compose
362
+ let logs = await docker_compose_1.default.logs('graph-node', {
363
+ follow: false,
364
+ cwd: path_1.default.join(tempdir, 'compose'),
365
+ });
366
+ return (0, strip_ansi_1.default)(logs.out.trim()).replace(/graph-node_1 \| /g, '');
367
+ }
368
+ };
369
+ const collectEthereumLogs = async (tempdir) => {
370
+ let logs = await docker_compose_1.default.logs('ethereum', {
371
+ follow: false,
372
+ cwd: path_1.default.join(tempdir, 'compose'),
373
+ });
374
+ return (0, strip_ansi_1.default)(logs.out.trim()).replace(/ethereum_1 \| /g, '');
375
+ };
376
+ const runTests = async (testCommand) => await (0, spinner_1.withSpinner)(`Run tests`, `Failed to run tests`, `Warnings running tests`, async (spinner) => new Promise((resolve, reject) => {
377
+ let output = [];
378
+ let testProcess = spawn(`${testCommand}`, { shell: true });
379
+ testProcess.stdout.on('data', data => output.push(Buffer.from(data)));
380
+ testProcess.stderr.on('data', data => output.push(Buffer.from(data)));
381
+ testProcess.on('close', code => {
382
+ resolve({
383
+ exitCode: code,
384
+ output: Buffer.concat(output).toString('utf-8'),
385
+ });
386
+ });
387
+ }));
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const url_1 = require("url");
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const node_1 = require("../command-helpers/node");
9
+ const auth_1 = require("../command-helpers/auth");
10
+ const jsonrpc_1 = require("../command-helpers/jsonrpc");
11
+ const HELP = `
12
+ ${chalk_1.default.bold('graph remove')} ${chalk_1.default.dim('[options]')} ${chalk_1.default.bold('<subgraph-name>')}
13
+
14
+ ${chalk_1.default.dim('Options:')}
15
+
16
+ --access-token <token> Graph access token
17
+ -h, --help Show usage information
18
+ -g, --node <url> Graph node to create the subgraph in
19
+ `;
20
+ exports.default = {
21
+ description: 'Unregisters a subgraph name',
22
+ run: async (toolbox) => {
23
+ // Obtain tools
24
+ let { filesystem, print, system } = toolbox;
25
+ // Read CLI parameters
26
+ let { accessToken, g, h, help, node } = toolbox.parameters.options;
27
+ let subgraphName = toolbox.parameters.first;
28
+ // Support both long and short option variants
29
+ help = help || h;
30
+ node = node || g;
31
+ // Show help text if requested
32
+ if (help) {
33
+ print.info(HELP);
34
+ return;
35
+ }
36
+ // Validate the subgraph name
37
+ if (!subgraphName) {
38
+ print.error('No subgraph name provided');
39
+ print.info(HELP);
40
+ process.exitCode = 1;
41
+ return;
42
+ }
43
+ // Validate node
44
+ if (!node) {
45
+ print.error(`No Graph node provided`);
46
+ print.info(HELP);
47
+ process.exitCode = 1;
48
+ return;
49
+ }
50
+ try {
51
+ (0, node_1.validateNodeUrl)(node);
52
+ }
53
+ catch (e) {
54
+ print.error(`Graph node "${node}" is invalid: ${e.message}`);
55
+ process.exitCode = 1;
56
+ return;
57
+ }
58
+ let requestUrl = new url_1.URL(node);
59
+ let client = (0, jsonrpc_1.createJsonRpcClient)(requestUrl);
60
+ // Exit with an error code if the client couldn't be created
61
+ if (!client) {
62
+ process.exitCode = 1;
63
+ return;
64
+ }
65
+ // Use the access token, if one is set
66
+ accessToken = await (0, auth_1.identifyDeployKey)(node, accessToken);
67
+ if (accessToken !== undefined && accessToken !== null) {
68
+ client.options.headers = { Authorization: `Bearer ${accessToken}` };
69
+ }
70
+ let spinner = print.spin(`Creating subgraph in Graph node: ${requestUrl}`);
71
+ client.request('subgraph_remove', { name: subgraphName }, function (requestError, jsonRpcError, res) {
72
+ if (jsonRpcError) {
73
+ spinner.fail(`Error removing the subgraph: ${jsonRpcError.message}`);
74
+ process.exitCode = 1;
75
+ }
76
+ else if (requestError) {
77
+ spinner.fail(`HTTP error removing the subgraph: ${requestError.code}`);
78
+ process.exitCode = 1;
79
+ }
80
+ else {
81
+ spinner.stop();
82
+ print.success(`Removed subgraph: ${subgraphName}`);
83
+ }
84
+ });
85
+ },
86
+ };