mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-20 22:52:46 -05:00
refactor: config path to resolve config files
This commit is contained in:
parent
86d92e96d2
commit
9b118a2dfb
3 changed files with 114 additions and 82 deletions
|
@ -3,6 +3,7 @@
|
||||||
/* eslint no-sync:0 */
|
/* eslint no-sync:0 */
|
||||||
/* eslint no-empty:0 */
|
/* eslint no-empty:0 */
|
||||||
import {afterConfigLoad} from './bootstrap';
|
import {afterConfigLoad} from './bootstrap';
|
||||||
|
import findConfigFile from './config-path';
|
||||||
|
|
||||||
if (process.getuid && process.getuid() === 0) {
|
if (process.getuid && process.getuid() === 0) {
|
||||||
global.console.error('Verdaccio doesn\'t need superuser privileges. Don\'t run it under root.');
|
global.console.error('Verdaccio doesn\'t need superuser privileges. Don\'t run it under root.');
|
||||||
|
@ -47,7 +48,7 @@ try {
|
||||||
if (commander.config) {
|
if (commander.config) {
|
||||||
config_path = path.resolve(commander.config);
|
config_path = path.resolve(commander.config);
|
||||||
} else {
|
} else {
|
||||||
config_path = require('./config-path')();
|
config_path = findConfigFile();
|
||||||
}
|
}
|
||||||
config = Utils.parseConfigFile(config_path);
|
config = Utils.parseConfigFile(config_path);
|
||||||
logger.logger.warn({file: config_path}, 'config file - @{file}');
|
logger.logger.warn({file: config_path}, 'config file - @{file}');
|
||||||
|
|
|
@ -1,110 +1,111 @@
|
||||||
'use strict';
|
import fs from 'fs';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import Path from 'path';
|
||||||
|
import logger from './logger';
|
||||||
|
import mkdirp from 'mkdirp';
|
||||||
|
|
||||||
|
import {folder_exists, file_exists} from './utils';
|
||||||
|
|
||||||
const fs = require('fs');
|
|
||||||
const Path = require('path');
|
|
||||||
const logger = require('./logger');
|
|
||||||
const CONFIG_FILE = 'config.yaml';
|
const CONFIG_FILE = 'config.yaml';
|
||||||
|
const XDG = 'xdg';
|
||||||
|
const WIN = 'win';
|
||||||
|
const WIN32 = 'win32';
|
||||||
const pkgJson = require('../../package.json');
|
const pkgJson = require('../../package.json');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find and get the first config file that match.
|
* Find and get the first config file that match.
|
||||||
* @return {String} the config file path
|
* @return {String} the config file path
|
||||||
*/
|
*/
|
||||||
function find_config_file() {
|
function findConfigFile() {
|
||||||
const paths = get_paths();
|
const configPaths = getConfigPaths();
|
||||||
|
|
||||||
for (let i=0; i<paths.length; i++) {
|
if (_.isEmpty(configPaths)) {
|
||||||
if (file_exists(paths[i].path)) return paths[i].path;
|
throw new Error('no configuration files can be proccesed');
|
||||||
}
|
}
|
||||||
|
|
||||||
create_config_file(paths[0]);
|
const primaryConf = _.find(configPaths, (configLocation) => file_exists(configLocation.path));
|
||||||
return paths[0].path;
|
if (_.isNil(primaryConf) === false) {
|
||||||
|
return primaryConf.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
return createConfigFile(_.head(configPaths)).path;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function createConfigFile(configLocation) {
|
||||||
* Create a default config file in your system.
|
createConfigFolder(configLocation);
|
||||||
* @param {String} config_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}');
|
|
||||||
|
|
||||||
let created_config = fs.readFileSync(require.resolve('../../conf/default.yaml'), 'utf8');
|
const defaultConfig = updateStorageLinks(configLocation, readDefaultConfig());
|
||||||
|
|
||||||
|
fs.writeFileSync(configLocation.path, defaultConfig);
|
||||||
|
|
||||||
|
return configLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
function readDefaultConfig() {
|
||||||
|
return fs.readFileSync(require.resolve('../../conf/default.yaml'), 'utf8');
|
||||||
|
}
|
||||||
|
|
||||||
|
function createConfigFolder(configLocation) {
|
||||||
|
mkdirp.sync(Path.dirname(configLocation.path));
|
||||||
|
logger.logger.info({file: configLocation.path}, 'Creating default config file in @{file}');
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateStorageLinks(configLocation, defaultConfig) {
|
||||||
|
console.log(defaultConfig);
|
||||||
|
if (configLocation.type !== XDG) {
|
||||||
|
return defaultConfig;
|
||||||
|
}
|
||||||
|
|
||||||
if (config_path.type === 'xdg') {
|
|
||||||
// $XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored,
|
// $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.
|
// 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');
|
let dataDir = process.env.XDG_DATA_HOME || Path.join(process.env.HOME, '.local', 'share');
|
||||||
if (folder_exists(data_dir)) {
|
if (folder_exists(dataDir)) {
|
||||||
data_dir = Path.resolve(Path.join(data_dir, pkgJson.name, 'storage'));
|
dataDir = Path.resolve(Path.join(dataDir, pkgJson.name, 'storage'));
|
||||||
created_config = created_config.replace(/^storage: .\/storage$/m, `storage: ${data_dir}`);
|
return defaultConfig.replace(/^storage: .\/storage$/m, `storage: ${dataDir}`);
|
||||||
|
} else {
|
||||||
|
return defaultConfig;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fs.writeFileSync(config_path.path, created_config);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function getConfigPaths() {
|
||||||
* Retrieve a list of possible config file locations.
|
return _.filter([getXDGDirectory(), getWindowsDirectory(), getRelativeDefaultDirectory(), getOldDirectory()]);
|
||||||
* @return {Array}
|
}
|
||||||
*/
|
|
||||||
function get_paths() {
|
|
||||||
let try_paths = [];
|
|
||||||
let xdg_config = process.env.XDG_CONFIG_HOME
|
|
||||||
|| process.env.HOME && Path.join(process.env.HOME, '.config');
|
|
||||||
if (xdg_config && folder_exists(xdg_config)) {
|
|
||||||
try_paths.push({
|
|
||||||
path: Path.join(xdg_config, pkgJson.name, CONFIG_FILE),
|
|
||||||
type: 'xdg',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.platform === 'win32' && process.env.APPDATA && folder_exists(process.env.APPDATA)) {
|
const getXDGDirectory = () => {
|
||||||
try_paths.push({
|
const xdgConfig = getXDGHome() ||
|
||||||
|
process.env.HOME && Path.join(process.env.HOME, '.config');
|
||||||
|
|
||||||
|
if (xdgConfig && folder_exists(xdgConfig)) {
|
||||||
|
return {
|
||||||
|
path: Path.join(xdgConfig, pkgJson.name, CONFIG_FILE),
|
||||||
|
type: XDG,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getXDGHome = () => process.env.XDG_CONFIG_HOME;
|
||||||
|
|
||||||
|
const getWindowsDirectory = () => {
|
||||||
|
if (process.platform === WIN32 && process.env.APPDATA && folder_exists(process.env.APPDATA)) {
|
||||||
|
return {
|
||||||
path: Path.resolve(Path.join(process.env.APPDATA, pkgJson.name, CONFIG_FILE)),
|
path: Path.resolve(Path.join(process.env.APPDATA, pkgJson.name, CONFIG_FILE)),
|
||||||
type: 'win',
|
type: WIN,
|
||||||
});
|
};
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
try_paths.push({
|
const getRelativeDefaultDirectory = () => {
|
||||||
|
return {
|
||||||
path: Path.resolve(Path.join('.', pkgJson.name, CONFIG_FILE)),
|
path: Path.resolve(Path.join('.', pkgJson.name, CONFIG_FILE)),
|
||||||
type: 'def',
|
type: 'def',
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|
||||||
// backward compatibility
|
const getOldDirectory = () => {
|
||||||
try_paths.push({
|
return {
|
||||||
path: Path.resolve(Path.join('.', CONFIG_FILE)),
|
path: Path.resolve(Path.join('.', CONFIG_FILE)),
|
||||||
type: 'old',
|
type: 'old',
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|
||||||
return try_paths;
|
export default findConfigFile;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether the path already exist.
|
|
||||||
* @param {String} path
|
|
||||||
* @return {Boolean}
|
|
||||||
*/
|
|
||||||
function folder_exists(path) {
|
|
||||||
try {
|
|
||||||
const stat = fs.statSync(path);
|
|
||||||
return stat.isDirectory();
|
|
||||||
} catch(_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether the file already exist.
|
|
||||||
* @param {String} path
|
|
||||||
* @return {Boolean}
|
|
||||||
*/
|
|
||||||
function file_exists(path) {
|
|
||||||
try {
|
|
||||||
const stat = fs.statSync(path);
|
|
||||||
return stat.isFile();
|
|
||||||
} catch(_) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = find_config_file;
|
|
||||||
|
|
|
@ -352,6 +352,36 @@ const ErrorCode = {
|
||||||
|
|
||||||
const parseConfigFile = (config_path) => YAML.safeLoad(fs.readFileSync(config_path, 'utf8'));
|
const parseConfigFile = (config_path) => YAML.safeLoad(fs.readFileSync(config_path, 'utf8'));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the path already exist.
|
||||||
|
* @param {String} path
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
function folder_exists(path) {
|
||||||
|
try {
|
||||||
|
const stat = fs.statSync(path);
|
||||||
|
return stat.isDirectory();
|
||||||
|
} catch(_) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check whether the file already exist.
|
||||||
|
* @param {String} path
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
function file_exists(path) {
|
||||||
|
try {
|
||||||
|
const stat = fs.statSync(path);
|
||||||
|
return stat.isFile();
|
||||||
|
} catch(_) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.folder_exists = folder_exists;
|
||||||
|
module.exports.file_exists = file_exists;
|
||||||
module.exports.parseInterval = parseInterval;
|
module.exports.parseInterval = parseInterval;
|
||||||
module.exports.semver_sort = semverSort;
|
module.exports.semver_sort = semverSort;
|
||||||
module.exports.parse_address = parse_address;
|
module.exports.parse_address = parse_address;
|
||||||
|
|
Loading…
Add table
Reference in a new issue