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