2017-04-23 20:02:26 +02:00
|
|
|
'use strict';
|
2014-11-16 14:33:03 +01:00
|
|
|
|
2017-05-20 08:56:06 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
const Path = require('path');
|
|
|
|
const logger = require('./logger');
|
2017-05-20 10:47:43 +02:00
|
|
|
const CONFIG_FILE = 'config.yaml';
|
|
|
|
const pkgJson = require('../package.json');
|
2017-05-20 08:56:06 +02:00
|
|
|
/**
|
2017-05-20 10:47:43 +02:00
|
|
|
* Find and get the first config file that match.
|
2017-05-20 08:56:06 +02:00
|
|
|
* @return {String} the config file path
|
|
|
|
*/
|
2014-11-16 14:33:03 +01:00
|
|
|
function find_config_file() {
|
2017-05-20 08:56:06 +02:00
|
|
|
const paths = get_paths();
|
2014-11-16 14:33:03 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
for (let i=0; i<paths.length; i++) {
|
|
|
|
if (file_exists(paths[i].path)) return paths[i].path;
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
create_config_file(paths[0]);
|
|
|
|
return paths[0].path;
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 10:47:43 +02:00
|
|
|
/**
|
|
|
|
* Create a default config file in your system.
|
|
|
|
* @param {String} config_path
|
|
|
|
*/
|
2014-11-16 14:33:03 +01:00
|
|
|
function create_config_file(config_path) {
|
2017-04-23 20:02:26 +02:00
|
|
|
require('mkdirp').sync(Path.dirname(config_path.path));
|
|
|
|
logger.logger.info({file: config_path.path}, 'Creating default config file in @{file}');
|
2014-11-16 14:33:03 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
let created_config = fs.readFileSync(require.resolve('../conf/default.yaml'), 'utf8');
|
2014-11-16 14:33:03 +01:00
|
|
|
|
|
|
|
if (config_path.type === 'xdg') {
|
2017-05-20 10:47:43 +02:00
|
|
|
// $XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored,
|
|
|
|
// If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.
|
|
|
|
let data_dir = process.env.XDG_DATA_HOME|| Path.join(process.env.HOME, '.local', 'share');
|
2014-11-16 14:33:03 +01:00
|
|
|
if (folder_exists(data_dir)) {
|
2017-05-20 10:47:43 +02:00
|
|
|
data_dir = Path.resolve(Path.join(data_dir, pkgJson.name, 'storage'));
|
|
|
|
created_config = created_config.replace(/^storage: .\/storage$/m, `storage: ${data_dir}`);
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
fs.writeFileSync(config_path.path, created_config);
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 10:47:43 +02:00
|
|
|
/**
|
|
|
|
* Retrieve a list of possible config file locations.
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
2014-11-16 14:33:03 +01:00
|
|
|
function get_paths() {
|
2017-04-23 20:02:26 +02:00
|
|
|
let try_paths = [];
|
|
|
|
let xdg_config = process.env.XDG_CONFIG_HOME
|
|
|
|
|| process.env.HOME && Path.join(process.env.HOME, '.config');
|
2014-12-19 16:00:56 +01:00
|
|
|
if (xdg_config && folder_exists(xdg_config)) {
|
2014-11-16 14:33:03 +01:00
|
|
|
try_paths.push({
|
2017-05-20 10:47:43 +02:00
|
|
|
path: Path.join(xdg_config, pkgJson.name, CONFIG_FILE),
|
2014-11-16 14:33:03 +01:00
|
|
|
type: 'xdg',
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 10:47:43 +02:00
|
|
|
if (process.platform === 'win32' && process.env.APPDATA && folder_exists(process.env.APPDATA)) {
|
2014-11-16 14:33:03 +01:00
|
|
|
try_paths.push({
|
2017-05-20 10:47:43 +02:00
|
|
|
path: Path.resolve(Path.join(process.env.APPDATA, pkgJson.name, CONFIG_FILE)),
|
2014-11-16 14:33:03 +01:00
|
|
|
type: 'win',
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try_paths.push({
|
2017-05-20 10:47:43 +02:00
|
|
|
path: Path.resolve(Path.join('.', pkgJson.name, CONFIG_FILE)),
|
2014-11-16 14:33:03 +01:00
|
|
|
type: 'def',
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
2014-11-16 14:33:03 +01:00
|
|
|
|
|
|
|
// backward compatibility
|
|
|
|
try_paths.push({
|
2017-05-20 10:47:43 +02:00
|
|
|
path: Path.resolve(Path.join('.', CONFIG_FILE)),
|
2014-11-16 14:33:03 +01:00
|
|
|
type: 'old',
|
2017-04-23 20:02:26 +02:00
|
|
|
});
|
2014-11-16 14:33:03 +01:00
|
|
|
|
2017-04-23 20:02:26 +02:00
|
|
|
return try_paths;
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 10:47:43 +02:00
|
|
|
/**
|
|
|
|
* Check whether the path already exist.
|
|
|
|
* @param {String} path
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2014-11-16 14:33:03 +01:00
|
|
|
function folder_exists(path) {
|
|
|
|
try {
|
2017-05-20 10:47:43 +02:00
|
|
|
const stat = fs.statSync(path);
|
|
|
|
return stat.isDirectory();
|
2014-11-16 14:33:03 +01:00
|
|
|
} catch(_) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return false;
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 10:47:43 +02:00
|
|
|
/**
|
|
|
|
* Check whether the file already exist.
|
|
|
|
* @param {String} path
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2014-11-16 14:33:03 +01:00
|
|
|
function file_exists(path) {
|
|
|
|
try {
|
2017-05-20 10:47:43 +02:00
|
|
|
const stat = fs.statSync(path);
|
|
|
|
return stat.isFile();
|
2014-11-16 14:33:03 +01:00
|
|
|
} catch(_) {
|
2017-04-23 20:02:26 +02:00
|
|
|
return false;
|
2014-11-16 14:33:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 08:56:06 +02:00
|
|
|
module.exports = find_config_file;
|