@@ -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 } = 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' )
@@ -169,27 +170,34 @@ async function runDocker(datasource, opts) {
169
170
// Get current working directory
170
171
let current_folder = await filesystem . cwd ( )
171
172
172
- // Build the Dockerfile location. Defaults to ./tests/.docker if
173
- // a custom testsFolder is not declared in the subgraph.yaml
174
- let dockerDir = ""
173
+ // Declate dockerfilePath with default location
174
+ let dockerfilePath = "./tests/.docker/Dockerfile"
175
175
176
- try {
177
- let doc = await yaml . load ( filesystem . read ( 'subgraph.yaml' , 'utf8' ) )
178
- testsFolder = doc . testsFolder || './tests'
179
- dockerDir = testsFolder . endsWith ( '/' ) ? testsFolder + '.docker' : testsFolder + '/.docker'
180
- } catch ( error ) {
181
- print . error ( error . message )
182
- return
176
+ // Check if matchstick.yaml config exists
177
+ if ( filesystem . exists ( 'matchstick.yaml' ) ) {
178
+ try {
179
+ // Load the config
180
+ let config = await yaml . load ( filesystem . read ( 'matchstick.yaml' , 'utf8' ) )
181
+
182
+ // Check if matchstick.yaml is not empty
183
+ if ( config != null ) {
184
+ // If a custom tests folder is declared update dockerfilePath
185
+ dockerfilePath = path . join ( config . testsFolder || 'tests' , '.docker/Dockerfile' )
186
+ }
187
+ } catch ( error ) {
188
+ print . info ( 'A problem occurred while reading matchstick.yaml. Please attend to the errors below:' )
189
+ print . error ( error . message )
190
+ process . exit ( 1 )
191
+ }
183
192
}
184
193
185
- // Create the Dockerfile
186
- try {
187
- await filesystem . write ( `${ dockerDir } /Dockerfile` , dockerfile ( versionOpt , latestVersion ) )
188
- print . info ( 'Successfully generated Dockerfile.' )
189
- } catch ( error ) {
190
- print . info ( 'A problem occurred while generating the Dockerfile. Please attend to the errors below:' )
191
- print . error ( error . message )
192
- return
194
+ // Check if the Dockerfil already exists
195
+ let dockerfileExists = filesystem . exists ( dockerfilePath )
196
+
197
+ // Generate the Dockerfile only if it doesn't exists,
198
+ // version flag and/or force flag is passed.
199
+ if ( ! dockerfileExists || versionOpt || forceOpt ) {
200
+ await dockerfile ( dockerfilePath , versionOpt , latestVersion )
193
201
}
194
202
195
203
// Run a command to check if matchstick image already exists
@@ -212,10 +220,10 @@ async function runDocker(datasource, opts) {
212
220
213
221
// If a matchstick image does not exists, the command returns an empty string,
214
222
// else it'll return the image ID. Skip `docker build` if an image already exists
215
- // If `-v/--version` is specified, delete current image(if any) and rebuild.
223
+ // Delete current image(if any) and rebuild.
216
224
// Use spawn() and {stdio: 'inherit'} so we can see the logs in real time.
217
- if ( stdout === '' || versionOpt || forceOpt ) {
218
- if ( ( stdout !== '' && versionOpt ) || forceOpt ) {
225
+ if ( ! dockerfileExists || stdout === '' || versionOpt || forceOpt ) {
226
+ if ( stdout !== '' ) {
219
227
exec ( 'docker image rm matchstick' , ( error , stdout , stderr ) => {
220
228
print . info ( chalk . bold ( `Removing matchstick image\n${ stdout } ` ) )
221
229
} )
@@ -224,7 +232,7 @@ async function runDocker(datasource, opts) {
224
232
// run a container from that image.
225
233
spawn (
226
234
'docker' ,
227
- [ 'build' , '--no-cache' , '- f', ` ${ dockerDir } /Dockerfile` , '-t' , 'matchstick' , '.' ] ,
235
+ [ 'build' , '-f' , dockerfilePath , '-t' , 'matchstick' , '.' ] ,
228
236
{ stdio : 'inherit' }
229
237
) . on ( 'close' , code => {
230
238
if ( code === 0 ) {
@@ -239,37 +247,32 @@ async function runDocker(datasource, opts) {
239
247
} )
240
248
}
241
249
242
- // TODO: Move these in separate file (in a function maybe)
243
- function dockerfile ( versionOpt , latestVersion ) {
244
- return `
245
- FROM ubuntu:20.04
246
- ENV ARGS=""
247
-
248
- # Install necessary packages
249
- RUN apt update
250
- RUN apt install -y nodejs
251
- RUN apt install -y npm
252
- RUN apt install -y git
253
- RUN apt install -y postgresql
254
- RUN apt install -y curl
255
- RUN apt install -y cmake
256
- RUN npm install -g @graphprotocol/graph-cli
250
+ // Downloads Dockerfile template from the demo-subgraph repo
251
+ // Replaces the placeholders with their respective values
252
+ async function dockerfile ( dockerfilePath , versionOpt , latestVersion ) {
253
+ let spinner = print . spin ( "Generating Dockerfile..." )
257
254
258
- # Download the latest linux binary
259
- RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/${ versionOpt || latestVersion } /binary-linux-20
260
-
261
- # Make it executable
262
- RUN chmod a+x binary-linux-20
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
+ } )
263
265
264
- # Create a matchstick dir where the host will be copied
265
- RUN mkdir matchstick
266
- WORKDIR matchstick
266
+ // Write the Dockerfile
267
+ await filesystem . write ( dockerfilePath , content )
267
268
268
- # Copy host to /matchstick
269
- COPY ../ .
269
+ // Replaces the version placeholders
270
+ await patching . replace ( dockerfilePath , '<MATCHSTICK_VERSION>' , versionOpt || latestVersion )
270
271
271
- RUN graph codegen
272
- RUN graph build
272
+ } catch ( error ) {
273
+ spinner . fail ( `A problem occurred while generating the Dockerfile. Please attend to the errors below:\n ${ error . message } ` )
274
+ process . exit ( 1 )
275
+ }
273
276
274
- CMD ../binary-linux-20 \${ARGS}`
277
+ spinner . succeed ( 'Successfully generated Dockerfile.' )
275
278
}
0 commit comments