refactor: provides a callback listener to verdaccio bootstrap

This commit is contained in:
Juan Picado @jotadeveloper 2017-11-29 08:33:57 +01:00 committed by juanpicado
parent 9b118a2dfb
commit 66e0d4a017
2 changed files with 42 additions and 38 deletions

57
src/lib/bootstrap.js vendored
View File

@ -23,7 +23,7 @@ const constants = require('constants');
- localhost:5557
@return {Array}
*/
function get_listen_addresses(argListen, configListen) {
function getListListenAddresses(argListen, configListen) {
// command line || config file || default
let addresses;
if (argListen) {
@ -59,15 +59,18 @@ function get_listen_addresses(argListen, configListen) {
* @param {String} pkgVersion
* @param {String} pkgName
*/
function afterConfigLoad(config, cliArguments, config_path, pkgVersion, pkgName) {
function startVerdaccio(config, cliListen, config_path, pkgVersion, pkgName, callback) {
if (!config.self_path) {
config.self_path = Path.resolve(config_path);
}
if (!config.https) {
config.https = {enable: false};
}
const app = server(config);
get_listen_addresses(cliArguments.listen, config.listen).forEach(function(addr) {
const addresses = getListListenAddresses(cliListen, config.listen);
addresses.forEach(function(addr) {
let webServer;
if (addr.proto === 'https') { // https must either have key cert and ca or a pfx and (optionally) a passphrase
if (!config.https || !config.https.key || !config.https.cert || !config.https.ca) {
@ -131,28 +134,7 @@ function afterConfigLoad(config, cliArguments, config_path, pkgVersion, pkgName)
fs.unlinkSync(addr.path);
}
webServer
.listen(addr.port || addr.path, addr.host)
.on('error', function(err) {
logger.logger.fatal({err: err}, 'cannot create server: @{err.message}');
process.exit(2);
});
logger.logger.warn({
addr: ( addr.path
? URL.format({
protocol: 'unix',
pathname: addr.path,
})
: URL.format({
protocol: addr.proto,
hostname: addr.host,
port: addr.port,
pathname: '/',
})
),
version: pkgName + '/' + pkgVersion,
}, 'http address - @{addr} - @{version}');
callback(webServer, addr, pkgName, pkgVersion);
});
// undocumented stuff for tests
@ -163,4 +145,27 @@ function afterConfigLoad(config, cliArguments, config_path, pkgVersion, pkgName)
}
}
export {afterConfigLoad};
function listenDefaultCallback(webServer, addr, pkgName, pkgVersion) {
webServer.listen(addr.port || addr.path, addr.host).on('error', function(err) {
logger.logger.fatal({err: err}, 'cannot create server: @{err.message}');
process.exit(2);
});
logger.logger.warn({
addr: ( addr.path
? URL.format({
protocol: 'unix',
pathname: addr.path,
})
: URL.format({
protocol: addr.proto,
hostname: addr.host,
port: addr.port,
pathname: '/',
})
),
version: pkgName + '/' + pkgVersion,
}, 'http address - @{addr} - @{version}');
}
export {startVerdaccio, listenDefaultCallback};

View File

@ -2,7 +2,8 @@
/* eslint no-sync:0 */
/* eslint no-empty:0 */
import {afterConfigLoad} from './bootstrap';
import {startVerdaccio, listenDefaultCallback} from './bootstrap';
import findConfigFile from './config-path';
if (process.getuid && process.getuid() === 0) {
@ -41,25 +42,23 @@ if (commander.args.length == 1 && !commander.config) {
if (commander.args.length !== 0) {
commander.help();
}
let verdaccioConfiguration;
let configPathLocation;
const cliListner = commander.listen;
let config;
let config_path;
try {
if (commander.config) {
config_path = path.resolve(commander.config);
} else {
config_path = findConfigFile();
}
config = Utils.parseConfigFile(config_path);
logger.logger.warn({file: config_path}, 'config file - @{file}');
configPathLocation = commander.config ? path.resolve(commander.config) : findConfigFile();
verdaccioConfiguration = Utils.parseConfigFile(configPathLocation);
logger.logger.warn({file: configPathLocation}, 'config file - @{file}');
} catch (err) {
logger.logger.fatal({file: config_path, err: err}, 'cannot open config file @{file}: @{!err.message}');
logger.logger.fatal({file: configPathLocation, err: err}, 'cannot open config file @{file}: @{!err.message}');
process.exit(1);
}
process.title = config.web && config.web.title || 'verdaccio';
afterConfigLoad(config, commander, config_path, pkgVersion, pkgName);
startVerdaccio(verdaccioConfiguration, cliListner, configPathLocation, pkgVersion, pkgName, listenDefaultCallback);
process.on('uncaughtException', function(err) {
logger.logger.fatal( {