1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-21 07:29:37 +01:00

Refactor, reduce eslint warnings

This commit is contained in:
Juan Picado @jotadeveloper 2017-05-20 10:47:43 +02:00
parent 45e4e66b4c
commit 2961d21124
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
4 changed files with 43 additions and 21 deletions

@ -3,9 +3,10 @@
const fs = require('fs'); const fs = require('fs');
const Path = require('path'); const Path = require('path');
const logger = require('./logger'); const logger = require('./logger');
const CONFIG_FILE = 'config.yaml';
const pkgJson = require('../package.json');
/** /**
* * 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 find_config_file() {
@ -19,6 +20,10 @@ function find_config_file() {
return paths[0].path; return paths[0].path;
} }
/**
* Create a default config file in your system.
* @param {String} config_path
*/
function create_config_file(config_path) { function create_config_file(config_path) {
require('mkdirp').sync(Path.dirname(config_path.path)); require('mkdirp').sync(Path.dirname(config_path.path));
logger.logger.info({file: config_path.path}, 'Creating default config file in @{file}'); logger.logger.info({file: config_path.path}, 'Creating default config file in @{file}');
@ -26,69 +31,80 @@ function create_config_file(config_path) {
let created_config = fs.readFileSync(require.resolve('../conf/default.yaml'), 'utf8'); let created_config = fs.readFileSync(require.resolve('../conf/default.yaml'), 'utf8');
if (config_path.type === 'xdg') { if (config_path.type === 'xdg') {
let data_dir = process.env.XDG_DATA_HOME // $XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored,
|| Path.join(process.env.HOME, '.local', 'share'); // 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');
if (folder_exists(data_dir)) { if (folder_exists(data_dir)) {
data_dir = Path.resolve(Path.join(data_dir, 'verdaccio', 'storage')); data_dir = Path.resolve(Path.join(data_dir, pkgJson.name, 'storage'));
created_config = created_config.replace(/^storage: .\/storage$/m, 'storage: ' + data_dir); created_config = created_config.replace(/^storage: .\/storage$/m, `storage: ${data_dir}`);
} }
} }
fs.writeFileSync(config_path.path, created_config); fs.writeFileSync(config_path.path, created_config);
} }
/**
* Retrieve a list of possible config file locations.
* @return {Array}
*/
function get_paths() { function get_paths() {
let try_paths = []; let try_paths = [];
let xdg_config = process.env.XDG_CONFIG_HOME let xdg_config = process.env.XDG_CONFIG_HOME
|| process.env.HOME && Path.join(process.env.HOME, '.config'); || process.env.HOME && Path.join(process.env.HOME, '.config');
if (xdg_config && folder_exists(xdg_config)) { if (xdg_config && folder_exists(xdg_config)) {
try_paths.push({ try_paths.push({
path: Path.join(xdg_config, 'verdaccio', 'config.yaml'), path: Path.join(xdg_config, pkgJson.name, CONFIG_FILE),
type: 'xdg', type: 'xdg',
}); });
} }
if (process.platform === 'win32' if (process.platform === 'win32' && process.env.APPDATA && folder_exists(process.env.APPDATA)) {
&& process.env.APPDATA
&& folder_exists(process.env.APPDATA)) {
try_paths.push({ try_paths.push({
path: Path.resolve(Path.join(process.env.APPDATA, 'verdaccio', 'config.yaml')), path: Path.resolve(Path.join(process.env.APPDATA, pkgJson.name, CONFIG_FILE)),
type: 'win', type: 'win',
}); });
} }
try_paths.push({ try_paths.push({
path: Path.resolve(Path.join('.', 'verdaccio', 'config.yaml')), path: Path.resolve(Path.join('.', pkgJson.name, CONFIG_FILE)),
type: 'def', type: 'def',
}); });
// backward compatibility // backward compatibility
try_paths.push({ try_paths.push({
path: Path.resolve(Path.join('.', 'config.yaml')), path: Path.resolve(Path.join('.', CONFIG_FILE)),
type: 'old', type: 'old',
}); });
return try_paths; return try_paths;
} }
/**
* Check whether the path already exist.
* @param {String} path
* @return {Boolean}
*/
function folder_exists(path) { function folder_exists(path) {
try { try {
var stat = fs.statSync(path); const stat = fs.statSync(path);
return stat.isDirectory();
} catch(_) { } catch(_) {
return false; return false;
} }
return stat.isDirectory();
} }
/**
* Check whether the file already exist.
* @param {String} path
* @return {Boolean}
*/
function file_exists(path) { function file_exists(path) {
try { try {
var stat = fs.statSync(path); const stat = fs.statSync(path);
return stat.isFile();
} catch(_) { } catch(_) {
return false; return false;
} }
return stat.isFile();
} }
module.exports = find_config_file; module.exports = find_config_file;

@ -1,3 +1,5 @@
/* eslint require-jsdoc: off */
'use strict'; 'use strict';
/** Node.js Crypt(3) Library /** Node.js Crypt(3) Library
@ -12,8 +14,8 @@
*/ */
let crypt = require('unix-crypt-td-js'), const crypt = require('unix-crypt-td-js');
crypto = require('crypto'); const crypto = require('crypto');
function createSalt(type) { function createSalt(type) {
type = type || 'sha512'; type = type || 'sha512';

@ -1,3 +1,5 @@
/* eslint require-jsdoc: off */
'use strict'; 'use strict';
let fs = require('fs'); let fs = require('fs');

@ -1,3 +1,5 @@
/* eslint require-jsdoc: off */
'use strict'; 'use strict';
let crypto = require('crypto'); let crypto = require('crypto');