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

Refactor, cleaned all eslint warnings, eslint set to error to avoid more warnings

This commit is contained in:
Juan Picado @jotadeveloper 2017-05-20 12:33:23 +02:00
parent a1e6368c29
commit abc40b8e8e
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
4 changed files with 101 additions and 51 deletions

@ -27,6 +27,9 @@ rules:
# add "falls through" for those
no-fallthrough: 2
# enforce use curly always
# curly: 1
# just warnings about whitespace weirdness here
eol-last: 1
no-irregular-whitespace: 1
@ -51,17 +54,32 @@ rules:
# camelcase is standard, but this should be 1 and then 2 soon
camelcase: 0
# configuration that should be upgraded progresivelly
require-jsdoc: 1
valid-jsdoc: 1
# jsdoc is mandatory
require-jsdoc: 2
valid-jsdoc: 2
# this feature is cool but not supported by Node 4, disable via comments
prefer-spread: 1
no-constant-condition: 1
no-var: 1
no-empty: 1
guard-for-in: 1
no-invalid-this: 1
new-cap: 1
one-var: 1
no-redeclare: 1
prefer-rest-params: 1
no-console: [1, {"allow": ["log", "warn"]}]
# encorage use es6
no-var: 2
# configuration that should be upgraded progresivelly
no-constant-condition: 2
no-empty: 2
# loop over objects http://eslint.org/docs/rules/guard-for-in
guard-for-in: 2
# this must be used within classes
no-invalid-this: 2
# All object must be uppercase
new-cap: 2
# readbility is important, no multiple inline declarations
one-var: 2
# console not allowed unless for testing
no-console: [2, {"allow": ["log", "warn"]}]

@ -4,15 +4,15 @@
* file-locking.js - file system locking (replaces fs-ext)
*/
let async = require('async'),
locker = require('lockfile'),
fs = require('fs'),
path = require('path');
const async = require('async');
const locker = require('lockfile');
const fs = require('fs');
const path = require('path');
// locks a file by creating a lock file
function lockFile(name, next) {
let lockFileName = name + '.lock',
lockOpts = {
const lockFile = function(name, next) {
const lockFileName = `${name}.lock`;
const lockOpts = {
wait: 1000, // time (ms) to wait when checking for stale locks
pollPeriod: 100, // how often (ms) to re-check stale locks
@ -20,7 +20,7 @@ function lockFile(name, next) {
retries: 100, // number of times to attempt to create a lock
retryWait: 100, // time (ms) between tries
};
};
async.series({
@ -64,26 +64,27 @@ function lockFile(name, next) {
// lock succeeded
return next(null);
});
}
};
// unlocks file by removing existing lock file
function unlockFile(name, next) {
let lockFileName = name + '.lock';
const unlockFile= function(name, next) {
const lockFileName = `${name}.lock`;
locker.unlock(lockFileName, function(err) {
if (err) {
return next(err);
}
return next(null);
});
}
};
/**
* reads a local file, which involves
* Reads a local file, which involves
* optionally taking a lock
* reading the file contents
* optionally parsing JSON contents
* @param {*} name
* @param {*} options
* @param {*} next
*/
function readFile(name, options, next) {
if (typeof options === 'function' && next === null) {
@ -95,7 +96,7 @@ function readFile(name, options, next) {
options.lock = options.lock || false;
options.parse = options.parse || false;
function lock(callback) {
const lock = function(callback) {
if (!options.lock) {
return callback(null);
}
@ -106,9 +107,9 @@ function readFile(name, options, next) {
}
return callback(null);
});
}
};
function read(callback) {
const read = function(callback) {
fs.readFile(name, 'utf8', function(err, contents) {
if (err) {
return callback(err);
@ -116,9 +117,9 @@ function readFile(name, options, next) {
callback(null, contents);
});
}
};
function parseJSON(contents, callback) {
const parseJSON = function(contents, callback) {
if (!options.parse) {
return callback(null, contents);
}
@ -129,7 +130,7 @@ function readFile(name, options, next) {
} catch (err) {
return callback(err);
}
}
};
async.waterfall([
lock,
@ -148,5 +149,4 @@ function readFile(name, options, next) {
exports.lockFile = lockFile;
exports.unlockFile = unlockFile;
exports.readFile = readFile;

@ -1,22 +1,22 @@
'use strict';
let express = require('express');
let Error = require('http-errors');
let compression = require('compression');
let Auth = require('./auth');
let Logger = require('./logger');
let Config = require('./config');
let Middleware = require('./middleware');
let Cats = require('./status-cats');
let Storage = require('./storage');
const express = require('express');
const Error = require('http-errors');
const compression = require('compression');
const Auth = require('./auth');
const Logger = require('./logger');
const Config = require('./config');
const Middleware = require('./middleware');
const Cats = require('./status-cats');
const Storage = require('./storage');
module.exports = function(config_hash) {
Logger.setup(config_hash.logs);
let config = new Config(config_hash);
let storage = new Storage(config);
let auth = new Auth(config);
let app = express();
const config = new Config(config_hash);
const storage = new Storage(config);
const auth = new Auth(config);
const app = express();
// run in production mode by default, just in case
// it shouldn't make any difference anyway
@ -64,7 +64,9 @@ module.exports = function(config_hash) {
if (config._debug) {
app.get('/-/_debug', function(req, res, next) {
let do_gc = typeof(global.gc) !== 'undefined';
if (do_gc) global.gc();
if (do_gc) {
global.gc();
}
next({
pid: process.pid,
main: process.mainModule.filename,
@ -90,8 +92,12 @@ module.exports = function(config_hash) {
});
app.use(function(err, req, res, next) {
if (Object.prototype.toString.call(err) !== '[object Error]') return next(err);
if (err.code === 'ECONNABORT' && res.statusCode === 304) return next();
if (Object.prototype.toString.call(err) !== '[object Error]') {
return next(err);
}
if (err.code === 'ECONNABORT' && res.statusCode === 304) {
return next();
}
if (typeof(res.report_error) !== 'function') {
// in case of very early error this middleware may not be loaded before error is generated
// fixing that

@ -3,8 +3,16 @@
const fs = require('fs');
const Path = require('path');
/**
* Handle local database.
* FUTURE: must be a plugin.
*/
class LocalData {
/**
* Load an parse the local json database.
* @param {*} path the database path
*/
constructor(path) {
this.path = path;
try {
@ -14,6 +22,10 @@ const Path = require('path');
}
}
/**
* Add a new element.
* @param {*} name
*/
add(name) {
if (this.data.list.indexOf(name) === -1) {
this.data.list.push(name);
@ -21,6 +33,10 @@ const Path = require('path');
}
}
/**
* Remove an element from the database.
* @param {*} name
*/
remove(name) {
const i = this.data.list.indexOf(name);
if (i !== -1) {
@ -29,15 +45,25 @@ const Path = require('path');
this.sync();
}
/**
* Return all database elements.
* @return {Array}
*/
get() {
return this.data.list;
}
/**
* Syncronize {create} database whether does not exist.
*/
sync() {
// Uses sync to prevent ugly race condition
try {
require('mkdirp').sync(Path.dirname(this.path));
} catch(err) {}
} catch(err) {
// perhaps a logger instance?
/* eslint no-empty:off */
}
fs.writeFileSync(this.path, JSON.stringify(this.data));
}