@@ -2,7 +2,7 @@ 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
+ const { filesystem } = require ( 'gluegun/filesystem ' )
6
6
const { fixParameters } = require ( '../command-helpers/gluegun' )
7
7
const semver = require ( 'semver' )
8
8
const { spawn, exec } = require ( 'child_process' )
@@ -115,7 +115,7 @@ async function runBinary(datasource, opts, print) {
115
115
116
116
let binary = new Binary ( platform , url , versionOpt || latestVersion )
117
117
forceOpt ? await binary . install ( true ) : await binary . install ( false )
118
- let args = new Array ( ) ;
118
+ let args = new Array ( )
119
119
120
120
if ( coverageOpt ) args . push ( '-c' )
121
121
if ( recompileOpt ) args . push ( '-r' )
@@ -158,7 +158,7 @@ function getPlatform(logsOpt) {
158
158
throw new Error ( `Unsupported platform: ${ type } ${ arch } ${ majorVersion } ` )
159
159
}
160
160
161
- function runDocker ( datasource , opts , print ) {
161
+ async function runDocker ( datasource , opts , print ) {
162
162
let coverageOpt = opts . get ( "coverage" )
163
163
let forceOpt = opts . get ( "force" )
164
164
let versionOpt = opts . get ( "version" )
@@ -167,48 +167,48 @@ function runDocker(datasource, opts, print) {
167
167
168
168
// Remove binary-install-raw binaries, because docker has permission issues
169
169
// when building the docker images
170
- fs . rmSync ( "./node_modules/binary-install-raw/bin" , { force : true , recursive : true } ) ;
170
+ await filesystem . remove ( "./node_modules/binary-install-raw/bin" )
171
171
172
- let dockerDir = "" ;
172
+ // Get current working directory
173
+ let current_folder = await filesystem . cwd ( )
174
+
175
+ // Build the Dockerfile location. Defaults to ./tests/.docker if
176
+ // a custom testsFolder is not delcared in the subgraph.yaml
177
+ let dockerDir = ""
173
178
174
179
try {
175
- let doc = yaml . load ( fs . readFileSync ( 'subgraph.yaml' , 'utf8' ) )
180
+ let doc = await yaml . load ( filesystem . read ( 'subgraph.yaml' , 'utf8' ) )
176
181
testsFolder = doc . testsFolder || './tests'
177
182
dockerDir = testsFolder . endsWith ( '/' ) ? testsFolder + '.docker' : testsFolder + '/.docker'
178
183
} catch ( e ) {
179
- print . error ( e ) ;
184
+ print . error ( e )
180
185
return
181
186
}
182
187
183
- if ( ! fs . existsSync ( dockerDir ) ) {
184
- fs . mkdirSync ( dockerDir , { recursive : true } ) ;
185
- }
186
-
188
+ // Create the Dockerfile
187
189
try {
188
- fs . writeFileSync ( `${ dockerDir } /Dockerfile` , dockerfile ( versionOpt , latestVersion ) )
189
- print . info ( 'Successfully generated Dockerfile.' ) ;
190
+ await filesystem . write ( `${ dockerDir } /Dockerfile` , dockerfile ( versionOpt , latestVersion ) )
191
+ print . info ( 'Successfully generated Dockerfile.' )
190
192
} catch ( error ) {
191
- print . info ( 'A problem occurred while generating the Dockerfile. Please attend to the errors below:' ) ;
192
- print . info ( chalk . red ( error ) ) ;
193
+ print . info ( 'A problem occurred while generating the Dockerfile. Please attend to the errors below:' )
194
+ print . info ( chalk . red ( error ) )
193
195
return
194
196
}
195
197
196
198
// Run a command to check if matchstick image already exists
197
199
exec ( 'docker images -q matchstick' , ( error , stdout , stderr ) => {
198
- // Getting the current working folder that will be passed to the
199
- // `docker run` command to be bind mounted.
200
- let current_folder = process . cwd ( ) ;
201
- let testArgs = '' ;
202
-
200
+ // Collect all(if any) flags and options that have to be passed to the matchstick binary
201
+ let testArgs = ''
203
202
if ( coverageOpt ) testArgs = testArgs + ' -c'
204
203
if ( recompileOpt ) testArgs = testArgs + ' -r'
205
204
if ( datasource ) testArgs = testArgs + ' ' + datasource
206
205
207
- let dockerRunOpts = [ 'run' , '-it' , '--rm' , '--mount' , `type=bind,source=${ current_folder } ,target=/matchstick` ] ;
206
+ // Build the `docker run` command options and flags
207
+ let dockerRunOpts = [ 'run' , '-it' , '--rm' , '--mount' , `type=bind,source=${ current_folder } ,target=/matchstick` ]
208
208
209
209
if ( testArgs !== '' ) {
210
210
dockerRunOpts . push ( '-e' )
211
- dockerRunOpts . push ( `ARGS=${ testArgs . trim ( ) } ` ) ;
211
+ dockerRunOpts . push ( `ARGS=${ testArgs . trim ( ) } ` )
212
212
}
213
213
214
214
dockerRunOpts . push ( 'matchstick' )
@@ -220,8 +220,8 @@ function runDocker(datasource, opts, print) {
220
220
if ( stdout === '' || versionOpt || forceOpt ) {
221
221
if ( ( stdout !== '' && versionOpt ) || forceOpt ) {
222
222
exec ( 'docker image rm matchstick' , ( error , stdout , stderr ) => {
223
- print . info ( chalk . bold ( `Removing matchstick image\n${ stdout } ` ) ) ;
224
- } ) ;
223
+ print . info ( chalk . bold ( `Removing matchstick image\n${ stdout } ` ) )
224
+ } )
225
225
}
226
226
// Build a docker image. If the process has executed successfully
227
227
// run a container from that image.
@@ -231,13 +231,13 @@ function runDocker(datasource, opts, print) {
231
231
{ stdio : 'inherit' }
232
232
) . on ( 'close' , code => {
233
233
if ( code === 0 ) {
234
- spawn ( 'docker' , dockerRunOpts , { stdio : 'inherit' } ) ;
234
+ spawn ( 'docker' , dockerRunOpts , { stdio : 'inherit' } )
235
235
}
236
236
} )
237
237
} else {
238
238
print . info ( "Docker image already exists. Skipping `docker build` command." )
239
239
// Run the container from the existing matchstick docker image
240
- spawn ( 'docker' , dockerRunOpts , { stdio : 'inherit' } ) ;
240
+ spawn ( 'docker' , dockerRunOpts , { stdio : 'inherit' } )
241
241
}
242
242
} )
243
243
}
0 commit comments