fix: ensure gracefully docker start and stop (#96)

Remove unnecessary `execa` dependency
Add `ACCEPT_EULA` in `serve` command with `-a` alias
This commit is contained in:
Christoffer Åström
2019-08-28 13:03:50 +02:00
committed by GitHub
parent 4dabc24d5d
commit 3ca17870f0
4 changed files with 79 additions and 78 deletions

View File

@@ -4,26 +4,32 @@ module.exports = {
command: 'serve',
desc: 'Dev server',
builder(yargs) {
yargs.option('entry', {
yargs
.option('entry', {
type: 'string',
description: 'File entrypoint',
});
yargs.option('build', {
})
.option('build', {
type: 'boolean',
default: true,
});
yargs.option('host', {
})
.option('host', {
type: 'string',
});
yargs.option('port', {
})
.option('port', {
type: 'number',
});
yargs.option('enigma.host', {
})
.option('enigma.host', {
type: 'string',
});
yargs.option('enigma.port', {
})
.option('enigma.port', {
type: 'port',
default: 9076,
})
.option('ACCEPT_EULA', {
type: 'boolean',
alias: 'a',
default: false,
}).argv;
},
handler(argv) {

View File

@@ -1,55 +1,51 @@
const path = require('path');
const execa = require('execa');
const { spawn } = require('child_process');
/* eslint no-use-before-define:0 */
const startEngine = () => {
if (process.env.ACCEPT_EULA == null || process.env.ACCEPT_EULA.toLowerCase() !== 'yes') {
const cwd = path.resolve(__dirname, '../');
const execCmd = (cmd, cmdArgs = [], opts) => {
return new Promise((resolve, reject) => {
const child = spawn(cmd, cmdArgs, opts);
const res = {
exitCode: null,
out: '',
err: '',
};
child.on('error', reject);
child.stdout.on('data', chunk => {
res.out += chunk.toString();
});
child.stderr.on('data', chunk => {
res.err += chunk.toString();
});
child.on('exit', exitCode => {
res.exitCode = exitCode;
if (exitCode === 0) {
resolve(res);
} else {
reject(res);
}
});
});
};
const useEngine = ({ ACCEPT_EULA = false }) => {
if (ACCEPT_EULA !== true) {
throw new Error('Need to accept EULA in order to start engine container');
}
console.log('Starting engine container ...');
return new Promise((resolve, reject) => {
const c = execa.command('cross-env ACCEPT_EULA=yes docker-compose up -d --build', {
cwd: path.resolve(__dirname, '../'),
stdio: 'inherit',
shell: true,
console.error('Starting engine container...');
process.env.ACCEPT_EULA = 'yes';
const stopEngine = async () => {
console.error('Stopping engine container...');
try {
await execCmd('docker-compose', ['down', '--remove-orphans'], {
cwd,
});
const ping = setInterval(() => {
const { stdout } = execa.command('docker ps -q -f name=engine -f status=running');
if (stdout) {
console.log('... engine container running');
clear();
resolve();
} catch (err) {
console.error(err);
}
}, 1000);
const timeout = setTimeout(() => {
clear();
reject();
}, 60000);
function clear() {
clearInterval(ping);
clearTimeout(timeout);
}
c.on('exit', code => {
if (code !== 0) {
clear();
reject();
}
});
});
};
return execCmd('docker-compose', ['up', '-d', '--build'], { cwd }).then(() => stopEngine);
};
const stopEngine = () => {
execa.shellSync('docker-compose down', {
cwd: path.resolve(__dirname, '../'),
stdio: 'inherit',
});
};
module.exports = {
startEngine,
stopEngine,
};
module.exports = useEngine;

View File

@@ -6,11 +6,12 @@ const { watch } = require('@nebula.js/cli-build');
const webpackServe = require('./webpack.serve.js');
const { startEngine, stopEngine } = require('./engine');
const useEngine = require('./engine');
module.exports = async argv => {
if (process.env.ACCEPT_EULA === 'yes') {
await startEngine();
let stopEngine = () => {};
if (argv.ACCEPT_EULA) {
stopEngine = await useEngine(argv);
}
const port = argv.port || (await portfinder.getPortPromise());
const host = argv.host || 'localhost';
@@ -57,10 +58,8 @@ module.exports = async argv => {
watcher,
});
const close = () => {
if (process.env.ACCEPT_EULA === 'yes') {
stopEngine();
}
const close = async () => {
await stopEngine();
if (watcher) {
watcher.close();
}
@@ -71,7 +70,8 @@ module.exports = async argv => {
process.on(signal, close);
});
return { //eslint-disable-line
// eslint-disable-next-line consistent-return
return {
url: server.url,
close,
};

View File

@@ -29,7 +29,6 @@
"@nebula.js/cli-build": "0.1.0-alpha.18",
"chalk": "^2.4.2",
"cross-env": "^5.2.0",
"execa": "^2.0.4",
"html-webpack-plugin": "^3.2.0",
"portfinder": "^1.0.23",
"source-map-loader": "^0.2.4",