Skip to content

Commit 0228cc6

Browse files
committed
Added gulp, eslint, uglify and fixed errors which came up with eslint
1 parent be60736 commit 0228cc6

8 files changed

Lines changed: 197 additions & 108 deletions

File tree

.eslintrc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "airbnb-base",
3+
"rules": {
4+
"no-console": 0,
5+
"no-var": 0,
6+
"comma-dangle": 0,
7+
"prefer-template": 0,
8+
"object-shorthand": 0,
9+
"prefer-rest-params": 0,
10+
"vars-on-top": 0,
11+
"no-restricted-syntax": 0,
12+
"prefer-arrow-callback": 0
13+
},
14+
"globals": {
15+
}
16+
}

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ Just include `logerr.js` file and the `init()` i.e initializer in the `<head>` s
5656
> Make sure you have CORS enabled if logging cross-domain.
5757
5858
```javascript
59-
//Request type is POST
59+
// Request type is POST
6060

6161
Logerr.init({
62-
remoteLogging: true, //Checkout https://github.com/i-break-codes/logerr-remote
62+
remoteLogging: true, // Checkout https://github.com/i-break-codes/logerr-remote
6363
remoteSettings: {
6464
url: 'REMOTE_URL',
6565
additionalParams: {
@@ -84,13 +84,13 @@ Also checkout Logerr Remote to log these exceptions remotely. (Powered by NodeJS
8484

8585
#### Default Configuration & Datatypes
8686
```javascript
87-
detailedErrors: true //Boolean true/false, optional
88-
remoteLogging: false //Boolean true/false, optional
89-
remoteSettings: { //Object {}, required if remoteLogging is set to true
90-
url: null, //String '', required if remoteLogging is set to true
91-
additionalParams: null, //Object {}, optional
92-
successCallback: null, //function() {}, optional
93-
errorCallback: null //function() {}, optional
87+
detailedErrors: true // Boolean true/false, optional
88+
remoteLogging: false // Boolean true/false, optional
89+
remoteSettings: { // Object {}, required if remoteLogging is set to true
90+
url: null, // String '', required if remoteLogging is set to true
91+
additionalParams: null, // Object {}, optional
92+
successCallback: null, // function() {}, optional
93+
errorCallback: null // function() {}, optional
9494
}
9595

9696
```

TODO.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### TODOs
2+
| Filename | line # | TODO
3+
|:------|:------:|:------

bower.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"node_modules",
2323
"bower_components",
2424
"test",
25-
"tests"
25+
"tests",
26+
"package.json",
27+
"TODO.md",
28+
"gulpfile.js"
2629
]
2730
}

gulpfile.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const gulp = require('gulp');
2+
const plumber = require('gulp-plumber');
3+
const notify = require('gulp-notify');
4+
const eslint = require('gulp-eslint');
5+
const watch = require('gulp-watch');
6+
const todo = require('gulp-todo');
7+
const uglify = require('gulp-uglify');
8+
const rename = require('gulp-rename');
9+
10+
const lint = (file) =>
11+
gulp.src(file)
12+
.pipe(plumber())
13+
.pipe(eslint())
14+
.pipe(eslint.format())
15+
.pipe(eslint.failAfterError())
16+
.on('error', notify.onError('Error: <%= error.message %>'));
17+
18+
gulp.task('lint', () => lint('logerr.js'));
19+
gulp.task('lint-gulp', () => lint('gulpfile.js'));
20+
21+
gulp.task('uglify', () =>
22+
gulp.src('logerr.js')
23+
.pipe(plumber())
24+
.pipe(uglify())
25+
.pipe(rename({ suffix: '.min' }))
26+
.pipe(gulp.dest('./'))
27+
.on('error', notify.onError('Error: <%= error.message %>'))
28+
);
29+
30+
gulp.task('todo', () =>
31+
gulp.src('logerr.js')
32+
.pipe(todo())
33+
.pipe(gulp.dest('./'))
34+
);
35+
36+
gulp.task('default', ['lint', 'lint-gulp', 'uglify', 'todo'], () => {
37+
watch('gulpfile.js', () => gulp.run(['lint-gulp']));
38+
watch('logerr.js', () => gulp.run(['lint', 'uglify', 'todo']));
39+
});

logerr.js

Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,99 +5,94 @@
55
* @author Vaibhav Mehta <vaibhav@decodingweb.com>
66
* @copyright Copyright (c) 2016 Vaibhav Mehta <https://github.com/i-break-codes>
77
* @license http://www.opensource.org/licenses/mit-license.html MIT License
8-
* @version 1.2 Stable
8+
* @version 1.2.1 Stable
99
*/
1010

11-
var Logerr = function() {
12-
'use strict';
13-
11+
window.Logerr = (function Logerr() {
1412
var setConfig;
1513

16-
function init(userConfig) {
17-
if(!userConfig) userConfig = {};
14+
function errorData(e) {
15+
var filename = e.filename.lastIndexOf('/');
16+
var datetime = new Date().toString();
1817

19-
// Default configuration
20-
var config = {
21-
detailedErrors: true,
22-
remoteLogging: false,
23-
remoteSettings: {
24-
url: null,
25-
additionalParams: null,
26-
successCallback: null,
27-
errorCallback: null
28-
}
18+
/**
19+
* userAgent only for POST request purposes, not required in pretty print
20+
*/
21+
return {
22+
type: e.type,
23+
path: e.filename,
24+
filename: e.filename.substring(++filename),
25+
line: e.lineno,
26+
column: e.colno,
27+
error: e.message,
28+
stackTrace: ((e.error) ? e.error.stack.toString().replace(/(\r\n|\n|\r)/gm, '') : ''),
29+
datetime: datetime,
30+
userAgent: navigator.userAgent || window.navigator.userAgent
2931
};
30-
31-
// Override with user config
32-
setConfig = Object.assign(config, userConfig);
33-
34-
//Remove current listener
35-
window.removeEventListener('error', _listener);
36-
37-
// Listen to errors
38-
window.addEventListener('error', _listener);
39-
}
40-
41-
// NOTE: Private
42-
function _listener(e) {
43-
if(setConfig.detailedErrors) {
44-
_detailedErrors(e);
45-
}
46-
47-
if(setConfig.remoteLogging) {
48-
_remoteLogging(e, setConfig.remoteSettings);
49-
}
5032
}
5133

52-
function _detailedErrors(e) {
53-
var i = _errorData(e);
54-
var helpPath = encodeURI("https://stackoverflow.com/search?q=" + i.error.split(' ').join('+'));
34+
function detailedErrors(e) {
35+
var i = errorData(e);
36+
var helpPath = encodeURI('https://stackoverflow.com/search?q=' + i.error.split(' ').join('+'));
5537

5638
var str = [
57-
"%cType: %c" + i.type,
58-
"%cError: %c" + i.error,
59-
"%cStackTrace: %c" + i.stackTrace,
60-
"%cFile Name: %c" + i.filename,
61-
"%cPath: %c" + i.path,
62-
"%cLine: %c" + i.line,
63-
"%cColumn: %c" + i.column,
64-
"%cDate: %c" + i.datetime,
65-
"%cDebug : %c" + i.path + ':' + i.line,
66-
"%cGet Help: " + "%c" + helpPath
67-
].join("\n");
68-
69-
if(window.chrome) {
70-
console.log(str, "font-weight: bold;", "color: #e74c3c;", "font-weight: bold;", "font-weight: normal; color: #e74c3c;","font-weight: bold;", "font-weight: normal; color: #e74c3c;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal; color: #3498db;");
39+
'%cType: %c' + i.type,
40+
'%cError: %c' + i.error,
41+
'%cStackTrace: %c' + i.stackTrace,
42+
'%cFile Name: %c' + i.filename,
43+
'%cPath: %c' + i.path,
44+
'%cLine: %c' + i.line,
45+
'%cColumn: %c' + i.column,
46+
'%cDate: %c' + i.datetime,
47+
'%cDebug : %c' + i.path + ':' + i.line,
48+
'%cGet Help: %c' + helpPath
49+
].join('\n');
50+
51+
if (window.chrome) {
52+
/* eslint-disable max-len */
53+
console.log(str, 'font-weight: bold;', 'color: #e74c3c;', 'font-weight: bold;', 'font-weight: normal; color: #e74c3c;', 'font-weight: bold;', 'font-weight: normal; color: #e74c3c;', 'font-weight: bold;', 'font-weight: normal;', 'font-weight: bold;', 'font-weight: normal;', 'font-weight: bold;', 'font-weight: normal;', 'font-weight: bold;', 'font-weight: normal;', 'font-weight: bold;', 'font-weight: normal;', 'font-weight: bold;', 'font-weight: normal;', 'font-weight: bold;', 'font-weight: normal; color: #3498db;');
54+
/* eslint-enable max-len */
7155
} else {
7256
console.log(str.replace(/%c/gi, ''));
7357
}
7458
}
7559

76-
function _remoteLogging(e, remoteSettings) {
77-
if(!remoteSettings.url) {
60+
function serializeData(params) {
61+
function encodeKey(k) {
62+
return encodeURIComponent(k) + '=' + encodeURIComponent(params[k]);
63+
}
64+
65+
return Object.keys(params).map(encodeKey).join('&');
66+
}
67+
68+
function remoteLogging(e, remoteSettings) {
69+
if (!remoteSettings.url) {
7870
throw new Error('Provide remote URL to log errors remotely');
79-
} else if(remoteSettings.additionalParams && typeof remoteSettings.additionalParams !== 'object') {
71+
} else if (
72+
remoteSettings.additionalParams &&
73+
typeof remoteSettings.additionalParams !== 'object'
74+
) {
8075
throw new Error('Invalid data type, additionalParams should be a valid object');
8176
}
8277

8378
var http = new XMLHttpRequest();
8479
var url = remoteSettings.url;
85-
var data = _errorData(e);
80+
var data = errorData(e);
8681
var setData = Object.assign(data, remoteSettings.additionalParams);
87-
var params = _serializeData(setData);
82+
var params = serializeData(setData);
8883

89-
http.open("POST", url, true);
90-
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
84+
http.open('POST', url, true);
85+
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
9186
http.send(params);
9287

93-
http.onreadystatechange = function() {
94-
if(http.readyState == 4 && http.status == 200) {
95-
if (http.readyState == XMLHttpRequest.DONE) {
96-
if(remoteSettings.successCallback) {
88+
http.onreadystatechange = function onreadystatechange() {
89+
if (http.readyState === 4 && http.status === 200) {
90+
if (http.readyState === XMLHttpRequest.DONE) {
91+
if (remoteSettings.successCallback) {
9792
remoteSettings.successCallback();
9893
}
9994
} else {
100-
if(remoteSettings.errorCallback) {
95+
if (remoteSettings.errorCallback) {
10196
remoteSettings.errorCallback();
10297
} else {
10398
throw new Error('Remote error logging failed!');
@@ -107,57 +102,62 @@ var Logerr = function() {
107102
};
108103
}
109104

110-
function _serializeData(params) {
111-
return Object.keys(params).map(function(k) {
112-
return encodeURIComponent(k) + "=" + encodeURIComponent(params[k]);
113-
}).join('&');
114-
}
115-
116-
function _errorData(e) {
117-
var filename = e.filename.lastIndexOf('/');
118-
var datetime = new Date().toString();
119-
120-
/**
121-
* userAgent only for POST request purposes, not required in pretty print
122-
*/
105+
function listener(e) {
106+
if (setConfig.detailedErrors) {
107+
detailedErrors(e);
108+
}
123109

124-
return {
125-
type: e.type,
126-
path: e.filename,
127-
filename: e.filename.substring(++filename),
128-
line: e.lineno,
129-
column: e.colno,
130-
error: e.message,
131-
stackTrace: ((e.error) ? e.error.stack.toString().replace(/(\r\n|\n|\r)/gm,"") : ""),
132-
datetime: datetime,
133-
userAgent: navigator.userAgent || window.navigator.userAgent
134-
};
110+
if (setConfig.remoteLogging) {
111+
remoteLogging(e, setConfig.remoteSettings);
112+
}
135113
}
136114

137-
//Polyfill for Object.assign
138-
if (typeof Object.assign != 'function') {
139-
Object.assign = function(target) {
115+
// Polyfill for Object.assign
116+
if (typeof Object.assign !== 'function') {
117+
Object.assign = function assign(target) {
140118
if (target === null) {
141119
throw new TypeError('Cannot convert undefined or null to object');
142120
}
143121

144-
target = Object(target);
122+
var newTarget = Object(target);
145123
for (var index = 1; index < arguments.length; index++) {
146124
var source = arguments[index];
147125
if (source !== null) {
148126
for (var key in source) {
149127
if (Object.prototype.hasOwnProperty.call(source, key)) {
150-
target[key] = source[key];
128+
newTarget[key] = source[key];
151129
}
152130
}
153131
}
154132
}
155-
return target;
133+
return newTarget;
156134
};
157135
}
158136

137+
function init(userConfig) {
138+
// Default configuration
139+
var config = {
140+
detailedErrors: true,
141+
remoteLogging: false,
142+
remoteSettings: {
143+
url: null,
144+
additionalParams: null,
145+
successCallback: null,
146+
errorCallback: null
147+
}
148+
};
149+
150+
// Override with user config
151+
setConfig = Object.assign(config, userConfig || {});
152+
153+
// Remove current listener
154+
window.removeEventListener('error', listener);
155+
156+
// Listen to errors
157+
window.addEventListener('error', listener);
158+
}
159+
159160
return {
160161
init: init
161162
};
162-
163-
}();
163+
}());

logerr.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "logerr",
3+
"version": "1.2.1",
4+
"description": "![alt tag](http://i.imgur.com/rVWDzcC.png)",
5+
"main": "logerr.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/i-break-codes/logerr.git"
9+
},
10+
"license": "MIT",
11+
"bugs": {
12+
"url": "https://github.com/i-break-codes/logerr/issues"
13+
},
14+
"homepage": "https://github.com/i-break-codes/logerr",
15+
"devDependencies": {
16+
"eslint": "^2.13.1",
17+
"eslint-config-airbnb-base": "^3.0.1",
18+
"eslint-plugin-import": "^1.15.0",
19+
"gulp": "^3.9.1",
20+
"gulp-eslint": "^2.1.0",
21+
"gulp-notify": "^2.2.0",
22+
"gulp-plumber": "^1.1.0",
23+
"gulp-rename": "^1.2.2",
24+
"gulp-todo": "^5.0.0",
25+
"gulp-uglify": "^2.0.0",
26+
"gulp-watch": "^4.3.9"
27+
}
28+
}

0 commit comments

Comments
 (0)