Skip to content

Commit 33e172a

Browse files
Fetch testsFolder from matchstick.yaml. Fetch Dockerfile template from demo-subgraph
1 parent e7e0448 commit 33e172a

File tree

1 file changed

+48
-55
lines changed

1 file changed

+48
-55
lines changed

src/commands/test.js

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { Binary } = require('binary-install-raw')
22
const os = require('os')
33
const chalk = require('chalk')
44
const fetch = require('node-fetch')
5-
const { filesystem, print } = require('gluegun')
5+
const { filesystem, print, patching } = require('gluegun')
66
const { fixParameters } = require('../command-helpers/gluegun')
77
const semver = require('semver')
88
const { spawn, exec } = require('child_process')
@@ -169,27 +169,37 @@ async function runDocker(datasource, opts) {
169169
// Get current working directory
170170
let current_folder = await filesystem.cwd()
171171

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"
175174

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+
}
183192
}
184193

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)
193203
}
194204

195205
// Run a command to check if matchstick image already exists
@@ -214,7 +224,7 @@ async function runDocker(datasource, opts) {
214224
// else it'll return the image ID. Skip `docker build` if an image already exists
215225
// If `-v/--version` is specified, delete current image(if any) and rebuild.
216226
// 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) {
218228
if ((stdout !== '' && versionOpt) || forceOpt) {
219229
exec('docker image rm matchstick', (error, stdout, stderr) => {
220230
print.info(chalk.bold(`Removing matchstick image\n${stdout}`))
@@ -224,7 +234,7 @@ async function runDocker(datasource, opts) {
224234
// run a container from that image.
225235
spawn(
226236
'docker',
227-
['build', '-f', `${dockerDir}/Dockerfile`, '-t', 'matchstick', '.'],
237+
['build', '-f', dockerfilePath, '-t', 'matchstick', '.'],
228238
{ stdio: 'inherit' }
229239
).on('close', code => {
230240
if (code === 0) {
@@ -239,40 +249,23 @@ async function runDocker(datasource, opts) {
239249
})
240250
}
241251

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()
246257

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+
}
276267

277-
CMD ../binary-linux-20 \${ARGS}`
268+
await patching.update(dockerfilePath, data => {
269+
return data.replace('<MATCHSTICK_VERSION>', versionOpt || latestVersion)
270+
})
278271
}

0 commit comments

Comments
 (0)