UNPKG

6.09 kBJavaScriptView Raw
1
2/**
3Output routines for HackMyResume.
4@license MIT. See LICENSE.md for details.
5@module cli/out
6 */
7
8(function() {
9 var Class, EXTEND, FS, HANDLEBARS, HME, LO, M2C, OutputHandler, PATH, YAML, _, chalk, dbgStyle, pad, printf;
10
11 chalk = require('chalk');
12
13 HME = require('../hmc/dist/core/event-codes');
14
15 _ = require('underscore');
16
17 Class = require('../hmc/dist/utils/class.js');
18
19 M2C = require('../hmc/dist/utils/md2chalk.js');
20
21 PATH = require('path');
22
23 LO = require('lodash');
24
25 FS = require('fs');
26
27 EXTEND = require('extend');
28
29 HANDLEBARS = require('handlebars');
30
31 YAML = require('yamljs');
32
33 printf = require('printf');
34
35 pad = require('string-padding');
36
37 dbgStyle = 'cyan';
38
39
40 /** A stateful output module. All HMR console output handled here. */
41
42 OutputHandler = module.exports = Class.extend({
43 init: function(opts) {
44 this.opts = EXTEND(true, this.opts || {}, opts);
45 this.msgs = YAML.load(PATH.join(__dirname, 'msg.yml')).events;
46 },
47 log: function(msg) {
48 var finished;
49 msg = msg || '';
50 printf = require('printf');
51 finished = printf.apply(printf, arguments);
52 return this.opts.silent || console.log(finished);
53 },
54 "do": function(evt) {
55 var L, WRAP, info, msg, numFormats, output, rawTpl, sty, style, suffix, template, that, themeName, tot;
56 that = this;
57 L = function() {
58 return that.log.apply(that, arguments);
59 };
60 switch (evt.sub) {
61 case HME.begin:
62 return this.opts.debug && L(M2C(this.msgs.begin.msg, dbgStyle), evt.cmd.toUpperCase());
63 case HME.beforeCreate:
64 L(M2C(this.msgs.beforeCreate.msg, 'green'), evt.fmt, evt.file);
65 break;
66 case HME.beforeTheme:
67 return this.opts.debug && L(M2C(this.msgs.beforeTheme.msg, dbgStyle), evt.theme.toUpperCase());
68 case HME.afterParse:
69 return L(M2C(this.msgs.afterRead.msg, 'gray', 'white.dim'), evt.fmt.toUpperCase(), evt.file);
70 case HME.beforeMerge:
71 msg = '';
72 evt.f.reverse().forEach(function(a, idx) {
73 return msg += printf((idx === 0 ? this.msgs.beforeMerge.msg[0] : this.msgs.beforeMerge.msg[1]), a.file);
74 }, this);
75 return L(M2C(msg, (evt.mixed ? 'yellow' : 'gray'), 'white.dim'));
76 case HME.applyTheme:
77 this.theme = evt.theme;
78 numFormats = Object.keys(evt.theme.formats).length;
79 return L(M2C(this.msgs.applyTheme.msg, evt.status === 'error' ? 'red' : 'gray', evt.status === 'error' ? 'bold' : 'white.dim'), evt.theme.name.toUpperCase(), numFormats, numFormats === 1 ? '' : 's');
80 case HME.end:
81 if (evt.cmd === 'build') {
82 themeName = this.theme.name.toUpperCase();
83 if (this.opts.tips && (this.theme.message || this.theme.render)) {
84 WRAP = require('word-wrap');
85 if (this.theme.message) {
86 L(M2C(this.msgs.afterBuild.msg[0], 'cyan'), themeName);
87 return L(M2C(this.theme.message, 'white'));
88 } else if (this.theme.render) {
89 L(M2C(this.msgs.afterBuild.msg[0], 'cyan'), themeName);
90 return L(M2C(this.msgs.afterBuild.msg[1], 'white'));
91 }
92 }
93 }
94 break;
95 case HME.afterGenerate:
96 suffix = '';
97 if (evt.fmt === 'pdf') {
98 if (this.opts.pdf) {
99 if (this.opts.pdf !== 'none') {
100 suffix = printf(M2C(this.msgs.afterGenerate.msg[0], evt.error ? 'red' : 'green'), this.opts.pdf);
101 } else {
102 L(M2C(this.msgs.afterGenerate.msg[1], 'gray'), evt.fmt.toUpperCase(), evt.file);
103 return;
104 }
105 }
106 }
107 return L(M2C(this.msgs.afterGenerate.msg[2] + suffix, evt.error ? 'red' : 'green'), pad(evt.fmt.toUpperCase(), 4, null, pad.RIGHT), PATH.relative(process.cwd(), evt.file));
108 case HME.beforeAnalyze:
109 return L(M2C(this.msgs.beforeAnalyze.msg, 'green'), evt.fmt, evt.file);
110 case HME.afterAnalyze:
111 info = evt.info;
112 rawTpl = FS.readFileSync(PATH.join(__dirname, 'analyze.hbs'), 'utf8');
113 HANDLEBARS.registerHelper(require('../hmc/dist/helpers/console-helpers'));
114 template = HANDLEBARS.compile(rawTpl, {
115 strict: false,
116 assumeObjects: false
117 });
118 tot = 0;
119 info.keywords.forEach(function(g) {
120 return tot += g.count;
121 });
122 info.keywords.totalKeywords = tot;
123 output = template(info);
124 return this.log(chalk.cyan(output));
125 case HME.beforeConvert:
126 return L(M2C(this.msgs.beforeConvert.msg, 'green'), evt.srcFile, evt.srcFmt, evt.dstFile, evt.dstFmt);
127 case HME.afterInlineConvert:
128 return L(M2C(this.msgs.afterInlineConvert.msg, 'gray', 'white.dim'), evt.file, evt.fmt);
129 case HME.afterValidate:
130 style = evt.isValid ? 'green' : 'yellow';
131 L(M2C(this.msgs.afterValidate.msg[0], 'white') + chalk[style].bold(evt.isValid ? this.msgs.afterValidate.msg[1] : this.msgs.afterValidate.msg[2]), evt.file, evt.fmt);
132 if (evt.errors) {
133 return _.each(evt.errors, function(err, idx) {
134 return L(chalk.yellow.bold('--> ') + chalk.yellow(err.field.replace('data.', 'resume.').toUpperCase() + ' ' + err.message));
135 }, this);
136 }
137 break;
138 case HME.afterPeek:
139 sty = evt.error ? 'red' : (evt.target !== void 0 ? 'green' : 'yellow');
140 if (evt.requested) {
141 L(M2C(this.msgs.beforePeek.msg[0], sty), evt.requested, evt.file);
142 } else {
143 L(M2C(this.msgs.beforePeek.msg[1], sty), evt.file);
144 }
145 if (evt.target !== void 0) {
146 return console.dir(evt.target, {
147 depth: null,
148 colors: true
149 });
150 } else if (!evt.error) {
151 return L(M2C(this.msgs.afterPeek.msg, 'yellow'), evt.requested, evt.file);
152 }
153 }
154 }
155 });
156
157}).call(this);