Skip to content

Commit 206981d

Browse files
committed
Updates
1 parent 36e4f25 commit 206981d

File tree

1 file changed

+60
-132
lines changed

1 file changed

+60
-132
lines changed

src/commands/test.js

Lines changed: 60 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
const { Binary } = require('binary-install-raw')
21
const os = require('os')
32
const chalk = require('chalk')
43
const fetch = require('node-fetch')
4+
const semver = require('semver')
55
const { exec } = require('child_process')
66
const fs = require('fs')
7-
const semver = require('semver')
87

98
const HELP = `
109
${chalk.bold('graph test')} ${chalk.dim('[options]')} ${chalk.bold('<datasource>')}
1110
1211
${chalk.dim('Options:')}
13-
14-
-f --force Overwrite folder + file when downloading
1512
-h, --help Show usage information
16-
-l, --logs Logs to the console information about the OS, CPU model and download url (debugging purposes)
1713
-v, --version <tag> Choose the version of the rust binary that you want to be downloaded/used
18-
-d, --docker Launches Matchstick in a docker container
19-
-c, --coverage (Docker) Runs in coverage mode
14+
-c, --flags <flags>
2015
`
2116

2217
module.exports = {
@@ -26,168 +21,101 @@ module.exports = {
2621
let { print } = toolbox
2722

2823
// Read CLI parameters
29-
let { f, force, h, help, l, logs, v, version, c, coverage, d, docker } = toolbox.parameters.options
24+
let { h, help, v, version, c, coverage, l, lib } = toolbox.parameters.options
3025
let datasource = toolbox.parameters.first
3126

3227
// Support both long and short option variants
33-
force = force || f
3428
help = help || h
35-
logs = logs || l
3629
version = version || v
3730
coverage = coverage || c
38-
docker = docker || d
3931

4032
// Show help text if requested
4133
if (help) {
4234
print.info(HELP)
4335
return
4436
}
4537

46-
if (docker) {
47-
let url = `https://github.com/LimeChain/matchstick/releases/download/${version || '0.2.0a'}/binary-linux-20`
48-
49-
if (version) {
50-
await fetch(url)
51-
.then(response => {
52-
if (response.status === 404) {
53-
print.info(`Error: Invalid Matchstick version '${version}'`)
54-
process.exit(1)
55-
}
56-
})
57-
}
38+
if(version) {
39+
let url = `https://github.com/LimeChain/matchstick/releases/download/${version || "0.2.2a"}/binary-linux-20`;
40+
41+
await fetch(url)
42+
.then(response => {
43+
if (response.status === 404){
44+
print.info(`Error: Invalid Matchstick version '${version}'`);
45+
process.exit(1);
46+
}
47+
})
48+
}
5849

59-
// TODO: Move these in separate file (in a function maybe)
60-
let contents = `FROM ubuntu:20.04
50+
// TODO: Move these in separate file (in a function maybe)
51+
let contents = `FROM ubuntu:20.04
6152
ENV ARGS=""
6253
RUN apt update
6354
RUN apt install -y nodejs
6455
RUN apt install -y npm
65-
RUN apt install -y cmake
66-
RUN apt install -y git
6756
COPY ./ ./
68-
RUN mkdir -p ./tests/.tools
69-
WORKDIR ./tests/.tools
70-
RUN git clone --recursive https://github.com/WebAssembly/wabt
71-
WORKDIR wabt
72-
RUN git submodule update --init
73-
RUN mkdir -p build
74-
WORKDIR build
75-
RUN cmake ..
76-
RUN cmake --build .
77-
WORKDIR /
7857
RUN npm run codegen
7958
RUN npm run build
8059
RUN apt install -y postgresql
8160
RUN apt install -y curl
82-
RUN curl -OL ${url}
61+
RUN apt-get update && apt-get -y install cmake protobuf-compiler
62+
RUN curl -OL https://github.com/LimeChain/matchstick/releases/download/${version || "0.2.1a"}/binary-linux-20
8363
RUN mv binary-linux-20 matchstick
64+
RUN chmod a+x matchstick
8465
CMD ./matchstick \${ARGS}`
8566

86-
let dir = 'tests/.docker'
67+
let dir = 'tests/.docker';
68+
69+
if (!fs.existsSync(dir)){
70+
fs.mkdirSync(dir, { recursive: true });
71+
}
8772

88-
if (!fs.existsSync(dir)) {
89-
fs.mkdirSync(dir, { recursive: true })
73+
fs.writeFileSync('tests/.docker/Dockerfile', contents, (err) => {
74+
if (err) {
75+
print.info('A problem occurred while generating the Dockerfile. Please attend to the errors below.');
76+
print.info(err);
9077
}
78+
else {
79+
print.info('Successfully generated Dockerfile.')
80+
}
81+
})
9182

92-
fs.writeFileSync('tests/.docker/Dockerfile', contents, (err) => {
93-
if (err) {
94-
print.info('A problem occurred while generating the Dockerfile. Please attend to the errors below.')
95-
print.info(err)
96-
} else {
97-
print.info('Successfully generated Dockerfile.')
98-
}
99-
})
83+
exec(`docker build -f tests/.docker/Dockerfile -t matchstick . `, (error, stdout, stderr) => {
84+
print.info('Building Matchstick image...');
10085

101-
exec(`docker build -f tests/.docker/Dockerfile -t matchstick . `, (error, stdout, stderr) => {
102-
print.info('Building Matchstick image...')
86+
if (error) {
87+
print.info('A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.');
88+
print.info(`error: ${error.message}`)
89+
}
90+
if (stderr) {
91+
print.info('A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.');
92+
print.info(`stderr: ${stderr}`)
93+
}
94+
95+
let runCommand = `docker run -e ARGS="${datasource || ""}${coverage ? "-c" : ""}" --rm matchstick`;
96+
97+
console.log("runCommand output");
98+
console.log(runCommand);
99+
print.info("runCommand output");
100+
print.info(runCommand);
101+
102+
exec(runCommand, (error, stdout, stderr) => {
103+
print.info('Running Matchstick image...');
103104

104105
if (error) {
105-
print.info('A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.')
106+
print.info('A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.');
106107
print.info(`error: ${error.message}`)
108+
process.exit(1);
107109
}
108110
if (stderr) {
109-
print.info('A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.')
111+
print.info('A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.');
110112
print.info(`stderr: ${stderr}`)
113+
process.exit(1);
111114
}
112-
113-
let runCommand = `docker run -e ARGS="${datasource || ''}${coverage ? '-c' : ''}" --rm matchstick`
114-
115-
console.log('runCommand output')
116-
console.log(runCommand)
117-
print.info('runCommand output')
118-
print.info(runCommand)
119-
120-
exec(runCommand, (error, stdout, stderr) => {
121-
print.info('Running Matchstick image...')
122-
123-
if (error) {
124-
print.info('A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.')
125-
print.info(`error: ${error.message}`)
126-
process.exit(1)
127-
}
128-
if (stderr) {
129-
print.info('A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.')
130-
print.info(`stderr: ${stderr}`)
131-
process.exit(1)
132-
}
133-
print.info(stdout)
134-
process.exit()
135-
})
115+
print.info(stdout)
116+
process.exit();
136117
})
118+
})
137119

138-
return
139-
}
140-
141-
const platform = getPlatform(logs)
142-
if (!version) {
143-
let result = await fetch('https://api.github.com/repos/LimeChain/matchstick/releases/latest')
144-
let json = await result.json()
145-
version = json.tag_name
146-
}
147-
148-
const url = `https://github.com/LimeChain/matchstick/releases/download/${version}/${platform}`
149-
150-
if (logs) {
151-
console.log(`Download link: ${url}`)
152-
}
153-
154-
let binary = new Binary(platform, url, version)
155-
await binary.install(force)
156-
datasource ? binary.run(datasource) : binary.run()
157120
},
158-
}
159-
160-
function getPlatform(logs) {
161-
const type = os.type()
162-
const arch = os.arch()
163-
const release = os.release()
164-
const cpuCore = os.cpus()[0]
165-
const majorVersion = semver.major(release)
166-
const isM1 = cpuCore.model.includes('Apple M1')
167-
168-
if (logs) {
169-
console.log(`OS type: ${type}\nOS arch: ${arch}\nOS release: ${release}\nOS major version: ${majorVersion}\nCPU model: ${cpuCore.model}`)
170-
}
171-
172-
if (arch === 'x64' || (arch === 'arm64' && isM1)) {
173-
if (type === 'Darwin') {
174-
if (majorVersion === 19) {
175-
return 'binary-macos-10.15'
176-
} else if (majorVersion === 18) {
177-
return 'binary-macos-10.14'
178-
} else if (isM1) {
179-
return 'binary-macos-11-m1'
180-
}
181-
return 'binary-macos-11'
182-
} else if (type === 'Linux') {
183-
if (majorVersion === 18) {
184-
return 'binary-linux-18'
185-
}
186-
return 'binary-linux-20'
187-
} else if (type === 'Windows_NT') {
188-
return 'binary-windows'
189-
}
190-
}
191-
192-
throw new Error(`Unsupported platform: ${type} ${arch} ${majorVersion}`)
193-
}
121+
}

0 commit comments

Comments
 (0)