Skip to content

Commit ca80b58

Browse files
committed
feat(module): Initial release
0 parents  commit ca80b58

File tree

10 files changed

+413
-0
lines changed

10 files changed

+413
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules
2+
.nyc_output
3+
.vscode
4+
.idea
5+
.DS_Store
6+
coverage
7+
8+
#TypeScript typings/definitions
9+
typings
10+
11+
dist
12+
test/*.js

.travis.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
sudo: false
2+
language: node_js
3+
cache:
4+
directories:
5+
- node_modules
6+
branches:
7+
only:
8+
- master
9+
notifications:
10+
email: false
11+
node_js:
12+
- '7'
13+
before_install:
14+
- npm i -g npm@^2.0.0
15+
before_script:
16+
- npm prune
17+
script:
18+
- npm run build
19+
- npm run cover
20+
- npm run check-coverage
21+
after_success:
22+
- npm run report-coverage
23+
- npm run semantic-release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Antonio Pintus
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
debuggo
2+
======
3+
4+
General purpose debug library based on visionmedia/debug
5+
6+
[![travis build](https://img.shields.io/travis/vivocha/debuggo.svg)](https://travis-ci.org/vivocha/debuggo)
7+
[![Coverage Status](https://coveralls.io/repos/github/vivocha/debuggo/badge.svg?branch=master)](https://coveralls.io/github/vivocha/debuggo?branch=master)
8+
[![npm version](https://img.shields.io/npm/v/debuggo.svg)](https://www.npmjs.com/package/debuggo)

package.json

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"name": "debuggo",
3+
"version": "0.0.0-development",
4+
"description": "General purpose debug library based on visionmedia/debug",
5+
"main": "dist/index.js",
6+
"typings": "dist/index.d.ts",
7+
"directories": {
8+
"test": "dist/test"
9+
},
10+
"scripts": {
11+
"clean": "rimraf dist coverage .nyc_output",
12+
"clean:all": "npm run clean && rimraf node_modules",
13+
"prebuild": "rimraf dist",
14+
"build": "tsc",
15+
"precommit": "npm run build && npm run cover && npm run check-coverage",
16+
"commit": "git-cz",
17+
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
18+
"report-coverage": "cat ./coverage/lcov.info | coveralls",
19+
"watch:test": "npm t -- -w",
20+
"test": "DEBUG=test:* mocha test/*.test.js",
21+
"cover": "nyc --reporter=lcov --reporter=text npm t",
22+
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
23+
},
24+
"repository": {
25+
"type": "git",
26+
"url": "https://github.com/vivocha/debuggo.git"
27+
},
28+
"files": [
29+
"dist",
30+
"README.md",
31+
"LICENSE"
32+
],
33+
"author": "Vivocha <dev@vivocha.com> (http://www.vivocha.com/)",
34+
"license": "MIT",
35+
"devDependencies": {
36+
"@types/chai": "^3.5.2",
37+
"@types/chai-as-promised": "0.0.30",
38+
"@types/chai-spies": "0.0.0",
39+
"@types/debug": "0.0.29",
40+
"@types/mocha": "^2.2.41",
41+
"@types/node": "^7.0.18",
42+
"chai": "^3.5.0",
43+
"chai-as-promised": "^6.0.0",
44+
"chai-spies": "^0.7.1",
45+
"commitizen": "^2.9.6",
46+
"coveralls": "^2.13.1",
47+
"cz-conventional-changelog": "^2.0.0",
48+
"husky": "^0.13.3",
49+
"mocha": "^3.4.1",
50+
"nyc": "^10.3.2",
51+
"rimraf": "^2.6.1",
52+
"semantic-release": "^6.3.6",
53+
"typescript": "^2.3.2"
54+
},
55+
"config": {
56+
"commitizen": {
57+
"path": "node_modules/cz-conventional-changelog"
58+
}
59+
},
60+
"dependencies": {
61+
"debug": "^2.6.8"
62+
}
63+
}

src/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const debug = require("debug");
4+
const __namespaces = {};
5+
function createLogger(ns, context) {
6+
__namespaces[ns] = true;
7+
let d = context ? function (label) {
8+
let origDebugger = debug(label);
9+
let wrappedDebugger = function (formatter, ...args) {
10+
origDebugger(`${context} ${formatter}`, ...args);
11+
};
12+
wrappedDebugger.enabled = origDebugger.enabled;
13+
wrappedDebugger.log = origDebugger.log;
14+
wrappedDebugger.namespace = origDebugger.namespace;
15+
return wrappedDebugger;
16+
} : debug;
17+
let out = {
18+
log: d(ns + ':log'),
19+
info: d(ns + ':info'),
20+
warn: d(ns + ':warn'),
21+
error: d(ns + ':error'),
22+
debug: d(ns + ':debug'),
23+
trace: d(ns + ':trace')
24+
};
25+
if (typeof window === 'object' && typeof window.console === 'object') {
26+
try {
27+
out.info.log = console.info.bind(console);
28+
out.warn.log = console.warn.bind(console);
29+
out.error.log = console.error.bind(console);
30+
if (console.debug)
31+
out.debug.log = console.debug.bind(console);
32+
}
33+
catch (e) {
34+
}
35+
}
36+
return out;
37+
}
38+
exports.createLogger = createLogger;
39+
function namespaces() {
40+
return Object.keys(__namespaces);
41+
}
42+
exports.namespaces = namespaces;
43+
function cb(ns = '') {
44+
return (err, data) => {
45+
if (err) {
46+
debug(`${ns}:error`)(err);
47+
}
48+
else {
49+
debug(`${ns}:info`)(data);
50+
}
51+
};
52+
}
53+
exports.cb = cb;
54+
function promise(p, ns = '') {
55+
p.then(function (data) {
56+
debug(`${ns}:info`)(data);
57+
}, function (err) {
58+
debug(`${ns}:error`)(err);
59+
});
60+
}
61+
exports.promise = promise;

src/index.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as debug from 'debug';
2+
3+
const __namespaces: { [ns:string]: boolean } = {};
4+
5+
export interface Logger {
6+
log: debug.IDebugger;
7+
info: debug.IDebugger;
8+
warn: debug.IDebugger;
9+
error: debug.IDebugger;
10+
debug: debug.IDebugger;
11+
trace: debug.IDebugger;
12+
}
13+
14+
export function createLogger(ns: string, context?: string): Logger {
15+
__namespaces[ns] = true;
16+
let d = context ? function(label:string): debug.IDebugger {
17+
let origDebugger:debug.IDebugger = debug(label);
18+
let wrappedDebugger:debug.IDebugger = <debug.IDebugger>function(formatter: string, ...args: any[]) {
19+
origDebugger(`${context} ${formatter}`, ...args);
20+
};
21+
wrappedDebugger.enabled = origDebugger.enabled;
22+
wrappedDebugger.namespace = origDebugger.namespace;
23+
Object.defineProperty(wrappedDebugger, 'log', {
24+
get: () => origDebugger.log,
25+
set: v => origDebugger.log = v
26+
});
27+
return wrappedDebugger;
28+
} : debug;
29+
30+
let out:Logger = {
31+
log: d(ns + ':log'),
32+
info: d(ns + ':info'),
33+
warn: d(ns + ':warn'),
34+
error: d(ns + ':error'),
35+
debug: d(ns + ':debug'),
36+
trace: d(ns + ':trace')
37+
};
38+
39+
if (typeof window === 'object' && typeof window.console === 'object') {
40+
try {
41+
out.info.log = window.console.info.bind(console);
42+
out.warn.log = window.console.warn.bind(console);
43+
out.error.log = window.console.error.bind(console);
44+
if (window.console.debug) {
45+
out.debug.log = window.console.debug.bind(console);
46+
}
47+
} catch (e) {
48+
}
49+
}
50+
51+
return out;
52+
}
53+
54+
export function namespaces() {
55+
return Object.keys(__namespaces);
56+
}
57+
58+
export function cb(ns: string = ''): ((err: any, data?: any) => void) {
59+
const l:Logger = createLogger(ns);
60+
return (err: any, data?: any) => {
61+
if (err) {
62+
l.error(err);
63+
} else {
64+
l.info(data);
65+
}
66+
}
67+
}
68+
69+
export function promise(p:PromiseLike<any>, ns: string = ''): PromiseLike<any> {
70+
const l:Logger = createLogger(ns);
71+
return p.then(function(data) {
72+
l.info(data);
73+
}, function(err) {
74+
l.error(err);
75+
})
76+
}

test/index.test.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import * as chai from 'chai';
2+
import * as spies from 'chai-spies';
3+
import * as debuggo from '../dist/index';
4+
5+
chai.should();
6+
chai.use(spies);
7+
8+
describe('debuggo', function() {
9+
10+
describe('createLogger', function() {
11+
12+
it('create a logger with no context', function() {
13+
let l = debuggo.createLogger('test');
14+
l.log.should.be.a('function');
15+
l.info.should.be.a('function');
16+
l.warn.should.be.a('function');
17+
l.error.should.be.a('function');
18+
l.debug.should.be.a('function');
19+
l.trace.should.be.a('function');
20+
});
21+
22+
it('create a logger with a context', function() {
23+
let l = debuggo.createLogger('test', 'bbb');
24+
l.log.should.be.a('function');
25+
l.info.should.be.a('function');
26+
l.warn.should.be.a('function');
27+
l.error.should.be.a('function');
28+
l.debug.should.be.a('function');
29+
l.trace.should.be.a('function');
30+
});
31+
32+
it('use console.log to log when available', function() {
33+
let w = {
34+
console: {
35+
info: chai.spy(),
36+
warn: chai.spy(),
37+
error: chai.spy()
38+
} as any
39+
};
40+
(<any>global).window = w;
41+
let l1 = debuggo.createLogger('test');
42+
l1.info('aaa');
43+
w.console.info.should.have.been.called.once;
44+
45+
w.console.debug = chai.spy();
46+
let l2 = debuggo.createLogger('test', 'ccc');
47+
l2.debug('aaa');
48+
l2.info('aaa');
49+
l2.info.log;
50+
w.console.debug.should.have.been.called.once;
51+
w.console.info.should.have.been.called.twice;
52+
});
53+
54+
});
55+
56+
describe('namespaces', function() {
57+
58+
it('should return the namespaces', function() {
59+
debuggo.namespaces().should.deep.equal(['test']);
60+
debuggo.createLogger('test2');
61+
debuggo.namespaces().should.deep.equal(['test', 'test2']);
62+
})
63+
64+
});
65+
66+
describe('cb', function() {
67+
68+
it('should return a logging callback', function() {
69+
let w = {
70+
console: {
71+
info: chai.spy(),
72+
warn: chai.spy(),
73+
error: chai.spy()
74+
} as any
75+
};
76+
(<any>global).window = w;
77+
let f = debuggo.cb('test');
78+
f.should.be.a('function');
79+
f(null, 'data');
80+
w.console.info.should.have.been.called.once;
81+
f('error');
82+
w.console.error.should.have.been.called.once;
83+
84+
let f2 = debuggo.cb();
85+
f2.should.be.a('function');
86+
f2(null, 'data');
87+
w.console.info.should.have.been.called.once;
88+
89+
});
90+
91+
});
92+
93+
describe('promise', function() {
94+
95+
it('should log a promise', function() {
96+
let w = {
97+
console: {
98+
info: chai.spy(),
99+
warn: chai.spy(),
100+
error: chai.spy()
101+
} as any
102+
};
103+
(<any>global).window = w;
104+
return debuggo.promise(Promise.resolve(true), 'test').then(() => {
105+
w.console.info.should.have.been.called.once;
106+
return debuggo.promise(Promise.reject(true), 'test').then(() => {
107+
w.console.error.should.have.been.called.once;
108+
return debuggo.promise(Promise.reject(true)).then(() => {
109+
w.console.error.should.have.been.called.once;
110+
});
111+
});
112+
});
113+
})
114+
115+
});
116+
117+
118+
});

0 commit comments

Comments
 (0)