truss/docker/task/build.js

76 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2018-12-23 07:56:44 -06:00
const dotenv = require('dotenv');
const path = require('path');
const cwd = process.cwd();
['.common.env', '.production.env', '.env'].forEach((filename) => {
dotenv.config({path: path.join(cwd, filename)});
});
const {spawnSync} = require('child_process');
const fs = require('fs');
const mkdirp = require('mkdirp');
const {servicesList} = require('@truss/core');
const {emitComposeFile} = require('@truss/docker');
const distPath = path.join(cwd, 'dist', 'production');
const services = servicesList();
const composeFile = emitComposeFile(services);
mkdirp.sync(distPath);
fs.writeFileSync(
path.join(distPath, 'docker-compose.yml'), composeFile
);
fs.copyFileSync(
path.join(cwd, '.common.env'),
path.join(distPath, '.env'),
);
fs.appendFileSync(
path.join(distPath, '.env'),
fs.readFileSync(path.join(cwd, '.production.env')),
);
fs.appendFileSync(
path.join(distPath, '.env'),
fs.readFileSync(path.join(cwd, '.env')),
);
for (const service of services) {
const servicePath = path.join(cwd, 'services', service);
const serviceDistPath = path.join(distPath, service);
mkdirp.sync(serviceDistPath);
spawnSync('docker', [
'run',
'--env-file', `${cwd}/.common.env`,
'--env-file', `${cwd}/.production.env`,
'--env-file', `${cwd}/.env`,
'-e', 'DEBUG=truss:*',
'-e', 'DEBUG_COLORS=1',
'-e', 'DEBUG_HIDE_DATE=1',
'-e', 'NODE_PRESERVE_SYMLINKS=1',
'-v', `${path.join(cwd, 'lib')}:/var/node/lib:ro`,
'-v', `${servicePath}:/var/node/src:ro`,
'-v', `${serviceDistPath}:/var/node/dist`,
'docker.i12e.cha0s.io/cha0s6983/truss-dev',
'yarn', 'run', 'build',
], {
stdio: 'inherit',
});
require('rimraf').sync(
path.join(serviceDistPath, '{node_modules,package.json,yarn.lock}')
)
}