const path = require('path'); const {spawnSync} = require('child_process'); const fs = require('fs'); const dotenv = require('dotenv'); const {emitObject, emitString} = require('./compose'); ['.common.env', '.prod.env', '.env'].forEach((filename) => { dotenv.config({path: path.join(__dirname, filename)}); }); const cwd = process.cwd(); const distPath = path.join(cwd, 'dist', 'production'); const services = process.env.SERVICES.split(','); services.push('gateway'); const composeFile = emitString(emitObject(services)); spawnSync('mkdir', ['-p', 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, '.prod.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); spawnSync('mkdir', ['-p', serviceDistPath]); spawnSync('docker', [ 'run', '--env-file', './.common.env', '--env-file', './.prod.env', '--env-file', './.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', }); spawnSync('rm', [ '-rf', path.join(serviceDistPath, 'node_modules'), path.join(serviceDistPath, 'package.json'), path.join(serviceDistPath, 'yarn.lock'), ]); }