@graphprotocol/graph-cli 0.37.4 → 0.37.5

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 (100) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/bin.js +6 -0
  3. package/dist/cli.js +26 -0
  4. package/dist/codegen/schema.js +244 -0
  5. package/dist/codegen/schema.test.js +455 -0
  6. package/dist/codegen/template.js +66 -0
  7. package/dist/codegen/types/conversions.js +335 -0
  8. package/dist/codegen/types/index.js +71 -0
  9. package/dist/codegen/types/index.test.js +563 -0
  10. package/dist/codegen/typescript.js +200 -0
  11. package/dist/codegen/util.js +40 -0
  12. package/dist/codegen/util.test.js +93 -0
  13. package/dist/command-helpers/abi.js +76 -0
  14. package/dist/command-helpers/auth.js +76 -0
  15. package/dist/command-helpers/compiler.js +66 -0
  16. package/dist/command-helpers/data-sources.js +29 -0
  17. package/dist/command-helpers/fs.js +9 -0
  18. package/dist/command-helpers/gluegun.js +48 -0
  19. package/dist/command-helpers/ipfs.js +5 -0
  20. package/dist/command-helpers/jsonrpc.js +27 -0
  21. package/dist/command-helpers/network.js +90 -0
  22. package/dist/command-helpers/network.test.js +86 -0
  23. package/dist/command-helpers/node.js +40 -0
  24. package/dist/command-helpers/scaffold.js +110 -0
  25. package/dist/command-helpers/spinner.js +80 -0
  26. package/dist/command-helpers/studio.js +14 -0
  27. package/dist/command-helpers/studio.test.js +41 -0
  28. package/dist/command-helpers/subgraph.js +22 -0
  29. package/dist/command-helpers/version.js +66 -0
  30. package/dist/command-helpers/version.test.js +186 -0
  31. package/dist/commands/add.js +187 -0
  32. package/dist/commands/auth.js +122 -0
  33. package/dist/commands/build.js +136 -0
  34. package/dist/commands/codegen.js +128 -0
  35. package/dist/commands/create.js +94 -0
  36. package/dist/commands/deploy.js +335 -0
  37. package/dist/commands/init.js +800 -0
  38. package/dist/commands/local.js +380 -0
  39. package/dist/commands/remove.js +95 -0
  40. package/dist/commands/test.js +293 -0
  41. package/dist/compiler/asc.js +83 -0
  42. package/dist/compiler/index.js +466 -0
  43. package/dist/debug.js +16 -0
  44. package/dist/migrations/mapping_api_version_0_0_1.js +83 -0
  45. package/dist/migrations/mapping_api_version_0_0_2.js +83 -0
  46. package/dist/migrations/mapping_api_version_0_0_3.js +83 -0
  47. package/dist/migrations/mapping_api_version_0_0_4.js +85 -0
  48. package/dist/migrations/mapping_api_version_0_0_5.js +85 -0
  49. package/dist/migrations/spec_version_0_0_2.js +45 -0
  50. package/dist/migrations/spec_version_0_0_3.js +50 -0
  51. package/dist/migrations/util/load-manifest.js +16 -0
  52. package/dist/migrations/util/versions.js +28 -0
  53. package/dist/migrations.js +54 -0
  54. package/dist/protocols/arweave/manifest.graphql +57 -0
  55. package/dist/protocols/arweave/scaffold/manifest.js +18 -0
  56. package/dist/protocols/arweave/scaffold/mapping.js +52 -0
  57. package/dist/protocols/arweave/subgraph.js +23 -0
  58. package/dist/protocols/contract.js +2 -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 +28 -0
  63. package/dist/protocols/ethereum/abi.js +138 -0
  64. package/dist/protocols/ethereum/codegen/abi.js +474 -0
  65. package/dist/protocols/ethereum/codegen/abi.test.js +325 -0
  66. package/dist/protocols/ethereum/codegen/template.js +52 -0
  67. package/dist/protocols/ethereum/contract.js +22 -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 +172 -0
  72. package/dist/protocols/ethereum/type-generator.js +123 -0
  73. package/dist/protocols/index.js +266 -0
  74. package/dist/protocols/ipfs/codegen/file_template.js +53 -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 +23 -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 +82 -0
  85. package/dist/scaffold/ethereum.test.js +496 -0
  86. package/dist/scaffold/index.js +133 -0
  87. package/dist/scaffold/mapping.js +57 -0
  88. package/dist/scaffold/near.test.js +85 -0
  89. package/dist/scaffold/schema.js +86 -0
  90. package/dist/scaffold/tests.js +179 -0
  91. package/dist/schema.js +54 -0
  92. package/dist/subgraph.js +243 -0
  93. package/dist/type-generator.js +225 -0
  94. package/dist/validation/contract.js +44 -0
  95. package/dist/validation/index.js +10 -0
  96. package/dist/validation/manifest.js +266 -0
  97. package/dist/validation/schema.js +768 -0
  98. package/dist/validation/schema.test.js +35 -0
  99. package/dist/watcher.js +79 -0
  100. package/package.json +1 -1
