containr
Version:
Docker Build scripts for npm
194 lines (150 loc) • 4.48 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.release = exports.test = exports.push = exports.tag = exports.build = exports.printBanner = undefined;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _logger = require('./logger');
var _logger2 = _interopRequireDefault(_logger);
var _register = require('./pkg/register');
var _register2 = _interopRequireDefault(_register);
var _docker = require('./docker');
var _docker2 = _interopRequireDefault(_docker);
var _templating = require('./templating');
var _templating2 = _interopRequireDefault(_templating);
var _package = require('../package.json');
var _package2 = _interopRequireDefault(_package);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Containr commands for cli
*
* see README
*/
let defaultRunCommandOptions = {
imagefile: 'Dockerfile',
production: false,
development: false,
client: true,
server: false,
watch: false,
configFile: 'builder.config.js'
};
/**
* Print a banner on application startup
*/
const printBanner = exports.printBanner = () => {
const version = _package2.default.version;
_logger2.default.banner(`containr v${version}`);
};
/**
* Build the current image
*/
const build = exports.build = (buildFile = 'Dockerfile', options = {}) => {
const buildEnv = options.production ? 'production' : 'development';
if (options.verbose) {
_logger2.default.setLevel(_logger.logLevels.debug);
}
_logger2.default.info(`Building ${_register2.default.imageName}:dev from ${buildFile}`);
_logger2.default.debug(`Mode: ${buildEnv}`);
let filePath = null;
if (_templating2.default.isEjs(buildFile)) {
filePath = _templating2.default.renderFile(buildFile, { pkg: _register2.default });
} else {
filePath = _path2.default.resolve(process.cwd(), buildFile);
}
// l.debug(`gitTag: ${pkg.gitTag}`);
const imageHash = _docker2.default.buildImage({
dockerfile: filePath,
version: 'dev',
name: _register2.default.imageName,
verbose: true
});
if (!imageHash.success) {
_logger2.default.error(`Build error: ${imageHash.message}`);
return false;
}
const { hash } = imageHash;
_logger2.default.info(`Finished. => ${hash}`);
return true;
};
/**
*
*/
const tag = exports.tag = (tagVersion = '', options = {}) => {
if (options.verbose) {
_logger2.default.setLevel(_logger.logLevels.debug);
}
const fromTag = `${_register2.default.imageName}:dev`;
// build image if it doesn't exist
build();
const version = tagVersion || _register2.default.version;
const toTag = `${_register2.default.imageName}:${version}`;
const tagExec = _docker2.default.tagContainer({
from: fromTag,
to: toTag,
verbose: options.verbose
});
if (tagExec.success) {
_logger2.default.info(`Tagged as: ${toTag}`);
return true;
}
_logger2.default.error(`${tagExec.message}`);
return false;
};
/**
*
*/
const push = exports.push = (tagVersion = '', options = {}) => {
if (options.verbose) {
_logger2.default.setLevel(_logger.logLevels.debug);
}
let localTags = [];
if (tagVersion) {
localTags = [tagVersion];
} else {
localTags = [_register2.default.version];
}
localTags.forEach(localTag => {
const imageNameTagged = `${_register2.default.imageName}:${localTag}`;
if (!_docker2.default.imageExists(imageNameTagged)) {
tag(localTag);
}
const pushExec = _docker2.default.pushImage({
tag: imageNameTagged,
verbose: options.verbose
});
if (pushExec.success) {
_logger2.default.info(`Pushed: ${imageNameTagged}`);
}
});
};
const test = exports.test = (cmd = '', options = {}) => {
if (options.verbose) {
_logger2.default.setLevel(_logger.logLevels.debug);
}
const imageNameTagged = `${_register2.default.imageName}:${_register2.default.gitTag}`;
if (_docker2.default.imageExists(imageNameTagged)) {
const testExec = _docker2.default.runContainer({
tag: imageNameTagged
});
_logger2.default.info(`Running: ${imageNameTagged}`);
} else {
_logger2.default.warn(`${imageNameTagged} doesn't exist run 'containr build'`);
}
};
/**
*
*/
const release = exports.release = () => {
push('latest', _register2.default.version);
};
exports.default = {
printBanner,
build,
tag,
test,
push,
release
};
//# sourceMappingURL=commands.js.map
;