1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00
verdaccio/lib/cli.js

127 lines
3.8 KiB
JavaScript
Raw Normal View History

2013-10-09 17:47:55 +02:00
#!/usr/bin/env node
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')
, pkg = require(pkg_file)
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-10-26 14:18:36 +02:00
console.log('I got tired waiting for an answer. Exitting...')
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()
console.log('So, you just accidentally run me in a wrong folder. Exitting...')
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()
server(config).listen(hostport[1], hostport[0])
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()
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
}
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)
})