@@ -2,18 +2,18 @@ const { Binary } = require('binary-install-raw')
2
2
const os = require ( 'os' )
3
3
const chalk = require ( 'chalk' )
4
4
const fetch = require ( 'node-fetch' )
5
+ const fs = require ( 'fs' )
5
6
const { fixParameters } = require ( '../command-helpers/gluegun' )
6
7
const semver = require ( 'semver' )
7
8
const { spawn, exec } = require ( 'child_process' )
8
- const fs = require ( 'fs' )
9
9
10
10
const HELP = `
11
11
${ chalk . bold ( 'graph test' ) } ${ chalk . dim ( '[options]' ) } ${ chalk . bold ( '<datasource>' ) }
12
12
13
13
${ 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
17
17
-h, --help Show usage information
18
18
-l, --logs Logs to the console information about the OS, CPU model and download url (debugging purposes)
19
19
-v, --version <tag> Choose the version of the rust binary that you want to be downloaded/used
@@ -42,12 +42,12 @@ module.exports = {
42
42
} = toolbox . parameters . options
43
43
44
44
// 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
51
51
52
52
// Fix if a boolean flag (e.g -c, --coverage) has an argument
53
53
try {
@@ -72,50 +72,50 @@ module.exports = {
72
72
let datasource = toolbox . parameters . first || toolbox . parameters . array [ 0 ]
73
73
74
74
// Show help text if requested
75
- if ( help_opt ) {
75
+ if ( helpOpt ) {
76
76
print . info ( HELP )
77
77
return
78
78
}
79
79
80
80
let result = await fetch ( 'https://api.github.com/repos/LimeChain/matchstick/releases/latest' )
81
81
let json = await result . json ( )
82
- let latest_version = json . tag_name
82
+ let latestVersion = json . tag_name
83
83
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 )
86
86
} else {
87
- runBinary ( coverage_opt , datasource , force_opt , logs_opt , version_opt , latest_version , print )
87
+ runBinary ( coverageOpt , datasource , forceOpt , logsOpt , versionOpt , latestVersion , print )
88
88
}
89
89
}
90
90
}
91
91
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 )
94
94
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 } `
96
96
97
- if ( logs_opt ) {
97
+ if ( logsOpt ) {
98
98
console . log ( `Download link: ${ url } ` )
99
99
}
100
100
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 )
103
103
let args = ""
104
104
105
- datasource ? args = datasource : args
106
- coverage_opt ? args = args + ' -c' : args
105
+ if ( datasource ) args = datasource
106
+ if ( coverageOpt ) args = args + ' -c'
107
107
args !== '' ? binary . run ( args . trim ( ) ) : binary . run ( )
108
108
}
109
109
110
- function getPlatform ( logs_opt ) {
110
+ function getPlatform ( logsOpt ) {
111
111
const type = os . type ( )
112
112
const arch = os . arch ( )
113
113
const release = os . release ( )
114
114
const cpuCore = os . cpus ( ) [ 0 ]
115
115
const majorVersion = semver . major ( release )
116
116
const isM1 = cpuCore . model . includes ( "Apple M1" )
117
117
118
- if ( logs_opt ) {
118
+ if ( logsOpt ) {
119
119
console . log ( `OS type: ${ type } \nOS arch: ${ arch } \nOS release: ${ release } \nOS major version: ${ majorVersion } \nCPU model: ${ cpuCore . model } ` )
120
120
}
121
121
@@ -142,7 +142,7 @@ function getPlatform(logs_opt) {
142
142
throw new Error ( `Unsupported platform: ${ type } ${ arch } ${ majorVersion } ` )
143
143
}
144
144
145
- function runDocker ( coverage_opt , datasource , version_opt , latest_version , print ) {
145
+ function runDocker ( coverageOpt , datasource , versionOpt , latestVersion , forceOpt , print ) {
146
146
// Remove binary-install-raw binaries, because docker has permission issues
147
147
// when building the docker images
148
148
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)
154
154
}
155
155
156
156
try {
157
- fs . writeFileSync ( `${ dir } /Dockerfile` , dockerfile ( version_opt , latest_version ) )
157
+ fs . writeFileSync ( `${ dir } /Dockerfile` , dockerfile ( versionOpt , latestVersion ) )
158
158
print . info ( 'Successfully generated Dockerfile.' ) ;
159
159
} catch ( error ) {
160
160
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)
167
167
// Getting the current working folder that will be passed to the
168
168
// `docker run` command to be bind mounted.
169
169
let current_folder = process . cwd ( ) ;
170
- let test_args = '' ;
170
+ let testArgs = '' ;
171
171
172
172
if ( datasource ) {
173
- test_args = test_args + datasource
173
+ testArgs = testArgs + datasource
174
174
}
175
175
176
- if ( coverage_opt ) {
177
- test_args = test_args + ' ' + '-c'
176
+ if ( coverageOpt ) {
177
+ testArgs = testArgs + ' ' + '-c'
178
178
}
179
179
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` ] ;
181
181
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 ( ) } ` ) ;
185
185
}
186
186
187
- docker_run_opts . push ( 'matchstick' )
187
+ dockerRunOpts . push ( 'matchstick' )
188
188
189
189
// If a matchstick image does not exists, the command returns an empty string,
190
190
// else it'll return the image ID. Skip `docker build` if an image already exists
191
191
// If `-v/--version` is specified, delete current image(if any) and rebuild.
192
192
// 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 ) {
195
195
exec ( 'docker image rm matchstick' , ( error , stdout , stderr ) => {
196
196
print . info ( chalk . bold ( `Removing matchstick image\n${ stdout } ` ) ) ;
197
197
} ) ;
@@ -204,18 +204,18 @@ function runDocker(coverage_opt, datasource, version_opt, latest_version, print)
204
204
{ stdio : 'inherit' }
205
205
) . on ( 'close' , code => {
206
206
if ( code === 0 ) {
207
- spawn ( 'docker' , docker_run_opts , { stdio : 'inherit' } ) ;
207
+ spawn ( 'docker' , dockerRunOpts , { stdio : 'inherit' } ) ;
208
208
}
209
209
} )
210
210
} else {
211
211
// Run the container from the existing matchstick docker image
212
- spawn ( 'docker' , docker_run_opts , { stdio : 'inherit' } ) ;
212
+ spawn ( 'docker' , dockerRunOpts , { stdio : 'inherit' } ) ;
213
213
}
214
214
} )
215
215
}
216
216
217
217
// TODO: Move these in separate file (in a function maybe)
218
- function dockerfile ( version_opt , latest_version ) {
218
+ function dockerfile ( versionOpt , latestVersion ) {
219
219
return `
220
220
FROM ubuntu:20.04
221
221
ENV ARGS=""
@@ -230,7 +230,7 @@ function dockerfile(version_opt, latest_version) {
230
230
RUN npm install -g @graphprotocol/graph-cli
231
231
232
232
# 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
234
234
235
235
# Make it executable
236
236
RUN chmod a+x binary-linux-20
0 commit comments