@@ -2,18 +2,18 @@ const { Binary } = require('binary-install-raw')
22const os = require ( 'os' )
33const chalk = require ( 'chalk' )
44const fetch = require ( 'node-fetch' )
5+ const fs = require ( 'fs' )
56const { fixParameters } = require ( '../command-helpers/gluegun' )
67const semver = require ( 'semver' )
78const { spawn, exec } = require ( 'child_process' )
8- const fs = require ( 'fs' )
99
1010const HELP = `
1111${ chalk . bold ( 'graph test' ) } ${ chalk . dim ( '[options]' ) } ${ chalk . bold ( '<datasource>' ) }
1212
1313${ chalk . dim ( 'Options:' ) }
14- -c, --coverage Run the tests in coverage mode. Works with v0.2.1 and above (0.2.2 and above in docker mode).
15- -d, --docker Run the tests in a docker container
16- -f --force Overwrite folder + file when downloading
14+ -c, --coverage Run the tests in coverage mode. Works with v0.2.1 and above (0.2.2 and above in docker mode)
15+ -d, --docker Run the tests in a docker container(Note: Please execute from the root folder of the subgraph)
16+ -f --force Binary - overwrites folder + file when downloading. Docker - rebuilds the docker image
1717 -h, --help Show usage information
1818 -l, --logs Logs to the console information about the OS, CPU model and download url (debugging purposes)
1919 -v, --version <tag> Choose the version of the rust binary that you want to be downloaded/used
@@ -42,12 +42,12 @@ module.exports = {
4242 } = toolbox . parameters . options
4343
4444 // Support both long and short option variants
45- let coverage_opt = coverage || c
46- let docker_opt = docker || d
47- let force_opt = force || f
48- let help_opt = help || h
49- let logs_opt = logs || l
50- let version_opt = version || v
45+ let coverageOpt = coverage || c
46+ let dockerOpt = docker || d
47+ let forceOpt = force || f
48+ let helpOpt = help || h
49+ let logsOpt = logs || l
50+ let versionOpt = version || v
5151
5252 // Fix if a boolean flag (e.g -c, --coverage) has an argument
5353 try {
@@ -72,50 +72,50 @@ module.exports = {
7272 let datasource = toolbox . parameters . first || toolbox . parameters . array [ 0 ]
7373
7474 // Show help text if requested
75- if ( help_opt ) {
75+ if ( helpOpt ) {
7676 print . info ( HELP )
7777 return
7878 }
7979
8080 let result = await fetch ( 'https://api.github.com/repos/LimeChain/matchstick/releases/latest' )
8181 let json = await result . json ( )
82- let latest_version = json . tag_name
82+ let latestVersion = json . tag_name
8383
84- if ( docker_opt ) {
85- runDocker ( coverage_opt , datasource , version_opt , latest_version , print )
84+ if ( dockerOpt ) {
85+ runDocker ( coverageOpt , datasource , versionOpt , latestVersion , forceOpt , print )
8686 } else {
87- runBinary ( coverage_opt , datasource , force_opt , logs_opt , version_opt , latest_version , print )
87+ runBinary ( coverageOpt , datasource , forceOpt , logsOpt , versionOpt , latestVersion , print )
8888 }
8989 }
9090}
9191
92- async function runBinary ( coverage_opt , datasource , force_opt , logs_opt , version_opt , latest_version , print ) {
93- const platform = getPlatform ( logs_opt )
92+ async function runBinary ( coverageOpt , datasource , forceOpt , logsOpt , versionOpt , latestVersion , print ) {
93+ const platform = getPlatform ( logsOpt )
9494
95- const url = `https://github.com/LimeChain/matchstick/releases/download/${ version_opt || latest_version } /${ platform } `
95+ const url = `https://github.com/LimeChain/matchstick/releases/download/${ versionOpt || latestVersion } /${ platform } `
9696
97- if ( logs_opt ) {
97+ if ( logsOpt ) {
9898 console . log ( `Download link: ${ url } ` )
9999 }
100100
101- let binary = new Binary ( platform , url , version_opt )
102- force_opt ? await binary . install ( true ) : await binary . install ( false )
101+ let binary = new Binary ( platform , url , versionOpt )
102+ forceOpt ? await binary . install ( true ) : await binary . install ( false )
103103 let args = ""
104104
105- datasource ? args = datasource : args
106- coverage_opt ? args = args + ' -c' : args
105+ if ( datasource ) args = datasource
106+ if ( coverageOpt ) args = args + ' -c'
107107 args !== '' ? binary . run ( args . trim ( ) ) : binary . run ( )
108108}
109109
110- function getPlatform ( logs_opt ) {
110+ function getPlatform ( logsOpt ) {
111111 const type = os . type ( )
112112 const arch = os . arch ( )
113113 const release = os . release ( )
114114 const cpuCore = os . cpus ( ) [ 0 ]
115115 const majorVersion = semver . major ( release )
116116 const isM1 = cpuCore . model . includes ( "Apple M1" )
117117
118- if ( logs_opt ) {
118+ if ( logsOpt ) {
119119 console . log ( `OS type: ${ type } \nOS arch: ${ arch } \nOS release: ${ release } \nOS major version: ${ majorVersion } \nCPU model: ${ cpuCore . model } ` )
120120 }
121121
@@ -142,7 +142,7 @@ function getPlatform(logs_opt) {
142142 throw new Error ( `Unsupported platform: ${ type } ${ arch } ${ majorVersion } ` )
143143}
144144
145- function runDocker ( coverage_opt , datasource , version_opt , latest_version , print ) {
145+ function runDocker ( coverageOpt , datasource , versionOpt , latestVersion , forceOpt , print ) {
146146 // Remove binary-install-raw binaries, because docker has permission issues
147147 // when building the docker images
148148 fs . rmSync ( "node_modules/binary-install-raw/bin" , { force : true , recursive : true } ) ;
@@ -154,7 +154,7 @@ function runDocker(coverage_opt, datasource, version_opt, latest_version, print)
154154 }
155155
156156 try {
157- fs . writeFileSync ( `${ dir } /Dockerfile` , dockerfile ( version_opt , latest_version ) )
157+ fs . writeFileSync ( `${ dir } /Dockerfile` , dockerfile ( versionOpt , latestVersion ) )
158158 print . info ( 'Successfully generated Dockerfile.' ) ;
159159 } catch ( error ) {
160160 print . info ( 'A problem occurred while generating the Dockerfile. Please attend to the errors below:' ) ;
@@ -167,31 +167,31 @@ function runDocker(coverage_opt, datasource, version_opt, latest_version, print)
167167 // Getting the current working folder that will be passed to the
168168 // `docker run` command to be bind mounted.
169169 let current_folder = process . cwd ( ) ;
170- let test_args = '' ;
170+ let testArgs = '' ;
171171
172172 if ( datasource ) {
173- test_args = test_args + datasource
173+ testArgs = testArgs + datasource
174174 }
175175
176- if ( coverage_opt ) {
177- test_args = test_args + ' ' + '-c'
176+ if ( coverageOpt ) {
177+ testArgs = testArgs + ' ' + '-c'
178178 }
179179
180- let docker_run_opts = [ 'run' , '-it' , '--rm' , '--mount' , `type=bind,source=${ current_folder } ,target=/matchstick` ] ;
180+ let dockerRunOpts = [ 'run' , '-it' , '--rm' , '--mount' , `type=bind,source=${ current_folder } ,target=/matchstick` ] ;
181181
182- if ( test_args !== '' ) {
183- docker_run_opts . push ( '-e' )
184- docker_run_opts . push ( `ARGS=${ test_args . trim ( ) } ` ) ;
182+ if ( testArgs !== '' ) {
183+ dockerRunOpts . push ( '-e' )
184+ dockerRunOpts . push ( `ARGS=${ testArgs . trim ( ) } ` ) ;
185185 }
186186
187- docker_run_opts . push ( 'matchstick' )
187+ dockerRunOpts . push ( 'matchstick' )
188188
189189 // If a matchstick image does not exists, the command returns an empty string,
190190 // else it'll return the image ID. Skip `docker build` if an image already exists
191191 // If `-v/--version` is specified, delete current image(if any) and rebuild.
192192 // Use spawn() and {stdio: 'inherit'} so we can see the logs in real time.
193- if ( stdout === '' || version_opt ) {
194- if ( stdout !== '' && version_opt ) {
193+ if ( stdout === '' || versionOpt || forceOpt ) {
194+ if ( ( stdout !== '' && versionOpt ) || forceOpt ) {
195195 exec ( 'docker image rm matchstick' , ( error , stdout , stderr ) => {
196196 print . info ( chalk . bold ( `Removing matchstick image\n${ stdout } ` ) ) ;
197197 } ) ;
@@ -204,18 +204,18 @@ function runDocker(coverage_opt, datasource, version_opt, latest_version, print)
204204 { stdio : 'inherit' }
205205 ) . on ( 'close' , code => {
206206 if ( code === 0 ) {
207- spawn ( 'docker' , docker_run_opts , { stdio : 'inherit' } ) ;
207+ spawn ( 'docker' , dockerRunOpts , { stdio : 'inherit' } ) ;
208208 }
209209 } )
210210 } else {
211211 // Run the container from the existing matchstick docker image
212- spawn ( 'docker' , docker_run_opts , { stdio : 'inherit' } ) ;
212+ spawn ( 'docker' , dockerRunOpts , { stdio : 'inherit' } ) ;
213213 }
214214 } )
215215}
216216
217217// TODO: Move these in separate file (in a function maybe)
218- function dockerfile ( version_opt , latest_version ) {
218+ function dockerfile ( versionOpt , latestVersion ) {
219219 return `
220220 FROM ubuntu:20.04
221221 ENV ARGS=""
@@ -230,7 +230,7 @@ function dockerfile(version_opt, latest_version) {
230230 RUN npm install -g @graphprotocol/graph-cli
231231
232232 # Download the latest linux binary
233- RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/${ version_opt || latest_version } /binary-linux-20
233+ RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/${ versionOpt || latestVersion } /binary-linux-20
234234
235235 # Make it executable
236236 RUN chmod a+x binary-linux-20
0 commit comments