verdaccio/lib/cli.js

121 lines
3.6 KiB
JavaScript
Raw Normal View History

2013-10-09 17:47:55 +02:00
#!/usr/bin/env node
var pkg_file = '../package.yaml';
var fs = require('fs');
var yaml = require('js-yaml');
var commander = require('commander');
var server = require('../lib/index');
var crypto = require('crypto');
var pkg = require(pkg_file);
commander
.option('-l, --listen <[host:]port>', 'host:port number to listen on (default: localhost:4873)')
.option('-c, --config <config.yaml>', 'use this configuration file (default: ./config.yaml)')
.version(pkg.version)
.parse(process.argv);
if (commander.args.length == 1 && !commander.config) {
// handling "sinopia [config]" case if "-c" is missing in commandline
commander.config = commander.args.pop();
}
if (commander.args.length != 0) {
commander.help();
}
var config, config_path, have_question;
2013-10-09 17:47:55 +02:00
try {
if (commander.config) {
config_path = commander.config;
config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8'));
} else {
config_path = './config.yaml';
try {
config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8'));
} 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')();
config = yaml.safeLoad(created_config.yaml);
write_config_banner(created_config, config);
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();
}
});
})();
2013-10-09 17:47:55 +02:00
}
}
} catch(err) {
if (err.code === 'ENOENT') {
console.error('ERROR: cannot open configuration file "'+config_path+'", file not found');
process.exit(1);
} else {
throw err;
}
}
if (!have_question) afterConfigLoad();
function get_hostport() {
// command line || config file || default
var hostport = commander.listen || String(config.listen || '') || '4873';
2013-10-09 17:47:55 +02:00
hostport = hostport.split(':');
if (hostport.length < 2) {
hostport = [undefined, hostport[0]];
}
if (hostport[0] == null) {
hostport[0] = 'localhost';
}
return hostport;
2013-10-09 17:47:55 +02:00
}
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]);
console.log('Server is listening on http://%s:%s/', hostport[0], hostport[1]);
// undocumented stuff for tests
if (typeof(process.send) === 'function') {
process.send({sinopia_started: hostport});
}
2013-10-09 17:47:55 +02:00
}
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('===========================================================');
2013-10-09 17:47:55 +02:00
}