mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-06 22:40:26 -05:00
create config in xdg_config_home by default
This commit is contained in:
parent
c71e8dc829
commit
637d51cba0
2 changed files with 95 additions and 52 deletions
57
lib/cli.js
57
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}' )
|
||||
|
|
90
lib/config-path.js
Normal file
90
lib/config-path.js
Normal file
|
@ -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<paths.length; i++) {
|
||||
if (file_exists(paths[i].path)) return paths[i].path
|
||||
}
|
||||
|
||||
create_config_file(paths[0])
|
||||
return paths[0].path
|
||||
}
|
||||
|
||||
function create_config_file(config_path) {
|
||||
require('mkdirp').sync(Path.dirname(config_path.path))
|
||||
logger.logger.info({ file: config_path.path }, 'Creating default config file in @{file}')
|
||||
|
||||
var created_config = fs.readFileSync(require.resolve('../conf/default.yaml'), 'utf8')
|
||||
|
||||
if (config_path.type === 'xdg') {
|
||||
var data_dir = process.env.XDG_DATA_HOME
|
||||
|| Path.join(process.env.HOME, '.local', 'share')
|
||||
if (folder_exists(data_dir)) {
|
||||
data_dir = Path.resolve(Path.join(data_dir, 'sinopia', 'storage'))
|
||||
created_config = created_config.replace(/^storage: .\/storage$/m, 'storage: ' + data_dir)
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(config_path.path, created_config)
|
||||
}
|
||||
|
||||
function get_paths() {
|
||||
var try_paths = []
|
||||
|
||||
var xdg_config = process.env.XDG_CONFIG_HOME
|
||||
|| Path.join(process.env.HOME, '.config')
|
||||
if (folder_exists(xdg_config)) {
|
||||
try_paths.push({
|
||||
path: Path.join(xdg_config, 'sinopia', 'config.yaml'),
|
||||
type: 'xdg',
|
||||
})
|
||||
}
|
||||
|
||||
if (process.platform === 'win32'
|
||||
&& process.env.APPDATA
|
||||
&& folder_exists(process.env.APPDATA)) {
|
||||
try_paths.push({
|
||||
path: Path.resolve(Path.join(process.env.APPDATA, 'sinopia', 'config.yaml')),
|
||||
type: 'win',
|
||||
})
|
||||
}
|
||||
|
||||
try_paths.push({
|
||||
path: Path.resolve(Path.join('.', 'sinopia', 'config.yaml')),
|
||||
type: 'def',
|
||||
})
|
||||
|
||||
// backward compatibility
|
||||
try_paths.push({
|
||||
path: Path.resolve(Path.join('.', 'config.yaml')),
|
||||
type: 'old',
|
||||
})
|
||||
|
||||
return try_paths
|
||||
}
|
||||
|
||||
function folder_exists(path) {
|
||||
try {
|
||||
var stat = fs.statSync(path)
|
||||
} catch(_) {
|
||||
return false
|
||||
}
|
||||
|
||||
return stat.isDirectory()
|
||||
}
|
||||
|
||||
function file_exists(path) {
|
||||
try {
|
||||
var stat = fs.statSync(path)
|
||||
} catch(_) {
|
||||
return false
|
||||
}
|
||||
|
||||
return stat.isFile()
|
||||
}
|
||||
|
Loading…
Reference in a new issue