1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/lib/cli.js

147 lines
4.2 KiB
JavaScript
Raw Normal View History

2013-10-09 17:47:55 +02:00
#!/usr/bin/env node
2014-02-23 18:20:50 +01:00
/*eslint no-sync:0*/
if (process.getuid && process.getuid() === 0) {
2013-12-23 01:14:57 +01:00
global.console.error("Sinopia doesn't need superuser privileges. Don't run it under root.")
}
try {
// for debugging memory leaks
// totally optional
require('heapdump')
} catch(err){}
2013-10-26 14:18:36 +02:00
var logger = require('./logger')
logger.setup() // default setup
2013-10-26 14:18:36 +02:00
var pkg_file = '../package.yaml'
, fs = require('fs')
, yaml = require('js-yaml')
, commander = require('commander')
, server = require('./index')
, crypto = require('crypto')
2013-12-19 15:50:31 +01:00
, pkg = yaml.safeLoad(fs.readFileSync(__dirname + '/' + pkg_file, 'utf8'))
2013-10-09 17:47:55 +02:00
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)
2013-10-26 14:18:36 +02:00
.parse(process.argv)
2013-10-09 17:47:55 +02:00
if (commander.args.length == 1 && !commander.config) {
// handling "sinopia [config]" case if "-c" is missing in commandline
2013-10-26 14:18:36 +02:00
commander.config = commander.args.pop()
2013-10-09 17:47:55 +02:00
}
if (commander.args.length != 0) {
2013-10-26 14:18:36 +02:00
commander.help()
2013-10-09 17:47:55 +02:00
}
2013-10-26 14:18:36 +02:00
var config, config_path, have_question
2013-10-09 17:47:55 +02:00
try {
if (commander.config) {
2013-10-26 14:18:36 +02:00
config_path = commander.config
config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8'))
2013-10-09 17:47:55 +02:00
} else {
2013-10-26 14:18:36 +02:00
config_path = './config.yaml'
2013-10-09 17:47:55 +02:00
try {
2013-10-26 14:18:36 +02:00
config = yaml.safeLoad(fs.readFileSync(config_path, 'utf8'))
2013-10-09 17:47:55 +02:00
} catch(err) {
2013-10-26 14:18:36 +02:00
var readline = require('readline')
var rl = readline.createInterface(process.stdin, process.stdout)
var timeout = setTimeout(function() {
2013-12-23 01:14:57 +01:00
global.console.log('I got tired waiting for an answer. Exitting...')
2013-10-26 14:18:36 +02:00
process.exit(1)
}, 20000)
2013-10-26 14:18:36 +02:00
;(function askUser() {
have_question = true
rl.question('Config file doesn\'t exist, create a new one? (Y/n) ', function(x) {
2013-10-26 14:18:36 +02:00
clearTimeout(timeout)
if (x[0] == 'Y' || x[0] == 'y' || x === '') {
2013-10-26 14:18:36 +02:00
rl.close()
2013-10-26 14:18:36 +02:00
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') {
2013-10-26 14:18:36 +02:00
rl.close()
2013-12-23 01:14:57 +01:00
global.console.log('So, you just accidentally run me in a wrong folder. Exitting...')
2013-10-26 14:18:36 +02:00
process.exit(1)
} else {
2013-10-26 14:18:36 +02:00
askUser()
}
2013-10-26 14:18:36 +02:00
})
})()
2013-10-09 17:47:55 +02:00
}
}
} catch(err) {
2013-10-26 14:18:36 +02:00
logger.logger.fatal({file: config_path, err: err}, 'cannot open config file @{file}: @{!err.message}')
process.exit(1)
2013-10-09 17:47:55 +02:00
}
2013-10-26 14:18:36 +02:00
if (!have_question) afterConfigLoad()
function get_hostport() {
// command line || config file || default
2013-10-26 14:18:36 +02:00
var hostport = commander.listen || String(config.listen || '') || '4873'
2013-10-09 17:47:55 +02:00
2013-10-26 14:18:36 +02:00
hostport = hostport.split(':')
if (hostport.length < 2) {
2013-10-26 14:18:36 +02:00
hostport = [undefined, hostport[0]]
}
if (hostport[0] == null) {
2013-10-26 14:18:36 +02:00
hostport[0] = 'localhost'
}
2013-10-26 14:18:36 +02:00
return hostport
2013-10-09 17:47:55 +02:00
}
function afterConfigLoad() {
2013-10-26 14:18:36 +02:00
if (!config.user_agent) config.user_agent = 'Sinopia/'+pkg.version
if (!config.self_path) config.self_path = config_path
2013-10-12 16:37:47 +02:00
logger.setup(config.logs)
2013-10-26 14:18:36 +02:00
var hostport = get_hostport()
2013-12-19 04:17:52 +01:00
server(config)
.listen(hostport[1], hostport[0])
.on('error', function(err) {
logger.logger.fatal({err: err}, 'cannot create server: @{err.message}')
process.exit(2)
})
2013-12-11 22:55:17 +01:00
logger.logger.warn({addr: 'http://'+hostport[0]+':'+hostport[1]+'/', version: 'Sinopia/'+pkg.version}, 'Server is listening on @{addr}')
// undocumented stuff for tests
if (typeof(process.send) === 'function') {
2013-10-26 14:18:36 +02:00
process.send({sinopia_started: hostport})
}
2013-10-09 17:47:55 +02:00
}
function write_config_banner(def, config) {
2013-10-26 14:18:36 +02:00
var hostport = get_hostport()
2013-12-23 01:14:57 +01:00
var log = global.console.log
log('===========================================================')
log(' Creating a new configuration file: "%s"', config_path)
log(' ')
log(' If you want to setup npm to work with this registry,')
log(' run following commands:')
log(' ')
log(' $ npm set registry http://%s:%s/', hostport[0], hostport[1])
log(' $ npm set always-auth true')
log(' $ npm adduser')
log(' Username: %s', def.user)
log(' Password: %s', def.pass)
log('===========================================================')
2013-10-09 17:47:55 +02:00
}
2013-12-10 11:29:46 +01:00
process.on('uncaughtException', function(err) {
logger.logger.fatal({err: err}, 'uncaught exception, please report this\n@{err.stack}')
process.exit(255)
})