Skip to content

Commit 63bc04f

Browse files
Replaces fs with toolbox.filesystem. Slight refactoring. Adds some comments
1 parent 219ec24 commit 63bc04f

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/commands/test.js

Lines changed: 26 additions & 26 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 fs = require('fs')
5+
const { filesystem } = require('gluegun/filesystem')
66
const { fixParameters } = require('../command-helpers/gluegun')
77
const semver = require('semver')
88
const { spawn, exec } = require('child_process')
@@ -115,7 +115,7 @@ async function runBinary(datasource, opts, print) {
115115

116116
let binary = new Binary(platform, url, versionOpt || latestVersion)
117117
forceOpt ? await binary.install(true) : await binary.install(false)
118-
let args = new Array();
118+
let args = new Array()
119119

120120
if (coverageOpt) args.push('-c')
121121
if (recompileOpt) args.push('-r')
@@ -158,7 +158,7 @@ function getPlatform(logsOpt) {
158158
throw new Error(`Unsupported platform: ${type} ${arch} ${majorVersion}`)
159159
}
160160

161-
function runDocker(datasource, opts, print) {
161+
async function runDocker(datasource, opts, print) {
162162
let coverageOpt = opts.get("coverage")
163163
let forceOpt = opts.get("force")
164164
let versionOpt = opts.get("version")
@@ -167,48 +167,48 @@ function runDocker(datasource, opts, print) {
167167

168168
// Remove binary-install-raw binaries, because docker has permission issues
169169
// when building the docker images
170-
fs.rmSync("./node_modules/binary-install-raw/bin", { force: true, recursive: true });
170+
await filesystem.remove("./node_modules/binary-install-raw/bin")
171171

172-
let dockerDir = "";
172+
// Get current working directory
173+
let current_folder = await filesystem.cwd()
174+
175+
// Build the Dockerfile location. Defaults to ./tests/.docker if
176+
// a custom testsFolder is not delcared in the subgraph.yaml
177+
let dockerDir = ""
173178

174179
try {
175-
let doc = yaml.load(fs.readFileSync('subgraph.yaml', 'utf8'))
180+
let doc = await yaml.load(filesystem.read('subgraph.yaml', 'utf8'))
176181
testsFolder = doc.testsFolder || './tests'
177182
dockerDir = testsFolder.endsWith('/') ? testsFolder + '.docker' : testsFolder + '/.docker'
178183
} catch (e) {
179-
print.error(e);
184+
print.error(e)
180185
return
181186
}
182187

183-
if (!fs.existsSync(dockerDir)){
184-
fs.mkdirSync(dockerDir, { recursive: true });
185-
}
186-
188+
// Create the Dockerfile
187189
try {
188-
fs.writeFileSync(`${dockerDir}/Dockerfile`, dockerfile(versionOpt, latestVersion))
189-
print.info('Successfully generated Dockerfile.');
190+
await filesystem.write(`${dockerDir}/Dockerfile`, dockerfile(versionOpt, latestVersion))
191+
print.info('Successfully generated Dockerfile.')
190192
} catch (error) {
191-
print.info('A problem occurred while generating the Dockerfile. Please attend to the errors below:');
192-
print.info(chalk.red(error));
193+
print.info('A problem occurred while generating the Dockerfile. Please attend to the errors below:')
194+
print.info(chalk.red(error))
193195
return
194196
}
195197

196198
// Run a command to check if matchstick image already exists
197199
exec('docker images -q matchstick', (error, stdout, stderr) => {
198-
// Getting the current working folder that will be passed to the
199-
// `docker run` command to be bind mounted.
200-
let current_folder = process.cwd();
201-
let testArgs = '';
202-
200+
// Collect all(if any) flags and options that have to be passed to the matchstick binary
201+
let testArgs = ''
203202
if (coverageOpt) testArgs = testArgs + ' -c'
204203
if (recompileOpt) testArgs = testArgs + ' -r'
205204
if (datasource) testArgs = testArgs + ' ' + datasource
206205

207-
let dockerRunOpts = ['run', '-it', '--rm', '--mount', `type=bind,source=${current_folder},target=/matchstick`];
206+
// Build the `docker run` command options and flags
207+
let dockerRunOpts = ['run', '-it', '--rm', '--mount', `type=bind,source=${current_folder},target=/matchstick`]
208208

209209
if(testArgs !== '') {
210210
dockerRunOpts.push('-e')
211-
dockerRunOpts.push(`ARGS=${testArgs.trim()}`);
211+
dockerRunOpts.push(`ARGS=${testArgs.trim()}`)
212212
}
213213

214214
dockerRunOpts.push('matchstick')
@@ -220,8 +220,8 @@ function runDocker(datasource, opts, print) {
220220
if(stdout === '' || versionOpt || forceOpt) {
221221
if ((stdout !== '' && versionOpt) || forceOpt) {
222222
exec('docker image rm matchstick', (error, stdout, stderr) => {
223-
print.info(chalk.bold(`Removing matchstick image\n${stdout}`));
224-
});
223+
print.info(chalk.bold(`Removing matchstick image\n${stdout}`))
224+
})
225225
}
226226
// Build a docker image. If the process has executed successfully
227227
// run a container from that image.
@@ -231,13 +231,13 @@ function runDocker(datasource, opts, print) {
231231
{ stdio: 'inherit' }
232232
).on('close', code => {
233233
if (code === 0) {
234-
spawn('docker', dockerRunOpts, { stdio: 'inherit' });
234+
spawn('docker', dockerRunOpts, { stdio: 'inherit' })
235235
}
236236
})
237237
} else {
238238
print.info("Docker image already exists. Skipping `docker build` command.")
239239
// Run the container from the existing matchstick docker image
240-
spawn('docker', dockerRunOpts, { stdio: 'inherit' });
240+
spawn('docker', dockerRunOpts, { stdio: 'inherit' })
241241
}
242242
})
243243
}

0 commit comments

Comments
 (0)