-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (157 loc) · 4.5 KB
/
index.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
'use strict';
const program = require('commander');
const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');
const fs = require('fs');
const Handlebars = require('handlebars');
const CliFrames = require("cli-frames");
var data = JSON.parse(fs.readFileSync('me.json', 'utf8'));
program
.version('1.0.0')
.description('Prints out my professional contact card in different formats.')
.option('--no-color', 'Suppress color output')
.option('--json', 'Output json')
.option('--html', 'Output html')
.option('--text', 'Output text (default)')
.option('--no-animation', 'Suppress the animation')
.on('json', () => { program.outputType = 'json'} )
.on('html', () => { program.outputType = 'html'} )
.on('text', () => { program.outputType = 'text'} )
.parse(process.argv);
if (program.animation) {
animateName(data, program, function() {
clear();
printBio(data, program);
process.exit();
});
} else {
printBio(data, program);
process.exit();
}
/**
* Prints out the bio information
*
* @method printBio
* @param {Object} data Contact data
* @param {Object} opts cli options
*/
function printBio(data, opts) {
var document;
switch(opts.outputType) {
case "json":
document = toJson(data, opts);
break;
case "html":
document = toHtml(data, opts);
break;
case "text":
document = toText(data, opts);
break;
default:
if(typeof(opts.type) !== 'undefined') {
process.stdout.write("Unrecognized type, use one of: --json, --html, --text");
process.exit();
} else {
document = toText(data, opts);
}
}
process.stdout.write(document);
}
/**
* Factory method to generate a handlebars helper function
* that chunks groups of n items separated by the join
* characters in the output template.
*
* @method chunkerFactory
* @param {Number} chunk Number of elements to chunk.
* @param {String} join Characters used to join the elements in a chunk.
* @return {Function} Function that will be suitable as as
*/
function chunkerFactory(chunk, join) {
return (context, options) => {
var ret = "";
for(var i = 0, l = context.length; i < l; i += chunk) {
var line = context.slice(i, i + chunk).join(join);
ret = ret + options.fn(line);
}
return ret;
}
}
/**
* Generates the data as JSON
*
* @method toJson
* @param {Object} data Contact data
* @param {Object} opts cli options
* @return {String} formatted document
*/
function toJson(data, opts) {
return JSON.stringify(data);
}
/**
* Generates the text version of the data
*
* @method toText
* @param {Object} data Contact data
* @param {Object} opts cli options
* @return {String} formatted document
*/
function toText(data, opts) {
Handlebars.registerHelper('each5', chunkerFactory(5, ', '));
Handlebars.registerHelper('each10', chunkerFactory(10, ', '));
var template = Handlebars.compile(
fs.readFileSync('me.txt.hb', 'utf8')
);
var title = figlet.textSync(data.name, { horizontalLayout: 'full' });
if (opts.color) {
title = chalk.green(title);
}
var doc = template(data);
return title + "\n" + doc + "\n";
}
/**
* Generates the html version of the data
*
* @method toHtml
* @param {Object} data Contact data
* @param {Object} opts cli options
* @return {String} formatted document
*/
function toHtml(data, opts) {
var template = Handlebars.compile(
fs.readFileSync('me.html.hb', 'utf8')
);
var doc = template(data);
return doc + "\n";
}
/**
* Animate the name at startup.
*
* @method animateName
* @param {Object} data Contact data
* @param {Object} opts cli options
* @param {Function} [callback] callback function to run after the animation.
*/
function animateName(data, opts, callback) {
var frames = [];
for(var n = 0, l = data.name.length; n < l; n++) {
var name = data.name.substr(0, n);
var title = figlet.textSync(name, { horizontalLayout: 'full' });
if (opts.color) {
title = chalk.green(title);
}
frames.push(title);
}
var rules = {
frames: frames,
autostart: {
delay: 150
}
};
if (callback) {
rules.autostart.end = callback;
}
new CliFrames(rules);
}