truss/docker/cjs/index.js
2019-03-05 19:21:09 -06:00

75 lines
1.7 KiB
JavaScript

'use strict';
// Core.
const fs = require('fs');
// 3rd-party.
const yaml = require('js-yaml');
exports.emitComposeFile = function(services) {
return emitString(emitObject(services));
};
function emitString(object) {
return yaml.safeDump(object);
}
function emitObject(services) {
const composeFile = {
version: '3',
services: {},
};
const cwd = process.cwd();
for (const service of services) {
composeFile.services[service] = emitService(service);
const modulePath = `${cwd}/services/${service}/compose`;
try {
require(modulePath)(composeFile.services[service], composeFile);
}
catch (error) {
if (`Cannot find module '${modulePath}'` !== error.message) {
console.error(error);
process.exit(1);
}
}
}
return composeFile;
}
function emitService(service) {
const definition = {};
if ('production' === process.env.NODE_ENV) {
definition.image = 'docker.i12e.cha0s.io/cha0s6983/truss-production';
}
else {
definition.image = 'docker.i12e.cha0s.io/cha0s6983/truss-dev';
}
if ('production' === process.env.NODE_ENV) {
definition.env_file = [
'.env',
];
}
else {
definition.env_file = [
'.common.env', `.dev.env`, '.env',
];
}
if ('production' === process.env.NODE_ENV) {
definition.volumes = [
`./${service}:/var/node/dist`,
];
}
else {
definition.volumes = [
`./services/${service}:/var/node/src`,
`./dist/dev/${service}:/var/node/dist`,
];
// Add individual lib entries.
const cwd = process.cwd();
const entries = fs.readdirSync(`${cwd}/lib`);
for (const entry of entries) {
definition.volumes.push(`./lib/${entry}:/var/node/lib/${entry}`);
}
}
return definition;
}