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

View File

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

View File

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

View File

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