├── .gitattributes ├── .travis.yml ├── .editorconfig ├── .gitignore ├── LICENSE ├── package.json ├── .verb.md ├── test.js ├── .eslintrc.json ├── README.md └── index.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '8' 9 | - '7' 10 | - '6' 11 | - '5' 12 | - '4' 13 | - '0.12' 14 | - '0.10' 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | package-lock.json -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015, 2017, Jon Schlinkert 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "logging-helpers", 3 | "description": "Basic template helpers for printing messages out to the console. Useful for debugging context in templates. Should work with any template engine.", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/helpers/logging-helpers", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "helpers/logging-helpers", 8 | "bugs": { 9 | "url": "https://github.com/helpers/logging-helpers/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=0.10.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "isobject": "^3.0.0", 24 | "log-utils": "^0.2.1" 25 | }, 26 | "devDependencies": { 27 | "engine-base": "^0.1.3", 28 | "engine-handlebars": "^0.8.2", 29 | "gulp-format-md": "^0.1.12", 30 | "handlebars": "^4.0.10", 31 | "mocha": "^3.4.2", 32 | "templates": "^1.2.8" 33 | }, 34 | "keywords": [ 35 | "assemble", 36 | "dash", 37 | "debug", 38 | "docs", 39 | "documentation", 40 | "engine", 41 | "generate", 42 | "generator", 43 | "handlebars", 44 | "helper", 45 | "helpers", 46 | "inspect", 47 | "lo", 48 | "lo-dash", 49 | "lodash", 50 | "logging", 51 | "markdown", 52 | "template", 53 | "templates", 54 | "verb" 55 | ], 56 | "verb": { 57 | "toc": false, 58 | "layout": "default", 59 | "tasks": [ 60 | "readme" 61 | ], 62 | "plugins": [ 63 | "gulp-format-md" 64 | ], 65 | "lint": { 66 | "reflinks": true 67 | }, 68 | "reflinks": [ 69 | "assemble", 70 | "generate", 71 | "handlebars", 72 | "templates", 73 | "update", 74 | "verb" 75 | ], 76 | "related": { 77 | "list": [] 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | The main export is the `log` helper function with other helpers decorated as properties on this function. Some apps, like [assemble][], [verb][] and [generate][] support this format. However, a non-enumberalbe `toObject` method is exposed for registering directly with engines like [handlebars][] 4 | 5 | Example: 6 | 7 | ```js 8 | var loggingHelpers = require('logging-helpers'); 9 | var hbs = require('handlebars'); 10 | 11 | hbs.registerHelper('log', helpers); 12 | // register all of the other helpers 13 | hbs.registerHelper(helpers.toObject); 14 | ``` 15 | 16 | ## Template examples 17 | 18 | Handlebars: 19 | 20 | ```handlebars 21 | {{log "%s" "this is a message"}} 22 | {{info "%s" "this is a message"}} 23 | {{warning "%s" "this is a message"}} 24 | {{warn "%s" "this is a message"}} 25 | {{success "%s" "this is a message"}} 26 | {{error "%s" "this is a message"}} 27 | {{danger "%s" "this is a message"}} 28 | {{ok "%s" "this is a message"}} 29 | {{bold "%s" "this is a message"}} 30 | {{_debug this}} 31 | {{_inspect foo.bar}} 32 | ``` 33 | 34 | Lo-Dash or Underscore: 35 | 36 | ```html 37 | <%%= log("%s", "this is a message") %> 38 | <%%= info("%s", "this is a message") %> 39 | <%%= warn("%s", "this is a message") %> 40 | <%%= warning("%s", "this is a message") %> 41 | <%%= error("%s", "this is a message") %> 42 | <%%= bold("%s", "this is a message") %> 43 | <%%= _debug(data) %> 44 | <%%= _inspect(foo.bar) %> 45 | ``` 46 | 47 | [Verb][] (uses lodash-style templates, but with special delimiters to avoid delimiter collision in documentation): 48 | 49 | ```js 50 | {%%= log("%s", "this is a message") %} 51 | {%%= info("%s", "this is a message") %} 52 | {%%= bold("%s", "this is a message") %} 53 | {%%= warn("%s", "this is a message") %} 54 | {%%= error("%s", "this is a message") %} 55 | {%%= debug(data) %} 56 | {%%= inspect(foo.bar) %} 57 | ``` 58 | 59 | ## Usage with [assemble][] 60 | 61 | This should work with [assemble][], [verb][], [generate][], [update][] or any application based on [templates][]. 62 | 63 | ```js 64 | var helpers = require('{%= name %}'); 65 | var assemble = require('assemble'); 66 | var app = assemble(); 67 | app.helpers(helpers); 68 | ``` 69 | 70 | You can now use the helpers like this: 71 | 72 | ```handlebars 73 | {{log "%s" "this is a message"}} 74 | {{info "%s" "this is a message"}} 75 | {{warning "%s" "this is a message"}} 76 | {{warn "%s" "this is a message"}} 77 | {{success "%s" "this is a message"}} 78 | {{error "%s" "this is a message"}} 79 | {{danger "%s" "this is a message"}} 80 | {{ok "%s" "this is a message"}} 81 | {{bold "%s" "this is a message"}} 82 | {{_debug this}} 83 | {{_inspect foo.bar}} 84 | ``` 85 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var assert = require('assert'); 5 | var handlebars = require('handlebars'); 6 | var isObject = require('isobject'); 7 | var helpers = require('./'); 8 | var error = console.error; 9 | var log = console.log; 10 | 11 | function render(str, ctx) { 12 | var hbs = handlebars.create(); 13 | hbs.registerHelper(helpers); 14 | return hbs.compile(str)(ctx); 15 | } 16 | 17 | describe('logging helpers', function() { 18 | beforeEach(function() { 19 | error.history = []; 20 | log.history = []; 21 | 22 | console.log = function() { 23 | var args = helpers.createArgs([].slice.call(arguments)); 24 | log.apply(console, args); 25 | log.history.push.apply(log.history, args); 26 | }; 27 | 28 | console.error = function() { 29 | var args = helpers.createArgs([].slice.call(arguments)); 30 | error.history.push.apply(error.history, args); 31 | error.apply(console, args); 32 | }; 33 | }); 34 | 35 | afterEach(function() { 36 | console.error = error; 37 | console.log = log; 38 | }); 39 | 40 | describe('{{log}}', function() { 41 | it('should log a message to stdout', function() { 42 | render('{{log "Log helper worked!"}}'); 43 | assert(/Log helper worked!/.test(log.history.join(''))); 44 | }); 45 | }); 46 | 47 | describe('{{warn}}', function() { 48 | it('should log a warning message to stdout', function() { 49 | render('{{warn "warn helper worked!"}}'); 50 | assert(/warn helper worked!/.test(error.history.join(''))); 51 | }); 52 | }); 53 | 54 | describe('{{success}}', function() { 55 | it('should log a success message to stdout', function() { 56 | render('{{success "success helper worked!"}}'); 57 | assert(/success helper worked!/.test(log.history.join(''))); 58 | }); 59 | }); 60 | 61 | describe('{{ok}}', function() { 62 | it('should log an "ok" message to stdout', function() { 63 | render('{{ok "ok helper worked!"}}'); 64 | assert(/ok helper worked!/.test(log.history.join(''))); 65 | }); 66 | }); 67 | 68 | describe('{{info}}', function() { 69 | it('should log an info message to stdout', function() { 70 | render('{{info "info helper worked!"}}'); 71 | assert(/info helper worked!/.test(log.history.join(''))); 72 | }); 73 | }); 74 | 75 | describe('{{error}}', function() { 76 | it('should log an error message to stdout', function() { 77 | render('{{error "error helper worked!"}}'); 78 | assert(/error helper worked!/.test(error.history.join(''))); 79 | }); 80 | }); 81 | 82 | describe('{{_debug}}', function() { 83 | it('should log current context to stderr', function() { 84 | render('{{_debug this}}', 'foo'); 85 | assert(/foo/.test(error.history.join(''))); 86 | }); 87 | }); 88 | }); 89 | 90 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # logging-helpers [![NPM version](https://img.shields.io/npm/v/logging-helpers.svg?style=flat)](https://www.npmjs.com/package/logging-helpers) [![NPM monthly downloads](https://img.shields.io/npm/dm/logging-helpers.svg?style=flat)](https://npmjs.org/package/logging-helpers) [![NPM total downloads](https://img.shields.io/npm/dt/logging-helpers.svg?style=flat)](https://npmjs.org/package/logging-helpers) [![Linux Build Status](https://img.shields.io/travis/helpers/logging-helpers.svg?style=flat&label=Travis)](https://travis-ci.org/helpers/logging-helpers) 2 | 3 | > Basic template helpers for printing messages out to the console. Useful for debugging context in templates. Should work with any template engine. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save logging-helpers 11 | ``` 12 | 13 | ## Usage 14 | 15 | The main export is the `log` helper function with other helpers decorated as properties on this function. Some apps, like [assemble](https://github.com/assemble/assemble), [verb](https://github.com/verbose/verb) and [generate](https://github.com/generate/generate) support this format. However, a non-enumberalbe `toObject` method is exposed for registering directly with engines like [handlebars](http://www.handlebarsjs.com/) 16 | 17 | Example: 18 | 19 | ```js 20 | var loggingHelpers = require('logging-helpers'); 21 | var hbs = require('handlebars'); 22 | 23 | hbs.registerHelper('log', helpers); 24 | // register all of the other helpers 25 | hbs.registerHelper(helpers.toObject); 26 | ``` 27 | 28 | ## Template examples 29 | 30 | Handlebars: 31 | 32 | ```handlebars 33 | {{log "%s" "this is a message"}} 34 | {{info "%s" "this is a message"}} 35 | {{warning "%s" "this is a message"}} 36 | {{warn "%s" "this is a message"}} 37 | {{success "%s" "this is a message"}} 38 | {{error "%s" "this is a message"}} 39 | {{danger "%s" "this is a message"}} 40 | {{ok "%s" "this is a message"}} 41 | {{bold "%s" "this is a message"}} 42 | {{_debug this}} 43 | {{_inspect foo.bar}} 44 | ``` 45 | 46 | Lo-Dash or Underscore: 47 | 48 | ```html 49 | <%= log("%s", "this is a message") %> 50 | <%= info("%s", "this is a message") %> 51 | <%= warn("%s", "this is a message") %> 52 | <%= warning("%s", "this is a message") %> 53 | <%= error("%s", "this is a message") %> 54 | <%= bold("%s", "this is a message") %> 55 | <%= _debug(data) %> 56 | <%= _inspect(foo.bar) %> 57 | ``` 58 | 59 | [Verb](https://github.com/verbose/verb) (uses lodash-style templates, but with special delimiters to avoid delimiter collision in documentation): 60 | 61 | ```js 62 | {%= log("%s", "this is a message") %} 63 | {%= info("%s", "this is a message") %} 64 | {%= bold("%s", "this is a message") %} 65 | {%= warn("%s", "this is a message") %} 66 | {%= error("%s", "this is a message") %} 67 | {%= debug(data) %} 68 | {%= inspect(foo.bar) %} 69 | ``` 70 | 71 | ## Usage with [assemble](https://github.com/assemble/assemble) 72 | 73 | This should work with [assemble](https://github.com/assemble/assemble), [verb](https://github.com/verbose/verb), [generate](https://github.com/generate/generate), [update](https://github.com/update/update) or any application based on [templates](https://github.com/jonschlinkert/templates). 74 | 75 | ```js 76 | var helpers = require('logging-helpers'); 77 | var assemble = require('assemble'); 78 | var app = assemble(); 79 | app.helpers(helpers); 80 | ``` 81 | 82 | You can now use the helpers like this: 83 | 84 | ```handlebars 85 | {{log "%s" "this is a message"}} 86 | {{info "%s" "this is a message"}} 87 | {{warning "%s" "this is a message"}} 88 | {{warn "%s" "this is a message"}} 89 | {{success "%s" "this is a message"}} 90 | {{error "%s" "this is a message"}} 91 | {{danger "%s" "this is a message"}} 92 | {{ok "%s" "this is a message"}} 93 | {{bold "%s" "this is a message"}} 94 | {{_debug this}} 95 | {{_inspect foo.bar}} 96 | ``` 97 | 98 | ## About 99 | 100 | ### Contributing 101 | 102 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 103 | 104 | ### Building docs 105 | 106 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 107 | 108 | To generate the readme, run the following command: 109 | 110 | ```sh 111 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 112 | ``` 113 | 114 | ### Running tests 115 | 116 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 117 | 118 | ```sh 119 | $ npm install && npm test 120 | ``` 121 | 122 | ### Author 123 | 124 | **Jon Schlinkert** 125 | 126 | * [github/jonschlinkert](https://github.com/jonschlinkert) 127 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 128 | 129 | ### License 130 | 131 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 132 | Released under the [MIT License](LICENSE). 133 | 134 | *** 135 | 136 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 21, 2017._ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var isObject = require('isobject'); 4 | var utils = require('log-utils'); 5 | var helpers = {}; 6 | 7 | /** 8 | * Helper for logging an unstyled message to the terminal. 9 | * 10 | * ```js 11 | * // Lo-Dash 12 | * <%= log("Foo:", someVariable) %> 13 | * <%= log("%j", foo) %> 14 | * 15 | * // Handlebars 16 | * {{log "this is a message!"}} 17 | * {{log page}} 18 | * ``` 19 | * @return {undefined} 20 | * @api public 21 | */ 22 | 23 | helpers.log = function() { 24 | var args = createArgs([].slice.call(arguments)); 25 | console.log.apply(console, args); 26 | }; 27 | 28 | /** 29 | * Helper for logging a green colored "ok" message preceded by a checkmark 30 | * to the terminal. 31 | * 32 | * ```js 33 | * // Lo-Dash 34 | * <%= ok("stop!") %> 35 | * <%= ok("%j", {some: 'value'}) %> 36 | * 37 | * // Handlebars 38 | * {{ok "this is a message!"}} 39 | * ``` 40 | * @return {undefined} 41 | * @api public 42 | */ 43 | 44 | helpers.ok = function() { 45 | var args = createArgs([].slice.call(arguments)); 46 | utils.ok.apply(utils.ok, args); 47 | }; 48 | 49 | /** 50 | * Helper for logging a green colored "success" message to the terminal. 51 | * 52 | * ```js 53 | * // Lo-Dash 54 | * <%= success("stop!") %> 55 | * <%= success("%j", {some: 'value'}) %> 56 | * 57 | * // Handlebars 58 | * {{success "this is a message!"}} 59 | * ``` 60 | * @return {undefined} 61 | * @api public 62 | */ 63 | 64 | helpers.success = function() { 65 | var args = createArgs([].slice.call(arguments), 'green'); 66 | console.log.apply(console, args); 67 | }; 68 | 69 | /** 70 | * Helper for logging a cyan colored "informational" message to the terminal. 71 | * 72 | * ```js 73 | * // Lo-Dash 74 | * <%= info("this is a message!") %> 75 | * <%= info("%j", foo) %> 76 | * 77 | * // Handlebars 78 | * {{info "this is a message!"}} 79 | * ``` 80 | * @return {undefined} 81 | * @api public 82 | */ 83 | 84 | helpers.info = function() { 85 | var args = createArgs([].slice.call(arguments), 'cyan'); 86 | console.log.apply(console, args); 87 | }; 88 | 89 | /** 90 | * Helper for logging a yellow colored "warning" message to the terminal. 91 | * 92 | * ```js 93 | * // Lo-Dash 94 | * <%= warn("this is a message!") %> 95 | * <%= warn("%j", foo) %> 96 | * 97 | * // Handlebars 98 | * {{warn "this is a message!"}} 99 | * ``` 100 | * @return {undefined} 101 | * @api public 102 | */ 103 | 104 | helpers.warning = function() { 105 | var args = createArgs([].slice.call(arguments), 'yellow'); 106 | console.error.apply(console, args); 107 | }; 108 | 109 | /** 110 | * Alias for [warning](#warning) helper. 111 | * 112 | * ```js 113 | * // Lo-Dash 114 | * <%= warn("this is a message!") %> 115 | * <%= warn("%j", foo) %> 116 | * 117 | * // Handlebars 118 | * {{warn "this is a message!"}} 119 | * ``` 120 | * @return {undefined} 121 | * @api public 122 | */ 123 | 124 | helpers.warn = helpers.warning; 125 | 126 | /** 127 | * Helper for logging a red colored "error" message to the terminal. 128 | * 129 | * ```js 130 | * // Lo-Dash 131 | * <%= error("stop!") %> 132 | * <%= error("%j", {some: 'value'}) %> 133 | * 134 | * // Handlebars 135 | * {{error "this is a message!"}} 136 | * ``` 137 | * @return {undefined} 138 | * @api public 139 | */ 140 | 141 | helpers.error = function() { 142 | var args = createArgs([].slice.call(arguments), 'red'); 143 | console.error.apply(console, args); 144 | }; 145 | 146 | /** 147 | * Helper for logging a red colored "danger" message to the terminal. 148 | * 149 | * ```js 150 | * // Lo-Dash 151 | * <%= danger("this is a message!") %> 152 | * <%= danger("%j", foo) %> 153 | * 154 | * // Handlebars 155 | * {{danger "this is a message!"}} 156 | * ``` 157 | * @return {undefined} 158 | * @api public 159 | */ 160 | 161 | helpers.danger = helpers.error; 162 | 163 | /** 164 | * Helper for logging a bold colored message to the terminal. 165 | * 166 | * ```js 167 | * // Lo-Dash 168 | * <%= bold("this is a message!") %> 169 | * <%= bold("%j", foo) %> 170 | * 171 | * // Handlebars 172 | * {{bold "this is a message!"}} 173 | * ``` 174 | * @return {undefined} 175 | * @api public 176 | */ 177 | 178 | helpers.bold = function() { 179 | var args = createArgs([].slice.call(arguments), 'bold'); 180 | console.error.apply(console, args); 181 | }; 182 | 183 | /** 184 | * Outputs a debug statement with the current context, 185 | * and/or `val` 186 | * 187 | * ```js 188 | * // Lo-Dash 189 | * <%= _debug("this is a message!") %> 190 | * <%= _debug("%j", foo) %> 191 | * 192 | * // Handlebars 193 | * {{_debug "this is a message!"}} 194 | * ``` 195 | * @return {undefined} 196 | * @api public 197 | */ 198 | 199 | helpers._debug = function(val) { 200 | var args = createArgs([].slice.call(arguments)); 201 | if (val && args.length > 0) { 202 | console.error(); 203 | console.error('================================='); 204 | console.error('context: %j', this); 205 | console.error(); 206 | console.error.apply(console, ['value: %j'].concat(args)); 207 | console.error('================================='); 208 | console.error(); 209 | } 210 | }; 211 | 212 | /** 213 | * Returns stringified JSON, wrapped in a gfm codeblock, html `
` tags,
214 |  * or unchanged, based on the `type` passed on the context.
215 |  *
216 |  * ```js
217 |  * // Lo-Dash
218 |  * <%= _inspect(obj, {type: }) %>
219 |  * <%= _inspect("%j", obj) %>
220 |  *
221 |  * // Handlebars
222 |  * {{_inspect this type="html"}}
223 |  * ```
224 |  * @return {undefined}
225 |  * @api public
226 |  */
227 | 
228 | helpers._inspect = function(context, options) {
229 |   var val = JSON.stringify(context, null, 2);
230 |   var type = options && options.hash && options.hash.type || 'html';
231 |   return switchOutput(type, val);
232 | };
233 | 
234 | /**
235 |  * Remove handlebars options from the arguments if more than one argument
236 |  * is passed, and apply a color to the first argument if specified
237 |  */
238 | 
239 | function createArgs(args, color) {
240 |   var last = args[args.length - 1];
241 |   if (args.length > 1 && isObject(last && last.hash)) {
242 |     args.pop();
243 |   }
244 |   if (typeof color === 'string' && utils[color]) {
245 |     args[0] = utils[color](args[0]);
246 |   }
247 |   return args;
248 | }
249 | 
250 | /**
251 |  * Generate output for the `_inspect` helper based on the
252 |  * given `type`
253 |  */
254 | 
255 | function switchOutput(type, json) {
256 |   if (type[0] === '.') type = type.slice(1);
257 |   var result = '';
258 | 
259 |   switch (type) {
260 |     case 'md':
261 |       result = ''
262 |         + '\n```json\n'
263 |         + json
264 |         + '\n```\n';
265 |       break;
266 |     case 'html':
267 |       result = ''
268 |         + '
\n' 269 | + '
\n'
270 |         + json
271 |         + '
' 272 | + '
'; 273 | break; 274 | default: { 275 | result = json; 276 | break; 277 | } 278 | } 279 | return result; 280 | } 281 | 282 | /** 283 | * Expose `.createArgs` as a non-enumberable method for unit tests 284 | */ 285 | 286 | Object.defineProperty(helpers, 'createArgs', { 287 | enumerable: false, 288 | configurable: false, 289 | get: function() { 290 | return createArgs; 291 | } 292 | }); 293 | 294 | /** 295 | * Expose `.group` as a non-enumberable method for creating a helper group. 296 | * This is not supported by handlebars unfortunately 297 | */ 298 | 299 | Object.defineProperty(helpers, 'group', { 300 | enumerable: false, 301 | configurable: false, 302 | get: function() { 303 | var obj = {}; 304 | for (var key in helpers) { 305 | if (helpers.hasOwnProperty(key)) { 306 | if (key === 'log') { 307 | obj[key] = helpers[key]; 308 | } else { 309 | obj['log.' + key] = helpers[key]; 310 | } 311 | } 312 | } 313 | return obj; 314 | } 315 | }); 316 | 317 | /** 318 | * Expose `helpers` 319 | * @type {Object} 320 | */ 321 | 322 | module.exports = helpers; 323 | --------------------------------------------------------------------------------