@graphprotocol/graph-cli 0.40.0 → 0.40.1-alpha-20230216174117-760a195

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.
@@ -6,92 +6,48 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const child_process_1 = require("child_process");
7
7
  const os_1 = __importDefault(require("os"));
8
8
  const path_1 = __importDefault(require("path"));
9
+ const core_1 = require("@oclif/core");
9
10
  const binary_install_raw_1 = require("binary-install-raw");
10
- const chalk_1 = __importDefault(require("chalk"));
11
11
  const gluegun_1 = require("gluegun");
12
12
  const js_yaml_1 = __importDefault(require("js-yaml"));
13
13
  const node_fetch_1 = __importDefault(require("node-fetch"));
14
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';
15
+ class TestCommand extends core_1.Command {
16
+ async run() {
17
+ const { args: { datasource }, flags: { coverage, docker, force, logs, recompile, version }, } = await this.parse(TestCommand);
18
+ const testsDir = './tests';
19
+ const cachePath = path_1.default.resolve(testsDir, '.latest.json');
34
20
  const opts = {
35
- testsFolder,
36
- cachePath: path_1.default.join(testsFolder, '.latest.json'),
21
+ testsDir,
22
+ cachePath,
23
+ coverage,
24
+ docker,
25
+ force,
26
+ logs,
27
+ recompile,
28
+ version,
29
+ latestVersion: getLatestVersionFromCache(cachePath),
37
30
  };
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
31
  // Check if matchstick.yaml config exists
75
32
  if (gluegun_1.filesystem.exists('matchstick.yaml')) {
76
33
  try {
77
34
  // Load the config
78
35
  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) {
36
+ // Check if matchstick.yaml and testsDir not null
37
+ if (config?.testsDir) {
81
38
  // assign test folder from matchstick.yaml if present
82
- opts.testsFolder = config.testsFolder;
39
+ opts.testsDir = config.testsDir;
83
40
  }
84
41
  }
85
42
  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);
43
+ this.error(`A problem occurred while reading "matchstick.yaml":\n${error.message}`, {
44
+ exit: 1,
45
+ });
89
46
  }
90
47
  }
91
- setVersionFromCache(opts);
92
48
  // Fetch the latest version tag if version is not specified with -v/--version or if the version is not cached
93
49
  if (opts.force || (!opts.version && !opts.latestVersion)) {
94
- gluegun_1.print.info('Fetching latest version tag');
50
+ this.log('Fetching latest version tag...');
95
51
  const result = await (0, node_fetch_1.default)('https://api.github.com/repos/LimeChain/matchstick/releases/latest');
96
52
  const json = await result.json();
97
53
  opts.latestVersion = json.tag_name;
@@ -103,23 +59,58 @@ exports.default = {
103
59
  });
104
60
  }
105
61
  if (opts.docker) {
106
- runDocker(datasource, opts);
62
+ runDocker.bind(this)(datasource, opts);
107
63
  }
108
64
  else {
109
- runBinary(datasource, opts);
65
+ runBinary.bind(this)(datasource, opts);
110
66
  }
111
- },
67
+ }
68
+ }
69
+ exports.default = TestCommand;
70
+ TestCommand.description = 'Runs rust binary for subgraph testing.';
71
+ TestCommand.args = {
72
+ datasource: core_1.Args.string({
73
+ required: true,
74
+ }),
112
75
  };
