@@ -2,8 +2,9 @@ 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 { filesystem, print , patching } = require ( 'gluegun' )
5
+ const { filesystem, patching , print } = require ( 'gluegun' )
6
6
const { fixParameters } = require ( '../command-helpers/gluegun' )
7
+ const path = require ( 'path' )
7
8
const semver = require ( 'semver' )
8
9
const { spawn, exec } = require ( 'child_process' )
9
10
const yaml = require ( 'js-yaml' )
@@ -179,10 +180,9 @@ async function runDocker(datasource, opts) {
179
180
let config = await yaml . load ( filesystem . read ( 'matchstick.yaml' , 'utf8' ) )
180
181
181
182
// Check if matchstick.yaml is not empty
182
- if ( config != undefined ) {
183
+ if ( config != null ) {
183
184
// If a custom tests folder is declared update dockerfilePath
184
- testsFolder = config . testsFolder || './tests'
185
- dockerfilePath = testsFolder . endsWith ( '/' ) ? testsFolder + '.docker/Dockerfile' : testsFolder + '/.docker/Dockerfile'
185
+ dockerfilePath = path . join ( config . testsFolder || 'tests' , '.docker/Dockerfile' )
186
186
}
187
187
} catch ( error ) {
188
188
print . info ( 'A problem occurred while reading matchstick.yaml. Please attend to the errors below:' )
@@ -197,8 +197,6 @@ async function runDocker(datasource, opts) {
197
197
// Generate the Dockerfile only if it doesn't exists,
198
198
// version flag and/or force flag is passed.
199
199
if ( ! dockerfileExists || versionOpt || forceOpt ) {
200
- print . info ( "Generating Dockerfile..." )
201
-
202
200
await dockerfile ( dockerfilePath , versionOpt , latestVersion )
203
201
}
204
202
@@ -222,10 +220,10 @@ async function runDocker(datasource, opts) {
222
220
223
221
// If a matchstick image does not exists, the command returns an empty string,
224
222
// else it'll return the image ID. Skip `docker build` if an image already exists
225
- // If `-v/--version` is specified, delete current image(if any) and rebuild.
223
+ // Delete current image(if any) and rebuild.
226
224
// Use spawn() and {stdio: 'inherit'} so we can see the logs in real time.
227
225
if ( ! dockerfileExists || stdout === '' || versionOpt || forceOpt ) {
228
- if ( ( stdout !== '' && versionOpt ) || forceOpt ) {
226
+ if ( stdout !== '' ) {
229
227
exec ( 'docker image rm matchstick' , ( error , stdout , stderr ) => {
230
228
print . info ( chalk . bold ( `Removing matchstick image\n${ stdout } ` ) )
231
229
} )
@@ -252,30 +250,29 @@ async function runDocker(datasource, opts) {
252
250
// Downloads Dockerfile template from the demo-subgraph repo
253
251
// Replaces the placeholders with their respective values
254
252
async function dockerfile ( dockerfilePath , versionOpt , latestVersion ) {
255
- let content = await fetch ( 'https://raw.githubusercontent.com/LimeChain/demo-subgraph/main/Dockerfile' )
256
- . then ( ( response ) => {
257
- if ( response . ok ) {
258
- return response . text ( )
259
- } else {
260
- print . error ( 'A problem occurred while downloading the Dockerfile template:' )
261
- print . error ( `Status Code: ${ response . status } , with error: ${ response . statusText } ` )
262
- process . exit ( 1 )
263
- }
264
- } )
253
+ let spinner = print . spin ( "Generating Dockerfile..." )
265
254
266
-
267
- // Create the Dockerfile
268
255
try {
256
+ // Fetch the Dockerfile template content from the demo-subgraph repo
257
+ let content = await fetch ( 'https://raw.githubusercontent.com/LimeChain/demo-subgraph/main/Dockerfile' )
258
+ . then ( ( response ) => {
259
+ if ( response . ok ) {
260
+ return response . text ( )
261
+ } else {
262
+ throw new Error ( `Status Code: ${ response . status } , with error: ${ response . statusText } ` ) ;
263
+ }
264
+ } )
265
+
266
+ // Write the Dockerfile
269
267
await filesystem . write ( dockerfilePath , content )
270
- print . info ( 'Successfully generated Dockerfile.' )
268
+
269
+ // Replaces the version placeholders
270
+ await patching . replace ( dockerfilePath , '<MATCHSTICK_VERSION>' , versionOpt || latestVersion )
271
+
271
272
} catch ( error ) {
272
- print . error ( 'A problem occurred while generating the Dockerfile. Please attend to the errors below:' )
273
- print . error ( error . message )
273
+ spinner . fail ( `A problem occurred while generating the Dockerfile. Please attend to the errors below:\n ${ error . message } ` )
274
274
process . exit ( 1 )
275
275
}
276
276
277
- // Replaces the version placeholders
278
- await patching . update ( dockerfilePath , data => {
279
- return data . replace ( '<MATCHSTICK_VERSION>' , versionOpt || latestVersion )
280
- } )
277
+ spinner . succeed ( 'Successfully generated Dockerfile.' )
281
278
}
0 commit comments