Skip to content

Commit

Permalink
feat: [master]: add param for using in specific builds/envs
Browse files Browse the repository at this point in the history
  • Loading branch information
LordotU committed Jan 11, 2019
1 parent 80ea0a8 commit abd9d2e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 32 deletions.
49 changes: 30 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,45 @@ yarn test # yarn test:jest && yarn test:coverage
You may decorate your class with default styles params, which are:

```javascript
{
styles: {
browser: {
invocation: 'background: #F2EAFF; color: #03A9F4; font-weight: bold',
result: 'background: #F2EAFF; color: #4CAF50; font-weight: bold',
error: 'background: #F2EAFF; color: #F20404; font-weight: bold',
},
node: {
invocation: 'bgBlack.blue.bold',
result: 'bgBlack.green.bold',
error: 'bgBlack.red.bold',
}
{
styles: {
browser: {
invocation: 'background: #F2EAFF; color: #03A9F4; font-weight: bold',
result: 'background: #F2EAFF; color: #4CAF50; font-weight: bold',
error: 'background: #F2EAFF; color: #F20404; font-weight: bold',
},
node: {
invocation: 'bgBlack.blue.bold',
result: 'bgBlack.green.bold',
error: 'bgBlack.red.bold',
}
}
}
```

Or set your own styles by passing object as a first argument of decorator, for example:

```javascript
@Cmild({
styles: {
browser: {
invocation: 'background: #F2EAFF; color: #FFEF00; font-weight: bold',
}
},
})
@Cmild({
styles: {
browser: {
invocation: 'background: #F2EAFF; color: #FFEF00; font-weight: bold',
}
},
})
```

Also you may disable decorator in specific env/build, for example:

```javascript
@Cmild({
enabled: process.env.NODE_ENV !== production,
})
```

Common usage:


```javascript
import Cmild from 'cmild'

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cmild",
"version": "1.0.1",
"version": "1.1.1",
"description": "Class methods invocation logging decorator",
"files": [
"build"
Expand Down
24 changes: 18 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function cmildOuter (param = {}) {
* Class methods invocation logging decorator inner
*
* @param {Object} [options.styles={}]
* @param {String} [options.enabled=true]
* @param {Object} [options.styles.browser={}]
* @param {String} [options.styles.browser.invocation='background: #F2EAFF; color: #03A9F4; font-weight: bold']
* @param {String} [options.styles.browser.result='background: #F2EAFF; color: #4CAF50; font-weight: bold']
Expand All @@ -28,17 +29,28 @@ export default function cmildOuter (param = {}) {
* @param {String} [options.styles.node.error='bgBlack.red.bold']
* @param {Function} target
*/
function cmildInner (options = {}, target) {
function cmildInner ({
enabled = true,
styles = {
browser: {
invocation: 'background: #F2EAFF; color: #03A9F4; font-weight: bold',
result: 'background: #F2EAFF; color: #4CAF50; font-weight: bold',
error: 'background: #F2EAFF; color: #F20404; font-weight: bold',
},
node: {
invocation: 'bgBlack.blue.bold',
result: 'bgBlack.green.bold',
error: 'bgBlack.red.bold',
},
},
} = {}, target) {

if (typeof target !== 'function') {
throw TypeError('`target` decorator param should be a class function!')
}

const styles = {
browser: {},
node: {},

...options.styles,
if (! enabled) {
return
}

for (const propertyName of Reflect.ownKeys(target.prototype)) {
Expand Down
8 changes: 6 additions & 2 deletions src/loggers/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ export default function logError (classMethod, styles, error) {
if (process.browser) {
console.debug(
`%c ${classMethod} error: %O`,
typeof styles === 'string' && !! styles ? styles : 'background: #F2EAFF; color: #F20404; font-weight: bold',
styles,
error,
)
} else {
const colors = require('colors/safe')
const _get = require('lodash.get')
const _styles = _get(colors, styles)

if (typeof _styles !== 'function') {
throw TypeError(`Cannot find given style chain '${styles}' in colors.js!`)
}

console.debug(
`${(_styles || colors.bgBlack.red.bold)(`${classMethod} error:`)} %O`,
`${_styles(`${classMethod} error:`)} %O`,
error,
)
}
Expand Down
8 changes: 6 additions & 2 deletions src/loggers/invocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ export default function logInvocation (classMethod, styles, ...args) {
if (process.browser) {
console.debug(
`%c ${classMethod} given arguments: %O`,
typeof styles === 'string' && !! styles ? styles : 'background: #F2EAFF; color: #03A9F4; font-weight: bold',
styles,
[...args],
)
} else {
const colors = require('colors/safe')
const _get = require('lodash.get')
const _styles = _get(colors, styles)

if (typeof _styles !== 'function') {
throw TypeError(`Cannot find given style chain '${styles}' in colors.js!`)
}

console.debug(
`${(_styles || colors.bgBlack.blue.bold)(`${classMethod} arguments:`)} %O`,
`${_styles(`${classMethod} arguments:`)} %O`,
[...args],
)
}
Expand Down
8 changes: 6 additions & 2 deletions src/loggers/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ export default function logResult (classMethod, styles, result) {
if (process.browser) {
console.debug(
`%c ${classMethod} result: %O`,
typeof styles === 'string' && !! styles ? styles : 'background: #F2EAFF; color: #4CAF50; font-weight: bold',
styles,
result,
)
} else {
const colors = require('colors/safe')
const _get = require('lodash.get')
const _styles = _get(colors, styles)

if (typeof _styles !== 'function') {
throw TypeError(`Cannot find given style chain '${styles}' in colors.js!`)
}

console.debug(
`${(_styles || colors.bgBlack.green.bold)(`${classMethod} result:`)} %O`,
`${_styles(`${classMethod} result:`)} %O`,
result,
)
}
Expand Down

0 comments on commit abd9d2e

Please sign in to comment.