0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

only create config if we're asked to (+ nice help)

This commit is contained in:
Alex Kocharin 2013-10-09 20:22:29 +04:00
parent a76a443994
commit 0aa687624d

View file

@ -23,8 +23,8 @@ if (commander.args.length != 0) {
commander.help(); commander.help();
} }
var config, config_path, have_question;
try { try {
var config, config_path;
if (commander.config) { if (commander.config) {
config_path = commander.config; config_path = commander.config;
config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8')); config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8'));
@ -33,10 +33,34 @@ try {
try { try {
config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8')); config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8'));
} catch(err) { } catch(err) {
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
var timeout = setTimeout(function() {
console.log('I got tired waiting for an answer. Exitting...');
process.exit(1);
}, 20000);
(function askUser() {
have_question = true;
rl.question('Config file doesn\'t exist, create a new one? (Y/n) ', function(x) {
clearTimeout(timeout);
if (x[0] == 'Y' || x[0] == 'y' || x === '') {
rl.close();
var created_config = require('../lib/config_gen')(); var created_config = require('../lib/config_gen')();
config = yaml.safeLoad(created_config.yaml); config = yaml.safeLoad(created_config.yaml);
console.log('starting with default config, use user: "%s", pass: "%s" to authenticate', created_config.user, created_config.pass); write_config_banner(created_config, config);
fs.writeFileSync(config_path, created_config.yaml); fs.writeFileSync(config_path, created_config.yaml);
afterConfigLoad();
} else if (x[0] == 'N' || x[0] == 'n') {
rl.close();
console.log('So, you just accidentally run me in a wrong folder. Exitting...');
process.exit(1);
} else {
askUser();
}
});
})();
} }
} }
} catch(err) { } catch(err) {
@ -47,9 +71,10 @@ try {
throw err; throw err;
} }
} }
if (!config.user_agent) config.user_agent = 'Sinopia/'+pkg.version;
if (!config.self_path) config.self_path = config_path;
if (!have_question) afterConfigLoad();
function get_hostport() {
// command line || config file || default // command line || config file || default
var hostport = commander.listen || String(config.listen || '') || '4873'; var hostport = commander.listen || String(config.listen || '') || '4873';
@ -60,6 +85,14 @@ if (hostport.length < 2) {
if (hostport[0] == null) { if (hostport[0] == null) {
hostport[0] = 'localhost'; hostport[0] = 'localhost';
} }
return hostport;
}
function afterConfigLoad() {
if (!config.user_agent) config.user_agent = 'Sinopia/'+pkg.version;
if (!config.self_path) config.self_path = config_path;
var hostport = get_hostport();
server(config).listen(hostport[1], hostport[0]); server(config).listen(hostport[1], hostport[0]);
console.log('Server is listening on http://%s:%s/', hostport[0], hostport[1]); console.log('Server is listening on http://%s:%s/', hostport[0], hostport[1]);
@ -67,4 +100,21 @@ console.log('Server is listening on http://%s:%s/', hostport[0], hostport[1]);
if (typeof(process.send) === 'function') { if (typeof(process.send) === 'function') {
process.send({sinopia_started: hostport}); process.send({sinopia_started: hostport});
} }
}
function write_config_banner(def, config) {
var hostport = get_hostport();
console.log('===========================================================');
console.log(' Creating a new configuration file: "%s"', config_path);
console.log(' ');
console.log(' If you want to setup npm to work with this registry,');
console.log(' run following commands:');
console.log(' ');
console.log(' $ npm set registry http://%s:%s/', hostport[0], hostport[1]);
console.log(' $ npm set always-auth true');
console.log(' $ npm adduser');
console.log(' Username: %s', def.user);
console.log(' Password: %s', def.pass);
console.log('===========================================================');
}