113
- async function setVersionFromCache(opts) {
114
- if (gluegun_1.filesystem.exists(opts.cachePath) == 'file') {
115
- const cached = gluegun_1.filesystem.read(opts.cachePath, 'json');
76
+ TestCommand.flags = {
77
+ coverage: core_1.Flags.boolean({
78
+ summary: 'Run the tests in coverage mode.',
79
+ char: 'c',
80
+ }),
81
+ docker: core_1.Flags.boolean({
82
+ summary: 'Run the tests in a docker container (Note: Please execute from the root folder of the subgraph).',
83
+ char: 'd',
84
+ }),
85
+ force: core_1.Flags.boolean({
86
+ summary: 'Binary - overwrites folder + file when downloading. Docker - rebuilds the docker image.',
87
+ char: 'f',
88
+ }),
89
+ logs: core_1.Flags.boolean({
90
+ summary: 'Logs to the console information about the OS, CPU model and download url (debugging purposes).',
91
+ char: 'l',
92
+ }),
93
+ recompile: core_1.Flags.boolean({
94
+ summary: 'Force-recompile tests.',
95
+ char: 'r',
96
+ }),
97
+ version: core_1.Flags.string({
98
+ summary: 'Choose the version of the rust binary that you want to be downloaded/used.',
99
+ char: 'v',
100
+ required: true,
101
+ }),
102
+ };
103
+ function getLatestVersionFromCache(cachePath) {
104
+ if (gluegun_1.filesystem.exists(cachePath) == 'file') {
105
+ const cached = gluegun_1.filesystem.read(cachePath, 'json');
116
106
  // Get the cache age in days
117
107
  const cacheAge = (Date.now() - cached.timestamp) / (1000 * 60 * 60 * 24);
118
108
  // If cache age is less than 1 day, use the cached version
119
109
  if (cacheAge < 1) {
120
- opts.latestVersion = cached.version;
110
+ return cached.version;
121
111
  }
122
112
  }
113
+ return null;
123
114
  }
124
115
  async function runBinary(datasource, opts) {
125
116
  const coverageOpt = opts.coverage;
@@ -128,10 +119,11 @@ async function runBinary(datasource, opts) {
128
119
  const versionOpt = opts.version;
129
120
  const latestVersion = opts.latestVersion;
130
121
  const recompileOpt = opts.recompile;
131
- const platform = await getPlatform(logsOpt);
132
- const url = `https://github.com/LimeChain/matchstick/releases/download/${versionOpt || latestVersion}/${platform}`;
122
+ const platform = await getPlatform.bind(this)(logsOpt);
123
+ const url = `https://github.com/LimeChain/matchstick/releases/download/${versionOpt ||
124
+ latestVersion}/${platform}`;
133
125
  if (logsOpt) {
134
- gluegun_1.print.info(`Download link: ${url}`);
126
+ this.log(`Download link: ${url}`);
135
127
  }
136
128
  const binary = new binary_install_raw_1.Binary(platform, url, versionOpt || latestVersion);
137
129
  forceOpt ? await binary.install(true) : await binary.install(false);
@@ -149,12 +141,13 @@ async function getPlatform(logsOpt) {
149
141
  const arch = os_1.default.arch();
150
142
  const cpuCore = os_1.default.cpus()[0];
151
143
  const isAppleSilicon = arch === 'arm64' && /Apple (M1|M2|processor)/.test(cpuCore.model);
152
- const linuxInfo = type === 'Linux' ? await getLinuxInfo() : {};
144
+ const linuxInfo = type === 'Linux' ? await getLinuxInfo.bind(this)() : {};
153
145
  const linuxDistro = linuxInfo.name;
154
146
  const release = linuxInfo.version || os_1.default.release();
155
147
  const majorVersion = parseInt(linuxInfo.version || '', 10) || semver_1.default.major(release);
156
148
  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}`);
149
+ this.log(`OS type: ${linuxDistro ||
150
+ type}\nOS arch: ${arch}\nOS release: ${release}\nOS major version: ${majorVersion}\nCPU model: ${cpuCore.model}`);
158
151
  }
159
152
  if (arch === 'x64' || isAppleSilicon) {
160
153
  if (type === 'Darwin') {
@@ -194,8 +187,7 @@ async function getLinuxInfo() {
194
187
  return linuxInfo;
195
188
  }
196
189
  catch (error) {
197
- gluegun_1.print.error(`Error fetching the Linux version:\n ${error}`);
198
- process.exit(1);
190
+ this.error(`Error fetching the Linux version:\n${error}`, { exit: 1 });
199
191
  }
200
192
  }
201
193
  async function runDocker(datasource, opts) {
@@ -210,13 +202,13 @@ async function runDocker(datasource, opts) {
210
202
  // Get current working directory
211
203
  const current_folder = gluegun_1.filesystem.cwd();
212
204
  // Declate dockerfilePath with default location
213
- const dockerfilePath = path_1.default.join(opts.testsFolder || 'tests', '.docker/Dockerfile');
205
+ const dockerfilePath = path_1.default.join(opts.testsDir || 'tests', '.docker/Dockerfile');
214
206
  // Check if the Dockerfil already exists
215
207
  const dockerfileExists = gluegun_1.filesystem.exists(dockerfilePath);
216
208
  // Generate the Dockerfile only if it doesn't exists,
217
209
  // version flag and/or force flag is passed.
218
210
  if (!dockerfileExists || versionOpt || forceOpt) {
219
- await dockerfile(dockerfilePath, versionOpt, latestVersion);
211
+ await dockerfile.bind(this)(dockerfilePath, versionOpt, latestVersion);
220
212
  }
221
213
  // Run a command to check if matchstick image already exists
222
214
  (0, child_process_1.exec)('docker images -q matchstick', (_error, stdout, _stderr) => {
@@ -248,7 +240,7 @@ async function runDocker(datasource, opts) {
248
240
  if (!dockerfileExists || stdout === '' || versionOpt || forceOpt) {
249
241
  if (stdout !== '') {
250
242
  (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}`));
243
+ this.log(`Remove matchstick image result:\n${stdout}`);
252
244
  });
253
245
  }
254
246
  // Build a docker image. If the process has executed successfully
@@ -262,7 +254,7 @@ async function runDocker(datasource, opts) {
262
254
  });
263
255
  }
264
256
  else {
265
- gluegun_1.print.info('Docker image already exists. Skipping `docker build` command.');
257
+ this.log('Docker image already exists. Skipping `docker build` command...');
266
258
  // Run the container from the existing matchstick docker image
267
259
  (0, child_process_1.spawn)('docker', dockerRunOpts, { stdio: 'inherit' });
268
260
  }
@@ -271,7 +263,7 @@ async function runDocker(datasource, opts) {
271
263
  // Downloads Dockerfile template from the demo-subgraph repo
272
264
  // Replaces the placeholders with their respective values
273
265
  async function dockerfile(dockerfilePath, versionOpt, latestVersion) {
274
- const spinner = gluegun_1.print.spin('Generating Dockerfile...');
266
+ core_1.ux.action.start('Generating Dockerfile');
275
267
  try {
276
268
  // Fetch the Dockerfile template content from the demo-subgraph repo
277
269
  const content = await (0, node_fetch_1.default)('https://raw.githubusercontent.com/LimeChain/demo-subgraph/main/Dockerfile').then(response => {
@@ -286,8 +278,9 @@ async function dockerfile(dockerfilePath, versionOpt, latestVersion) {
286
278
  await gluegun_1.patching.replace(dockerfilePath, '<MATCHSTICK_VERSION>', versionOpt || latestVersion || 'unknown');
287
279
  }
288
280
  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);
281
+ this.error(`A problem occurred while generating the Dockerfile:\n${error.message}`, {
282
+ exit: 1,
283
+ });
291
284
  }
292
- spinner.succeed('Successfully generated Dockerfile.');
285
+ core_1.ux.action.stop('Successfully generated Dockerfile');
293
286
  }
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.run = void 0;
4
+ var core_1 = require("@oclif/core");
5
+ Object.defineProperty(exports, "run", { enumerable: true, get: function () { return core_1.run; } });