1
1
const os = require ( 'os' )
2
2
const chalk = require ( 'chalk' )
3
3
const fetch = require ( 'node-fetch' )
4
+ const { fixParameters } = require ( '../command-helpers/gluegun' )
4
5
const semver = require ( 'semver' )
5
6
const { spawn, exec } = require ( 'child_process' )
6
7
const fs = require ( 'fs' )
7
8
8
9
const HELP = `
9
- ${ chalk . bold ( 'graph test' ) } ${ chalk . bold ( '<datasource> ') } ${ chalk . dim ( '[options] ') }
10
+ ${ chalk . bold ( 'graph test' ) } ${ chalk . dim ( '[options] ') } ${ chalk . bold ( '<datasource> ') }
10
11
11
12
${ chalk . dim ( 'Options:' ) }
12
13
-h, --help Show usage information
@@ -23,26 +24,41 @@ module.exports = {
23
24
24
25
// Read CLI parameters
25
26
let { h, help, v, version, c, coverage } = toolbox . parameters . options
26
- let datasource = toolbox . parameters . first
27
27
28
28
// 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 ]
32
48
33
49
// Show help text if requested
34
- if ( help ) {
50
+ if ( help_opt ) {
35
51
print . info ( HELP )
36
52
return
37
53
}
38
54
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` ;
41
57
42
58
await fetch ( url )
43
59
. then ( response => {
44
60
if ( response . status === 404 ) {
45
- print . info ( `Error: Invalid Matchstick version '${ version } '` ) ;
61
+ print . info ( `Error: Invalid Matchstick version '${ version_opt } '` ) ;
46
62
process . exit ( 1 ) ;
47
63
}
48
64
} )
@@ -65,7 +81,7 @@ RUN apt install -y curl
65
81
RUN npm install -g @graphprotocol/graph-cli
66
82
67
83
# 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
69
85
# Make it executable
70
86
RUN chmod a+x binary-linux-20
71
87
@@ -106,31 +122,31 @@ CMD ../binary-linux-20 \${ARGS}
106
122
// Getting the current working folder that will be passed to the
107
123
// `docker run` command to be bind mounted.
108
124
let current_folder = process . cwd ( ) ;
109
- let args = '' ;
125
+ let test_args = '' ;
110
126
111
127
if ( datasource ) {
112
- args = args + datasource
128
+ test_args = test_args + datasource
113
129
}
114
130
115
- if ( coverage ) {
116
- args = args + ' ' + '-c'
131
+ if ( coverage_opt ) {
132
+ test_args = test_args + ' ' + '-c'
117
133
}
118
134
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` ] ;
120
136
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 ( ) } ` ) ;
124
140
}
125
141
126
- options . push ( 'matchstick' )
142
+ docker_run_opts . push ( 'matchstick' )
127
143
128
144
// If a matchstick image does not exists, the command returns an empty string,
129
145
// else it'll return the image ID. Skip `docker build` if an image already exists
130
146
// If `-v/--version` is specified, delete current image(if any) and rebuild.
131
147
// 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 ) {
134
150
exec ( 'docker image rm matchstick' , ( error , stdout , stderr ) => {
135
151
print . info ( chalk . bold ( `Removing matchstick image\n${ stdout } ` ) ) ;
136
152
} ) ;
@@ -143,12 +159,12 @@ CMD ../binary-linux-20 \${ARGS}
143
159
{ stdio : 'inherit' }
144
160
) . on ( 'close' , code => {
145
161
if ( code === 0 ) {
146
- spawn ( 'docker' , options , { stdio : 'inherit' } ) ;
162
+ spawn ( 'docker' , docker_run_opts , { stdio : 'inherit' } ) ;
147
163
}
148
164
} )
149
165
} else {
150
166
// Run the container from the existing matchstick docker image
151
- spawn ( 'docker' , options , { stdio : 'inherit' } ) ;
167
+ spawn ( 'docker' , docker_run_opts , { stdio : 'inherit' } ) ;
152
168
}
153
169
} )
154
170
} ,
0 commit comments