Skip to content

Commit

Permalink
first delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienv1520 committed Aug 30, 2020
0 parents commit d201276
Show file tree
Hide file tree
Showing 31 changed files with 3,133 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "airbnb-base"
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
npm-debug.log
*.DS_Store
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
save-prefix='~'
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# CHANGELOG

## 1.0.0 - delivery @30/08/2020

- first delivery
200 changes: 200 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<p align="center">
<img src="doc/bugbug.png" title="bugbug" alt="bugbug"/>
<p>

<p align="center">
A lightweight, zero-dependencies, asynchronous and <strong>fully-tested</strong> pure Node.js debugger.
<p>

# Table of Contents
- [Presentation](#presentation)
- [Installation](#installation)
- [Technical information](#technical-information)
- [Node.js](#nodejs)
- [Tests](#tests)
- [Linting](#linting)
- [Unit](#unit)
- [Usage](#usage)
- [Library](#library)
- [Import bugbug](#import-bugbug)
- [getDebugger(name, color)](#getdebuggername-color)
- [Examples](#examples)
- [Environment variable](#environment-variable)
- [Examples](#examples-1)
- [Development](#development)
- [Licence](#licence)

# Presentation

*bugbug* is a lightweight, zero-dependencies, asynchronous and **fully-tested** pure Node.js debugger. It writes on *process.stderr* when the *DEBUG* environment variable is set and match a specific module's name/pattern and uses a specified/random output color if the terminal allows it.

The aim of this project is to provide a very simple, lightweight, zero-dependency, fast and **fully-tested** debugger.

# Installation

`npm install bugbug`

`npm i -S bugbug`

# Technical information

## Node.js

- Language: JavaScript ES6/ES7
- VM: Node.js >=10.0.0

## Tests

Command to run all tests:

`npm test`

### Linting

ESLint with Airbnb base rules. See [Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript).

`npm run test:lint`

### Unit

Mocha and Chai.

`npm run test:unit`

# Usage

## Library

### Import bugbug

*bugbug* module exports one function named *getDebugger*. This function once called will return the debug function related to a specific debugger's name that will allow to debug on *stderr*.

```javascript
// first get the getDebugger function
const getDebugger = require('bugbug');

// then get the debug function from a stored or newly created debugger
const debug = getDebugger('my-module', 'red');

// OR faster way to get the debug function
const debug = require('bugbug')('my-module', 'red');
```

### getDebugger(name, color)

- `name` **<String\>** The debugger's name. Generally the module's name to debug. *Default*: `debug`
- `color` **<String\>** The debugging color if the terminal allows it. *Default*: `red` for the default debugger or a `random` color
- `red`
- `green`
- `yellow`
- `blue`
- `magenta`
- `cyan`
- `lightRed`
- `lightGreen`
- `lightYellow`
- `lightBlue`
- `lightMagenta`
- `lightCyan`


- Returns: **<Function\>** Function called *debug* to finally debug in the name of the related module.
- Throws: **NO**

### Examples

You can find examples here: [doc/examples](doc/examples)

#### Example 1
```javascript
const debug = require('bugbug')('my-module', 'red');

debug('debugging');
debug('still debugging');
```

![Example 1](doc/examples/example1/example1.png "Example 1")

#### Example 2
```javascript
const debug = require('bugbug')('node-sparkline', 'green');

const options = {
values: [1, 2, 3, 4, 5],
width: 500,
height: 500,
};

debug('everything\'s fine');
debug('process done');
debug('sparkline generated in SVG format,', 'additional stuff');
debug('options:', options);
```

![Example 2](doc/examples/example2/example2.png "Example 2")

#### Example 3
```javascript
const debug = require('bugbug')();

const options = {
values: [1, 2, 3, 4, 5],
width: 500,
height: 500,
};

debug('main debugger is debugging');
debug('in red color if terminal allows it');
debug('options:', options);
```

![Example 3](doc/examples/example3/example3.png "Example 3")

## Environment variable

**DEBUG** is used to whether debug a specific module. *DEBUG* value can be a **comma-separated string** listing module names to debug or to avoid debugging. Format is: `DEBUG=moduleName[,moduleName]`
- `DEBUG=moduleName` will debug *moduleName* module;
- `DEBUG=moduleName:*` will debug *moduleName* module and sub modules;
- `DEBUG=-moduleName:*` will disable debugging any *moduleName* module and sub modules;
- `DEBUG=*` will debug all *moduleName* module and sub modules plus other modules used in your project if they use **bugbug** or an equivalent debugger based on *process.env.DEBUG* value.

### Examples

#### DEBUG=*
![Example 4-1](doc/examples/example4/example4-1.png "Example 4-1")

#### DEBUG=gzip
![Example 4-2](doc/examples/example4/example4-2.png "Example 4-2")

#### DEBUG=gzip:compression
![Example 4-3](doc/examples/example4/example4-3.png "Example 4-3")

#### DEBUG=-gzip:compression
![Example 4-4](doc/examples/example4/example4-4.png "Example 4-4")

#### DEBUG=gzip:*,-gzip:compression
![Example 4-5](doc/examples/example4/example4-5.png "Example 4-5")

#### DEBUG=*,-gzip:compression
![Example 4-6](doc/examples/example4/example4-6.png "Example 4-6")

#### DEBUG=\*,-gzip:*
![Example 4-7](doc/examples/example4/example4-7.png "Example 4-7")

# Development

All contributions are greatly welcome :)

Please follow Git flow, ES6/7, ESLint Airbnb base rules.

# Licence

*bugbug* is released under the MIT license.

Copyright (C) 2020 Adrien Valcke

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Binary file added doc/bugbug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/examples/example1/example1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions doc/examples/example1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const debug = require('../../../lib')('my-module', 'red');

debug('debugging');
debug('still debugging');
Binary file added doc/examples/example2/example2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions doc/examples/example2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const debug = require('../../../lib')('node-sparkline', 'green');

const options = {
values: [1, 2, 3, 4, 5],
width: 500,
height: 500,
};

debug('everything\'s fine');
debug('process done');
debug('sparkline generated in SVG format,', 'additional stuff');
debug('options:', options);
Binary file added doc/examples/example3/example3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions doc/examples/example3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const debug = require('../../../lib')();

const options = {
values: [1, 2, 3, 4, 5],
width: 500,
height: 500,
};

debug('main debugger is debugging');
debug('in red color if terminal allows it');
debug('options:', options);
Binary file added doc/examples/example4/example4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/examples/example4/example4-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/examples/example4/example4-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/examples/example4/example4-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/examples/example4/example4-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/examples/example4/example4-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/examples/example4/example4-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions doc/examples/example4/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const getDebugger = require('../../../lib');

const debugHttp = getDebugger('http', 'green');
const debugGzip = getDebugger('gzip', 'lightMagenta');
const debugGzipCompression = getDebugger('gzip:compression', 'cyan');
const debugGzipHelper = getDebugger('gzip:helper', 'yellow');

debugHttp('debugging http');
debugGzip('gzip compression...');
debugGzip('compressing file');
debugGzipCompression('data being processed');
debugGzipHelper('data successfully parsed');
debugGzipCompression('data compressed');
debugGzip('gzip compression done');
123 changes: 123 additions & 0 deletions lib/Debugger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* Debugger
*
* - name: String
* - color: String
* - canDebug: Boolean
* - previousTime: Number
* - timeBetween: Number
*
* - constructor(name, color)
* - setName(name) -> undefined | throw Error
* - setColor(color) -> undefined
* - setCanDebug() -> undefined
* - getOutput(...args) -> String
* - updateTimeBetweenTwoDebugs() -> undefined
* - debug(...args) -> undefined
*/

const { isatty } = require('tty');
const { format } = require('util');
const { is, hasOwn } = require('./helpers');
const {
colors,
restrictedColors,
set,
reset,
} = require('./font');

class Debugger {
constructor(name, color) {
this.setName(name);
this.setColor(color);
this.setCanDebug();
}

setName(name) {
if (is(String, name) && name.trim() !== '') {
this.name = name;
} else {
throw new Error('bad debugger name');
}
}

setColor(color) {
if (hasOwn(colors, color)) {
this.color = colors[color];
} else {
const colorNames = Object.keys(colors);

this.color = colors[colorNames[Math.floor(Math.random() * colorNames.length)]];
}
}

// no hot reload in Node.js so set the variable when creating object instance
setCanDebug() {
const envDebug = process.env.DEBUG;
this.canDebug = false;

if (is(String, envDebug)) {
const modules = envDebug.split(/\s*,\s*/);
const modulesLen = modules.length;

for (let i = 0; i < modulesLen; i += 1) {
if (modules[i] === '*') {
this.canDebug = true;
} else {
const [name, subModule] = this.name.split(':');
const [debugName, debugSubModule] = modules[i].split(':');

if (debugName === `-${name}` && (debugSubModule === subModule || debugSubModule === '*')) {
this.canDebug = false;
break;
} else if (modules[i] === `${name}:*` || modules[i] === this.name) {
this.canDebug = true;
}
}
}
}
}

getOutput(...args) {
const formatedArgs = format(...args);
let output = '';

// prefix, content and time between two calls (determined when debug is called)
if (isatty(process.stderr.fd)) {
output += `${set.bold}${this.color}${this.name}${reset.all}`;
output += ` ${restrictedColors.darkGray}${formatedArgs}${reset.all}`;
output += ` ${set.bold}${this.color}+${this.timeBetween}ms${reset.all}`;
} else {
output += `${new Date().toISOString()} ${this.name}`;
output += ` ${formatedArgs}`;
output += ` +${this.timeBetween}ms`;
}

// end
output += '\n';

return output;
}

updateTimeBetweenTwoDebugs() {
// calculate time between two debug calls
const currentTime = Date.now();

if (this.previousTime === undefined) {
this.previousTime = currentTime;
}

this.timeBetween = currentTime - this.previousTime;
this.previousTime = currentTime;
}

debug(...args) {
this.updateTimeBetweenTwoDebugs();

if (this.canDebug) {
process.stderr.write(`${this.getOutput(...args)}`);
}
}
}

module.exports = Debugger;
Loading

0 comments on commit d201276

Please sign in to comment.