tsdocker/dist/npmdocker.docker.js

101 lines
7.3 KiB
JavaScript
Raw Normal View History

2016-07-18 18:48:34 +00:00
"use strict";
const plugins = require("./npmdocker.plugins");
2016-07-19 17:21:06 +00:00
const paths = require("./npmdocker.paths");
const snippets = require("./npmdocker.snippets");
let config;
let dockerData = {
imageTag: "npmdocker-temp-image:latest",
containerName: "npmdocker-temp-container",
exitCode: 0
};
2016-07-18 18:48:34 +00:00
/**
* check if docker is available
*/
2016-07-19 17:21:06 +00:00
let checkDocker = () => {
2016-07-18 18:48:34 +00:00
let done = plugins.q.defer();
if (plugins.shelljs.which("docker")) {
2016-07-19 17:21:06 +00:00
plugins.beautylog.ok("Docker found!");
2016-07-18 18:48:34 +00:00
done.resolve();
}
else {
done.reject(new Error("docker not found on this machine"));
}
return done.promise;
};
2016-07-19 17:21:06 +00:00
/**
* builds the Dockerfile according to the config in the project
*/
let buildDockerFile = () => {
let done = plugins.q.defer();
let dockerfile = snippets.dockerfileSnippet({
baseImage: config.baseImage,
command: config.command
});
plugins.beautylog.info(`Base image is: ${config.baseImage}`);
plugins.beautylog.info(`Command is: ${config.command}`);
plugins.smartfile.memory.toFsSync(dockerfile, paths.dockerfile);
plugins.beautylog.ok("Dockerfile created!");
done.resolve();
return done.promise;
};
/**
* builds the Dockerimage from the built Dockerfile
*/
let buildDockerImage = () => {
let done = plugins.q.defer();
plugins.beautylog.log("pulling latest image...");
plugins.shelljs.exec(`docker pull ${config.baseImage}`, {
silent: true
}); // first pull latest version of baseImage
plugins.shelljs.exec(`docker build -f ${paths.dockerfile} -t ${dockerData.imageTag} ${paths.assets}`, {
silent: true
}, () => {
plugins.beautylog.ok("Dockerimage built!");
done.resolve();
});
return done.promise;
};
/**
* creates a container by running the built Dockerimage
*/
let runDockerImage = () => {
let done = plugins.q.defer();
plugins.beautylog.info("Now starting Container!");
dockerData.exitCode = plugins.shelljs.exec(`docker run -v ${paths.cwd}:/workspace --name ${dockerData.containerName} ${dockerData.imageTag}`).code;
done.resolve();
return done.promise;
};
let deleteDockerContainter = () => {
let done = plugins.q.defer();
plugins.shelljs.exec(`docker rm ${dockerData.containerName}`, {
silent: true
});
done.resolve();
plugins.beautylog.ok("removed test container!");
return done.promise;
};
let deleteDockerImage = () => {
let done = plugins.q.defer();
plugins.shelljs.exec(`docker rmi ${dockerData.imageTag}`, {
silent: true
});
done.resolve();
plugins.beautylog.ok("removed test image!");
plugins.beautylog.ok("Cleaned up!");
return done.promise;
};
2016-07-18 22:37:13 +00:00
exports.run = (configArg) => {
let done = plugins.q.defer();
2016-07-19 17:21:06 +00:00
config = configArg;
checkDocker()
.then(buildDockerFile)
.then(buildDockerImage)
.then(runDockerImage)
.then(deleteDockerContainter)
.then(deleteDockerImage)
.then(() => {
done.resolve(configArg);
});
2016-07-18 22:37:13 +00:00
return done.promise;
};
2016-07-19 17:21:06 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLHFCQUFxQixDQUFDLENBQUE7QUFDL0MsTUFBWSxLQUFLLFdBQU0sbUJBQW1CLENBQUMsQ0FBQTtBQUMzQyxNQUFZLFFBQVEsV0FBTSxzQkFBc0IsQ0FBQyxDQUFBO0FBRWpELElBQUksTUFBTSxDQUFDO0FBQ1gsSUFBSSxVQUFVLEdBQUc7SUFDYixRQUFRLEVBQUUsNkJBQTZCO0lBQ3ZDLGFBQWEsRUFBRSwwQkFBMEI7SUFDekMsUUFBUSxFQUFDLENBQUM7Q0FDYixDQUFDO0FBQ0Y7O0dBRUc7QUFDSCxJQUFJLFdBQVcsR0FBRztJQUNkLElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLEN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