diff --git a/lib/cli.js b/lib/cli.js index 7470be1d6..3bc6efedc 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -44,49 +44,18 @@ if (commander.args.length != 0) { var config, config_path, have_question try { if (commander.config) { - config_path = commander.config - config = YAML.safeLoad(fs.readFileSync(config_path, 'utf8')) + config_path = Path.resolve(commander.config) } 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() - - var created_config = fs.readFileSync(require.resolve('../conf/default.yaml'), 'utf8') - config = YAML.safeLoad(created_config) - write_config_banner(created_config, config) - fs.writeFileSync(config_path, created_config) - 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() - } - }) - })() - } + config_path = require('./config-path')() } + config = YAML.safeLoad(fs.readFileSync(config_path, 'utf8')) + logger.logger.warn({ file: config_path }, 'Using config file: @{file}') } catch (err) { logger.logger.fatal({ file: config_path, err: err }, 'cannot open config file @{file}: @{!err.message}') process.exit(1) } -if (!have_question) afterConfigLoad() +afterConfigLoad() function get_hostport() { // command line || config file || default @@ -124,22 +93,6 @@ function afterConfigLoad() { } } -function write_config_banner(def, config) { - 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('===========================================================') -} - process.on('uncaughtException', function(err) { logger.logger.fatal( { err: err } , 'uncaught exception, please report this\n@{err.stack}' ) diff --git a/lib/config-path.js b/lib/config-path.js new file mode 100644 index 000000000..a73dd1daf --- /dev/null +++ b/lib/config-path.js @@ -0,0 +1,90 @@ +var fs = require('fs') +var Path = require('path') +var logger = require('./logger') + +module.exports = find_config_file + +function find_config_file() { + var paths = get_paths() + + for (var i=0; i