Skip to content

Commit d7b312e

Browse files
Address comments
1 parent b7bc5eb commit d7b312e

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

src/commands/test.js

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ 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, patching } = require('gluegun')
5+
const { filesystem, patching, print } = require('gluegun')
66
const { fixParameters } = require('../command-helpers/gluegun')
7+
const path = require('path')
78
const semver = require('semver')
89
const { spawn, exec } = require('child_process')
910
const yaml = require('js-yaml')
@@ -179,10 +180,9 @@ async function runDocker(datasource, opts) {
179180
let config = await yaml.load(filesystem.read('matchstick.yaml', 'utf8'))
180181

181182
// Check if matchstick.yaml is not empty
182-
if(config != undefined) {
183+
if(config != null) {
183184
// 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')
186186
}
187187
} catch (error) {
188188
print.info('A problem occurred while reading matchstick.yaml. Please attend to the errors below:')
@@ -197,8 +197,6 @@ async function runDocker(datasource, opts) {
197197
// Generate the Dockerfile only if it doesn't exists,
198198
// version flag and/or force flag is passed.
199199
if(!dockerfileExists || versionOpt || forceOpt) {
200-
print.info("Generating Dockerfile...")
201-
202200
await dockerfile(dockerfilePath, versionOpt, latestVersion)
203201
}
204202

@@ -222,10 +220,10 @@ async function runDocker(datasource, opts) {
222220

223221
// If a matchstick image does not exists, the command returns an empty string,
224222
// 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.
226224
// Use spawn() and {stdio: 'inherit'} so we can see the logs in real time.
227225
if(!dockerfileExists || stdout === '' || versionOpt || forceOpt) {
228-
if ((stdout !== '' && versionOpt) || forceOpt) {
226+
if (stdout !== '') {
229227
exec('docker image rm matchstick', (error, stdout, stderr) => {
230228
print.info(chalk.bold(`Removing matchstick image\n${stdout}`))
231229
})
@@ -252,30 +250,29 @@ async function runDocker(datasource, opts) {
252250
// Downloads Dockerfile template from the demo-subgraph repo
253251
// Replaces the placeholders with their respective values
254252
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...")
265254

266-
267-
// Create the Dockerfile
268255
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
269267
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+
271272
} 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}`)
274274
process.exit(1)
275275
}
276276

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.')
281278
}

0 commit comments

Comments
 (0)