@@ -0,0 +1,293 @@
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 child_process_1 = require("child_process");
7
+ const os_1 = __importDefault(require("os"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const binary_install_raw_1 = require("binary-install-raw");
10
+ const chalk_1 = __importDefault(require("chalk"));
11
+ const gluegun_1 = require("gluegun");
12
+ const js_yaml_1 = __importDefault(require("js-yaml"));
13
+ const node_fetch_1 = __importDefault(require("node-fetch"));
14
+ const semver_1 = __importDefault(require("semver"));
15
+ const gluegun_2 = require("../command-helpers/gluegun");
16
+ const HELP = `
17
+ ${chalk_1.default.bold('graph test')} ${chalk_1.default.dim('[options]')} ${chalk_1.default.bold('<datasource>')}
18
+
19
+ ${chalk_1.default.dim('Options:')}
20
+ -c, --coverage Run the tests in coverage mode
21
+ -d, --docker Run the tests in a docker container(Note: Please execute from the root folder of the subgraph)
22
+ -f --force Binary - overwrites folder + file when downloading. Docker - rebuilds the docker image
23
+ -h, --help Show usage information
24
+ -l, --logs Logs to the console information about the OS, CPU model and download url (debugging purposes)
25
+ -r, --recompile Force-recompile tests
26
+ -v, --version <tag> Choose the version of the rust binary that you want to be downloaded/used
27
+ `;
28
+ exports.default = {
29
+ description: 'Runs rust binary for subgraph testing',
30
+ run: async (toolbox) => {
31
+ // Read CLI parameters
32
+ const { c, coverage, d, docker, f, force, h, help, l, logs, r, recompile, v, version } = toolbox.parameters.options;
33
+ const testsFolder = './tests';
34
+ const opts = {
35
+ testsFolder,
36
+ cachePath: path_1.default.join(testsFolder, '.latest.json'),
37
+ };
38
+ // Support both long and short option variants
39
+ opts.coverage = coverage || c;
40
+ opts.docker = docker || d;
41
+ opts.force = force || f;
42
+ opts.help = help || h;
43
+ opts.logs = logs || l;
44
+ opts.recompile = recompile || r;
45
+ opts.version = version || v;
46
+ // Fix if a boolean flag (e.g -c, --coverage) has an argument
47
+ try {
48
+ (0, gluegun_2.fixParameters)(toolbox.parameters, {
49
+ h,
50
+ help,
51
+ c,
52
+ coverage,
53
+ d,
54
+ docker,
55
+ f,
56
+ force,
57
+ l,
58
+ logs,
59
+ r,
60
+ recompile,
61
+ });
62
+ }
63
+ catch (e) {
64
+ gluegun_1.print.error(e.message);
65
+ process.exitCode = 1;
66
+ return;
67
+ }
68
+ const datasource = toolbox.parameters.first || toolbox.parameters.array?.[0];
69
+ // Show help text if requested
70
+ if (opts.help) {
71
+ gluegun_1.print.info(HELP);
72
+ return;
73
+ }
74
+ // Check if matchstick.yaml config exists
75
+ if (gluegun_1.filesystem.exists('matchstick.yaml')) {
76
+ try {
77
+ // Load the config
78
+ const config = await js_yaml_1.default.load(gluegun_1.filesystem.read('matchstick.yaml', 'utf8'));
79
+ // Check if matchstick.yaml and testsFolder not null
80
+ if (config?.testsFolder) {
81
+ // assign test folder from matchstick.yaml if present
82
+ opts.testsFolder = config.testsFolder;
83
+ }
84
+ }
85
+ catch (error) {
86
+ gluegun_1.print.info('A problem occurred while reading matchstick.yaml. Please attend to the errors below:');
87
+ gluegun_1.print.error(error.message);
88
+ process.exit(1);
89
+ }
90
+ }
91
+ setVersionFromCache(opts);
92
+ // Fetch the latest version tag if version is not specified with -v/--version or if the version is not cached
93
+ if (opts.force || (!opts.version && !opts.latestVersion)) {
94
+ gluegun_1.print.info('Fetching latest version tag');
95
+ const result = await (0, node_fetch_1.default)('https://api.github.com/repos/LimeChain/matchstick/releases/latest');
96
+ const json = await result.json();
97
+ opts.latestVersion = json.tag_name;
98
+ gluegun_1.filesystem.file(opts.cachePath, {
99
+ content: {
100
+ version: json.tag_name,
101
+ timestamp: Date.now(),
102
+ },
103
+ });
104
+ }
105
+ if (opts.docker) {
106
+ runDocker(datasource, opts);
107
+ }
108
+ else {
109
+ runBinary(datasource, opts);
110
+ }
111
+ },
112
+ };
113
+ async function setVersionFromCache(opts) {
114
+ if (gluegun_1.filesystem.exists(opts.cachePath) == 'file') {
115
+ const cached = gluegun_1.filesystem.read(opts.cachePath, 'json');
116
+ // Get the cache age in days
117
+ const cacheAge = (Date.now() - cached.timestamp) / (1000 * 60 * 60 * 24);
118
+ // If cache age is less than 1 day, use the cached version
119
+ if (cacheAge < 1) {
120
+ opts.latestVersion = cached.version;
121
+ }
122
+ }
123
+ }
124
+ async function runBinary(datasource, opts) {
125
+ const coverageOpt = opts.coverage;
126
+ const forceOpt = opts.force;
127
+ const logsOpt = opts.logs;
128
+ const versionOpt = opts.version;
129
+ const latestVersion = opts.latestVersion;
130
+ const recompileOpt = opts.recompile;
131
+ const platform = await getPlatform(logsOpt);
132
+ const url = `https://github.com/LimeChain/matchstick/releases/download/${versionOpt || latestVersion}/${platform}`;
133
+ if (logsOpt) {
134
+ gluegun_1.print.info(`Download link: ${url}`);
135
+ }
136
+ const binary = new binary_install_raw_1.Binary(platform, url, versionOpt || latestVersion);
137
+ forceOpt ? await binary.install(true) : await binary.install(false);
138
+ const args = [];
139
+ if (coverageOpt)
140
+ args.push('-c');
141
+ if (recompileOpt)
142
+ args.push('-r');
143
+ if (datasource)
144
+ args.push(datasource);
145
+ args.length > 0 ? binary.run(...args) : binary.run();
146
+ }
147
+ async function getPlatform(logsOpt) {
148
+ const type = os_1.default.type();
149
+ const arch = os_1.default.arch();
150
+ const cpuCore = os_1.default.cpus()[0];
151
+ const isAppleSilicon = arch === 'arm64' && /Apple (M1|M2|processor)/.test(cpuCore.model);
152
+ const linuxInfo = type === 'Linux' ? await getLinuxInfo() : {};
153
+ const linuxDistro = linuxInfo.name;
154
+ const release = linuxInfo.version || os_1.default.release();
155
+ const majorVersion = parseInt(linuxInfo.version || '', 10) || semver_1.default.major(release);
156
+ if (logsOpt) {
157
+ gluegun_1.print.info(`OS type: ${linuxDistro || type}\nOS arch: ${arch}\nOS release: ${release}\nOS major version: ${majorVersion}\nCPU model: ${cpuCore.model}`);
158
+ }
159
+ if (arch === 'x64' || isAppleSilicon) {
160
+ if (type === 'Darwin') {
161
+ if (majorVersion === 18 || majorVersion === 19) {
162
+ return 'binary-macos-10.15'; // GitHub dropped support for macOS 10.14 in Actions, but it seems 10.15 binary works on 10.14 too
163
+ }
164
+ if (isAppleSilicon) {
165
+ return 'binary-macos-11-m1';
166
+ }
167
+ return 'binary-macos-11';
168
+ }
169
+ if (type === 'Linux') {
170
+ if (majorVersion === 18) {
171
+ return 'binary-linux-18';
172
+ }
173
+ if (majorVersion === 22) {
174
+ return 'binary-linux-22';
175
+ }
176
+ return 'binary-linux-20';
177
+ }
178
+ }
179
+ throw new Error(`Unsupported platform: ${type} ${arch} ${majorVersion}`);
180
+ }
181
+ async function getLinuxInfo() {
182
+ try {
183
+ const result = await gluegun_1.system.run("cat /etc/*-release | grep -E '(^VERSION|^NAME)='", {
184
+ trim: true,
185
+ });
186
+ const infoArray = result
187
+ .replace(/['"]+/g, '')
188
+ .split('\n')
189
+ .map(p => p.split('='));
190
+ const linuxInfo = {};
191
+ infoArray.forEach(val => {
192
+ linuxInfo[val[0].toLowerCase()] = val[1];
193
+ });
194
+ return linuxInfo;
195
+ }
196
+ catch (error) {
197
+ gluegun_1.print.error(`Error fetching the Linux version:\n ${error}`);
198
+ process.exit(1);
199
+ }
200
+ }
201
+ async function runDocker(datasource, opts) {
202
+ const coverageOpt = opts.coverage;
203
+ const forceOpt = opts.force;
204
+ const versionOpt = opts.version;
205
+ const latestVersion = opts.latestVersion;
206
+ const recompileOpt = opts.recompile;
207
+ // Remove binary-install-raw binaries, because docker has permission issues
208
+ // when building the docker images
209
+ gluegun_1.filesystem.remove('./node_modules/binary-install-raw/bin');
210
+ // Get current working directory
211
+ const current_folder = gluegun_1.filesystem.cwd();
212
+ // Declate dockerfilePath with default location
213
+ const dockerfilePath = path_1.default.join(opts.testsFolder || 'tests', '.docker/Dockerfile');
214
+ // Check if the Dockerfil already exists
215
+ const dockerfileExists = gluegun_1.filesystem.exists(dockerfilePath);
216
+ // Generate the Dockerfile only if it doesn't exists,
217
+ // version flag and/or force flag is passed.
218
+ if (!dockerfileExists || versionOpt || forceOpt) {
219
+ await dockerfile(dockerfilePath, versionOpt, latestVersion);
220
+ }
221
+ // Run a command to check if matchstick image already exists
222
+ (0, child_process_1.exec)('docker images -q matchstick', (_error, stdout, _stderr) => {
223
+ // Collect all(if any) flags and options that have to be passed to the matchstick binary
224
+ let testArgs = '';
225
+ if (coverageOpt)
226
+ testArgs = testArgs + ' -c';
227
+ if (recompileOpt)
228
+ testArgs = testArgs + ' -r';
229
+ if (datasource)
230
+ testArgs = testArgs + ' ' + datasource;
231
+ // Build the `docker run` command options and flags
232
+ const dockerRunOpts = [
233
+ 'run',
234
+ '-it',
235
+ '--rm',
236
+ '--mount',
237
+ `type=bind,source=${current_folder},target=/matchstick`,
238
+ ];
239
+ if (testArgs !== '') {
240
+ dockerRunOpts.push('-e');
241
+ dockerRunOpts.push(`ARGS=${testArgs.trim()}`);
242
+ }
243
+ dockerRunOpts.push('matchstick');
244
+ // If a matchstick image does not exists, the command returns an empty string,
245
+ // else it'll return the image ID. Skip `docker build` if an image already exists
246
+ // Delete current image(if any) and rebuild.
247
+ // Use spawn() and {stdio: 'inherit'} so we can see the logs in real time.
248
+ if (!dockerfileExists || stdout === '' || versionOpt || forceOpt) {
249
+ if (stdout !== '') {
250
+ (0, child_process_1.exec)('docker image rm matchstick', (_error, stdout, _stderr) => {
251
+ gluegun_1.print.info(chalk_1.default.bold(`Removing matchstick image\n${stdout}`));
252
+ });
253
+ }
254
+ // Build a docker image. If the process has executed successfully
255
+ // run a container from that image.
256
+ (0, child_process_1.spawn)('docker', ['build', '-f', dockerfilePath, '-t', 'matchstick', '.'], {
257
+ stdio: 'inherit',
258
+ }).on('close', code => {
259
+ if (code === 0) {
260
+ (0, child_process_1.spawn)('docker', dockerRunOpts, { stdio: 'inherit' });
261
+ }
262
+ });
263
+ }
264
+ else {
265
+ gluegun_1.print.info('Docker image already exists. Skipping `docker build` command.');
266
+ // Run the container from the existing matchstick docker image
267
+ (0, child_process_1.spawn)('docker', dockerRunOpts, { stdio: 'inherit' });
268
+ }
269
+ });
270
+ }
271
+ // Downloads Dockerfile template from the demo-subgraph repo
272
+ // Replaces the placeholders with their respective values
273
+ async function dockerfile(dockerfilePath, versionOpt, latestVersion) {
274
+ const spinner = gluegun_1.print.spin('Generating Dockerfile...');
275
+ try {
276
+ // Fetch the Dockerfile template content from the demo-subgraph repo
277
+ const content = await (0, node_fetch_1.default)('https://raw.githubusercontent.com/LimeChain/demo-subgraph/main/Dockerfile').then(response => {
278
+ if (response.ok) {
279
+ return response.text();
280
+ }
281
+ throw new Error(`Status Code: ${response.status}, with error: ${response.statusText}`);
282
+ });
283
+ // Write the Dockerfile
284
+ gluegun_1.filesystem.write(dockerfilePath, content);
285
+ // Replaces the version placeholders
286
+ await gluegun_1.patching.replace(dockerfilePath, '<MATCHSTICK_VERSION>', versionOpt || latestVersion || 'unknown');
287
+ }
288
+ catch (error) {
289
+ spinner.fail(`A problem occurred while generating the Dockerfile. Please attend to the errors below:\n ${error.message}`);
290
+ process.exit(1);
291
+ }
292
+ spinner.succeed('Successfully generated Dockerfile.');
293
+ }
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.compile = exports.ready = void 0;
27
+ const asc = __importStar(require("assemblyscript/cli/asc"));
28
+ const createExitHandler = (inputFile) => () => {
29
+ throw new Error(`The AssemblyScript compiler crashed when compiling this file: '${inputFile}'
30
+ Suggestion: try to comment the whole file and uncomment it little by little while re-running the graph-cli until you isolate the line where the problem happens.
31
+ Also, please contact us so we can make the CLI better by handling errors like this. You can reach out in any of these links:
32
+ - Discord channel: https://discord.gg/eM8CA6WA9r
33
+ - Github issues: https://github.com/graphprotocol/graph-cli/issues`);
34
+ };
35
+ const setupExitHandler = (exitHandler) => process.addListener('exit', exitHandler);
36
+ const removeExitHandler = (exitHandler) => process.removeListener('exit', exitHandler);
37
+ // Important note, the `asc.main` callback function parameter is synchronous,
38
+ // that's why this function doesn't need to be `async` and the throw works properly.
39
+ const assemblyScriptCompiler = (argv, options) => asc.main(argv, options, err => {
40
+ if (err) {
41
+ throw err;
42
+ }
43
+ return 0;
44
+ });
45
+ const compilerDefaults = {
46
+ stdout: process.stdout,
47
+ stderr: process.stdout,
48
+ };
49
+ // You MUST call this function once before compiling anything.
50
+ // Internally it just delegates to the AssemblyScript compiler
51
+ // which just delegates to the binaryen lib.
52
+ const ready = async () => {
53
+ await asc.ready;
54
+ };
55
+ exports.ready = ready;
56
+ // For now this function doesn't check if `asc` is ready, because
57
+ // it requires an asynchronous wait. Whenever you call this function,
58
+ // it doesn't matter how many times, just make sure you call `ready`
59
+ // once before everything..
60
+ const compile = ({ inputFile, global, baseDir, libs, outputFile }) => {
61
+ const exitHandler = createExitHandler(inputFile);
62
+ setupExitHandler(exitHandler);
63
+ const compilerArgs = [
64
+ '--explicitStart',
65
+ '--exportRuntime',
66
+ '--runtime',
67
+ 'stub',
68
+ inputFile,
69
+ global,
70
+ '--baseDir',
71
+ baseDir,
72
+ '--lib',
73
+ libs,
74
+ '--outFile',
75
+ outputFile,
76
+ '--optimize',
77
+ '--debug',
78
+ ];
79
+ assemblyScriptCompiler(compilerArgs, compilerDefaults);
80
+ // only if compiler succeded, that is, when the line above doesn't throw
81
+ removeExitHandler(exitHandler);
82
+ };
83
+ exports.compile = compile;