Skip to content

Commit 00fed10

Browse files
Fix if boolean flag has argument. Refactoring
1 parent 4bb0970 commit 00fed10

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

src/commands/test.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const os = require('os')
22
const chalk = require('chalk')
33
const fetch = require('node-fetch')
4+
const { fixParameters } = require('../command-helpers/gluegun')
45
const semver = require('semver')
56
const { spawn, exec } = require('child_process')
67
const fs = require('fs')
78

89
const HELP = `
9-
${chalk.bold('graph test')} ${chalk.bold('<datasource>')} ${chalk.dim('[options]')}
10+
${chalk.bold('graph test')} ${chalk.dim('[options]')} ${chalk.bold('<datasource>')}
1011
1112
${chalk.dim('Options:')}
1213
-h, --help Show usage information
@@ -23,26 +24,41 @@ module.exports = {
2324

2425
// Read CLI parameters
2526
let { h, help, v, version, c, coverage } = toolbox.parameters.options
26-
let datasource = toolbox.parameters.first
2727

2828
// Support both long and short option variants
29-
help = help || h
30-
version = version || v
31-
coverage = coverage || c
29+
let help_opt = help || h
30+
let version_opt = version || v
31+
let coverage_opt = coverage || c
32+
33+
// Fix if a boolean flag (-h, --help, -c, --coverage) has an argument
34+
try {
35+
fixParameters(toolbox.parameters, {
36+
h,
37+
help,
38+
c,
39+
coverage,
40+
})
41+
} catch (e) {
42+
print.error(e.message)
43+
process.exitCode = 1
44+
return
45+
}
46+
47+
let datasource = toolbox.parameters.first || toolbox.parameters.array[0]
3248

3349
// Show help text if requested
34-
if (help) {
50+
if (help_opt) {
3551
print.info(HELP)
3652
return
3753
}
3854

39-
if(version) {
40-
let url = `https://github.com/LimeChain/matchstick/releases/download/${version || "0.2.2a"}/binary-linux-20`;
55+
if(version_opt) {
56+
let url = `https://github.com/LimeChain/matchstick/releases/download/${version_opt || "0.2.2a"}/binary-linux-20`;
4157

4258
await fetch(url)
4359
.then(response => {
4460
if (response.status === 404){
45-
print.info(`Error: Invalid Matchstick version '${version}'`);
61+
print.info(`Error: Invalid Matchstick version '${version_opt}'`);
4662
process.exit(1);
4763
}
4864
})
@@ -65,7 +81,7 @@ RUN apt install -y curl
6581
RUN npm install -g @graphprotocol/graph-cli
6682
6783
# Download the latest linux binary
68-
RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/${version || "0.2.2a"}/binary-linux-20
84+
RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/${version_opt || "0.2.2a"}/binary-linux-20
6985
# Make it executable
7086
RUN chmod a+x binary-linux-20
7187
@@ -106,31 +122,31 @@ CMD ../binary-linux-20 \${ARGS}
106122
// Getting the current working folder that will be passed to the
107123
// `docker run` command to be bind mounted.
108124
let current_folder = process.cwd();
109-
let args = '';
125+
let test_args = '';
110126

111127
if(datasource) {
112-
args = args + datasource
128+
test_args = test_args + datasource
113129
}
114130

115-
if(coverage) {
116-
args = args + ' ' + '-c'
131+
if(coverage_opt) {
132+
test_args = test_args + ' ' + '-c'
117133
}
118134

119-
let options = ['run', '-it', '--rm', '--mount', `type=bind,source=${current_folder},target=/matchstick`];
135+
let docker_run_opts = ['run', '-it', '--rm', '--mount', `type=bind,source=${current_folder},target=/matchstick`];
120136

121-
if(args !== '') {
122-
options.push('-e')
123-
options.push(`ARGS=${args.trim()}`);
137+
if(test_args !== '') {
138+
docker_run_opts.push('-e')
139+
docker_run_opts.push(`ARGS=${test_args.trim()}`);
124140
}
125141

126-
options.push('matchstick')
142+
docker_run_opts.push('matchstick')
127143

128144
// If a matchstick image does not exists, the command returns an empty string,
129145
// else it'll return the image ID. Skip `docker build` if an image already exists
130146
// If `-v/--version` is specified, delete current image(if any) and rebuild.
131147
// Use spawn() and {stdio: 'inherit'} so we can see the logs in real time.
132-
if(stdout === '' || version) {
133-
if (stdout !== '' && version) {
148+
if(stdout === '' || version_opt) {
149+
if (stdout !== '' && version_opt) {
134150
exec('docker image rm matchstick', (error, stdout, stderr) => {
135151
print.info(chalk.bold(`Removing matchstick image\n${stdout}`));
136152
});
@@ -143,12 +159,12 @@ CMD ../binary-linux-20 \${ARGS}
143159
{ stdio: 'inherit' }
144160
).on('close', code => {
145161
if (code === 0) {
146-
spawn('docker', options, { stdio: 'inherit' });
162+
spawn('docker', docker_run_opts, { stdio: 'inherit' });
147163
}
148164
})
149165
} else {
150166
// Run the container from the existing matchstick docker image
151-
spawn('docker', options, { stdio: 'inherit' });
167+
spawn('docker', docker_run_opts, { stdio: 'inherit' });
152168
}
153169
})
154170
},

0 commit comments

Comments
 (0)