-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·69 lines (51 loc) · 1.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const exec = require('child_process').exec;
const dockerize = require('./lib/dockerize');
const commandBuilder = require('./lib/command_builder');
const directory = process.cwd();
const dockerfileLocation = `${directory}/Dockerfile`;
const executeCommand = (command, directory, errorMessage) =>
new Promise((resolve, reject) => {
const proc = exec(command, {cwd: directory});
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on('exit', code => {
if (code === 0) {
return resolve();
}
const error = new Error(errorMessage);
error.exitCode = code;
reject(error);
});
});
dockerize.dockerfile(directory)
.then(dockerfile => new Promise((resolve, reject) => {
fs.writeFile(dockerfileLocation, dockerfile, err => {
if (err) {
console.error(`Unable to write dockerfile to ${dockerfileLocation}.`);
reject(err);
}
console.log('Created Dockerfile');
resolve();
});
}))
.then(() => {
const command = commandBuilder.dockerBuild(directory);
console.log(`Building docker image with command: ${command}`);
return executeCommand(command, directory, 'docker build command failed');
})
.then(() => {
console.log('Built docker image');
const command = commandBuilder.dockerRun(directory);
console.log(`Running docker image with command: ${command}`);
return executeCommand(command, directory, 'docker run command failed');
})
.then(() => console.log('Exited container'))
.catch(err => {
console.error(err);
if (err.exitCode) {
console.log(`Exit code: ${err.exitCode}`);
}
});