2013-10-09 10:47:55 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2014-02-23 12:20:50 -05:00
|
|
|
/*eslint no-sync:0*/
|
|
|
|
|
2014-01-23 22:36:03 -05:00
|
|
|
if (process.getuid && process.getuid() === 0) {
|
2014-11-12 06:14:37 -05:00
|
|
|
global.console.error("Sinopia doesn't need superuser privileges. Don't run it under root.")
|
2013-12-17 11:25:17 -05:00
|
|
|
}
|
|
|
|
|
2014-09-24 20:13:03 -05:00
|
|
|
process.title = 'sinopia'
|
2014-11-12 07:05:07 -05:00
|
|
|
require('es6-shim')
|
2014-09-24 20:13:03 -05:00
|
|
|
|
2013-12-28 19:52:23 -05:00
|
|
|
try {
|
2014-11-12 06:14:37 -05:00
|
|
|
// for debugging memory leaks
|
|
|
|
// totally optional
|
|
|
|
require('heapdump')
|
|
|
|
} catch(err) {}
|
2013-12-28 19:52:23 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
var logger = require('./logger')
|
|
|
|
logger.setup() // default setup
|
2013-10-11 00:32:59 -05:00
|
|
|
|
2014-11-12 06:14:37 -05:00
|
|
|
var commander = require('commander')
|
|
|
|
var fs = require('fs')
|
|
|
|
var YAML = require('js-yaml')
|
|
|
|
var Path = require('path')
|
|
|
|
var server = require('./index')
|
|
|
|
var pkg_file = '../package.yaml'
|
|
|
|
var pkg = YAML.safeLoad(fs.readFileSync(__dirname+'/'+ pkg_file, 'utf8'))
|
2013-10-09 10:47:55 -05:00
|
|
|
|
|
|
|
commander
|
2014-11-12 06:14:37 -05:00
|
|
|
.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)
|
2013-10-09 10:47:55 -05:00
|
|
|
|
|
|
|
if (commander.args.length == 1 && !commander.config) {
|
2014-11-12 06:14:37 -05:00
|
|
|
// handling "sinopia [config]" case if "-c" is missing in commandline
|
|
|
|
commander.config = commander.args.pop()
|
2013-10-09 10:47:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (commander.args.length != 0) {
|
2014-11-12 06:14:37 -05:00
|
|
|
commander.help()
|
2013-10-09 10:47:55 -05:00
|
|
|
}
|
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
var config, config_path, have_question
|
2013-10-09 10:47:55 -05:00
|
|
|
try {
|
2014-11-12 06:14:37 -05:00
|
|
|
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() {
|
|
|
|
global.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()
|
|
|
|
|
2014-11-12 10:49:37 -05:00
|
|
|
var created_config = fs.readFileSync(require.resolve('../conf/default.yaml'), 'utf8')
|
|
|
|
config = YAML.safeLoad(created_config)
|
2014-11-12 06:14:37 -05:00
|
|
|
write_config_banner(created_config, config)
|
2014-11-12 10:49:37 -05:00
|
|
|
fs.writeFileSync(config_path, created_config)
|
2014-11-12 06:14:37 -05:00
|
|
|
afterConfigLoad()
|
|
|
|
} else if (x[0] == 'N' || x[0] == 'n') {
|
|
|
|
rl.close()
|
|
|
|
global.console.log('So, you just accidentally run me in a wrong folder. Exitting...')
|
|
|
|
process.exit(1)
|
|
|
|
} else {
|
|
|
|
askUser()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.logger.fatal({ file: config_path, err: err }, 'cannot open config file @{file}: @{!err.message}')
|
|
|
|
process.exit(1)
|
2013-10-09 10:47:55 -05:00
|
|
|
}
|
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
if (!have_question) afterConfigLoad()
|
2013-10-09 11:22:29 -05:00
|
|
|
|
|
|
|
function get_hostport() {
|
2014-11-12 06:14:37 -05:00
|
|
|
// command line || config file || default
|
|
|
|
var hostport = commander.listen || String(config.listen || '') || '4873'
|
|
|
|
|
|
|
|
hostport = hostport.split(':')
|
|
|
|
if (hostport.length < 2) {
|
|
|
|
hostport = [ undefined, hostport[0] ]
|
|
|
|
}
|
|
|
|
if (hostport[0] == null) {
|
|
|
|
hostport[0] = 'localhost'
|
|
|
|
}
|
|
|
|
return hostport
|
2013-10-09 10:47:55 -05:00
|
|
|
}
|
2013-10-09 11:22:29 -05:00
|
|
|
|
|
|
|
function afterConfigLoad() {
|
2014-11-12 06:14:37 -05:00
|
|
|
if (!config.user_agent) config.user_agent = 'Sinopia/'+pkg.version
|
|
|
|
if (!config.self_path) config.self_path = Path.resolve(config_path)
|
2013-10-09 11:22:29 -05:00
|
|
|
|
2014-11-12 06:14:37 -05:00
|
|
|
logger.setup(config.logs)
|
2013-10-12 09:37:47 -05:00
|
|
|
|
2014-11-12 06:14:37 -05:00
|
|
|
var hostport = get_hostport()
|
|
|
|
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-18 22:17:52 -05:00
|
|
|
|
2014-11-12 06:14:37 -05:00
|
|
|
logger.logger.warn({ addr: 'http://'+hostport[0]+':'+hostport[1]+'/', version: 'Sinopia/'+pkg.version }, 'Server is listening on @{addr}')
|
2013-10-09 11:22:29 -05:00
|
|
|
|
2014-11-12 06:14:37 -05:00
|
|
|
// undocumented stuff for tests
|
|
|
|
if (typeof(process.send) === 'function') {
|
|
|
|
process.send({ sinopia_started: hostport })
|
|
|
|
}
|
2013-10-09 10:47:55 -05:00
|
|
|
}
|
|
|
|
|
2013-10-09 11:22:29 -05:00
|
|
|
function write_config_banner(def, config) {
|
2014-11-12 06:14:37 -05:00
|
|
|
var hostport = get_hostport()
|
|
|
|
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('===========================================================')
|
2013-10-09 10:47:55 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 05:29:46 -05:00
|
|
|
process.on('uncaughtException', function(err) {
|
2014-11-12 06:14:37 -05:00
|
|
|
logger.logger.fatal( { err: err }
|
|
|
|
, 'uncaught exception, please report this\n@{err.stack}' )
|
|
|
|
process.exit(255)
|
2013-12-10 05:29:46 -05:00
|
|
|
})
|
|
|
|
|