This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
122 lines (105 loc) · 3.59 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* This file is part of the chalkbars npm package. Copyright (C) 2015 and above Shogun <shogun@cowtech.it>.
* Licensed under the MIT license, which can be found at https://choosealicense.com/licenses/mit.
*/
const chalk = require("chalk");
const templating = require("./lib/templating");
const configuration = require("./lib/configuration");
const style = require("./lib/functions").manageStyle;
/**
* Compiles a Handlebars templates and then applies chalk colors.
*
* @alias module:chalkbars.format
* @param {...string} template - The template to compile. You can specify more than one string, they will be concatenated.
* @param {object} context - The context for the Handlebars template.
* @returns {string} The compiled template with color styles applied.
*/
const format = function(){
"use strict";
const args = Array.prototype.slice.call(arguments, 0);
if(!args.length)
return "";
// Get the last element and check whether is a object.
let context = args.pop();
if(!context || typeof context !== "object"){
args.push(context);
context = {};
}
// Perform the handlebars compilation
try{
return templating.renderTemplate(args, context);
}catch(e){
// If it fails we return the raw source or raise an error
if(module.exports.configuration.silent)
return args.join("");
throw e;
}
};
/**
* Chalkbars module.
*
* @module chalkbars
*/
module.exports = {
/**
* The console library used by chalkbars.
*
* @type {object}
*/
chalk: templating.chalk,
/**
* The templating library used by chalkbars.
*
* @type {object}
*/
handlebars: templating.handlebars,
configuration,
style,
format,
/**
* Compiles a Handlebars templates and then it strips out all ANSI color sequences.
* {@see format}
*
* @param {...string} template - The template to compile. You can specify more than one string, they will be concatenated.
* @param {object} context - The context for the Handlebars template.
* @returns {string} The compiled template with color styles removed.
*/
formatNoColor(){
const args = Array.prototype.slice.call(arguments, 0);
return format.apply(this, args).replace(/\u001b\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]/g, "");
},
/**
* Compiles a Handlebars templates and then it strips out all ANSI escape sequences.
* {@see format}
*
* @param {...string} template - The template to compile. You can specify more than one string, they will be concatenated.
* @param {object} context - The context for the Handlebars template.
* @returns {string} The compiled template with ANSI escape sequences removed.
*/
plainFormat(){
const args = Array.prototype.slice.call(arguments, 0);
return chalk.stripColor(format.apply(this, args));
},
/**
* Compiles a Handlebars templates and then outputs it to the console.
* {@see format}
*
* @param {...string} template - The template to compile. You can specify more than one string, they will be concatenated.
* @param {object} context - The context for the Handlebars template.
*/
log(){
const args = Array.prototype.slice.call(arguments, 0);
console.log(format.apply(this, args));
},
/**
* Compiles a Handlebars templates and then outputs it to the console as error.
* {@see format}
*
* @param {...string} template - The template to compile. You can specify more than one string, they will be concatenated.
* @param {object} context - The context for the Handlebars template.
*/
error(){
const args = Array.prototype.slice.call(arguments, 0);
console.error(format.apply(this, args));
}
};