HackMyResume/dist/cli/out.js

191 lines
6.8 KiB
JavaScript
Raw Normal View History

2016-01-25 15:34:57 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Output routines for HackMyResume.
@license MIT. See LICENSE.md for details.
@module cli/out
*/
2018-02-12 09:01:00 +00:00
var EXTEND, FS, HANDLEBARS, HME, M2C, OutputHandler, PATH, YAML, _, chalk, dbgStyle, pad, printf;
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
chalk = require('chalk');
2016-01-25 15:34:57 +00:00
HME = require('../core/event-codes');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
_ = require('underscore');
2016-01-25 15:34:57 +00:00
M2C = require('../utils/md2chalk.js');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
PATH = require('path');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
FS = require('fs');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
EXTEND = require('extend');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
HANDLEBARS = require('handlebars');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
YAML = require('yamljs');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
printf = require('printf');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
pad = require('string-padding');
2016-01-25 15:34:57 +00:00
2016-01-26 16:43:49 +00:00
dbgStyle = 'cyan';
2016-01-25 15:34:57 +00:00
2018-02-12 09:01:00 +00:00
OutputHandler = class OutputHandler {
2018-02-12 05:05:29 +00:00
constructor(opts) {
this.init(opts);
return;
}
2018-02-12 05:05:29 +00:00
init(opts) {
2016-01-26 16:43:49 +00:00
this.opts = EXTEND(true, this.opts || {}, opts);
2016-01-27 10:29:26 +00:00
this.msgs = YAML.load(PATH.join(__dirname, 'msg.yml')).events;
2018-02-12 05:05:29 +00:00
}
2018-02-12 09:01:00 +00:00
log() {
2016-01-26 16:43:49 +00:00
var finished;
printf = require('printf');
finished = printf.apply(printf, arguments);
2018-02-12 09:01:00 +00:00
return this.opts.silent || console.log(finished); // eslint-disable-line no-console
2018-02-12 05:05:29 +00:00
}
2018-02-12 05:05:29 +00:00
do(evt) {
2018-02-12 09:01:00 +00:00
var L, adj, info, msg, msgs, numFormats, output, rawTpl, sty, style, suffix, template, that, themeName, tot;
2016-01-26 16:43:49 +00:00
that = this;
L = function() {
return that.log.apply(that, arguments);
};
switch (evt.sub) {
2016-01-25 15:34:57 +00:00
case HME.begin:
2016-01-26 16:43:49 +00:00
return this.opts.debug && L(M2C(this.msgs.begin.msg, dbgStyle), evt.cmd.toUpperCase());
2018-02-12 05:05:29 +00:00
//when HME.beforeCreate
//L( M2C( this.msgs.beforeCreate.msg, 'green' ), evt.fmt, evt.file )
//break;
2016-02-02 02:14:36 +00:00
case HME.afterCreate:
L(M2C(this.msgs.beforeCreate.msg, evt.isError ? 'red' : 'green'), evt.fmt, evt.file);
2016-01-25 15:34:57 +00:00
break;
case HME.beforeTheme:
2016-01-26 16:43:49 +00:00
return this.opts.debug && L(M2C(this.msgs.beforeTheme.msg, dbgStyle), evt.theme.toUpperCase());
2016-01-25 15:34:57 +00:00
case HME.afterParse:
2016-01-26 16:43:49 +00:00
return L(M2C(this.msgs.afterRead.msg, 'gray', 'white.dim'), evt.fmt.toUpperCase(), evt.file);
2016-01-25 15:34:57 +00:00
case HME.beforeMerge:
2016-01-26 16:43:49 +00:00
msg = '';
evt.f.reverse().forEach(function(a, idx) {
return msg += printf((idx === 0 ? this.msgs.beforeMerge.msg[0] : this.msgs.beforeMerge.msg[1]), a.file);
2016-01-25 15:34:57 +00:00
}, this);
2016-01-26 19:43:48 +00:00
return L(M2C(msg, (evt.mixed ? 'yellow' : 'gray'), 'white.dim'));
2016-01-25 15:34:57 +00:00
case HME.applyTheme:
this.theme = evt.theme;
2016-01-26 16:43:49 +00:00
numFormats = Object.keys(evt.theme.formats).length;
2016-01-26 19:43:48 +00:00
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');
2016-01-25 15:34:57 +00:00
case HME.end:
2016-01-26 16:43:49 +00:00
if (evt.cmd === 'build') {
themeName = this.theme.name.toUpperCase();
if (this.opts.tips && (this.theme.message || this.theme.render)) {
if (this.theme.message) {
L(M2C(this.msgs.afterBuild.msg[0], 'cyan'), themeName);
return L(M2C(this.theme.message, 'white'));
} else if (this.theme.render) {
L(M2C(this.msgs.afterBuild.msg[0], 'cyan'), themeName);
return L(M2C(this.msgs.afterBuild.msg[1], 'white'));
2016-01-25 15:34:57 +00:00
}
}
}
break;
case HME.afterGenerate:
2016-01-26 16:43:49 +00:00
suffix = '';
if (evt.fmt === 'pdf') {
if (this.opts.pdf) {
if (this.opts.pdf !== 'none') {
suffix = printf(M2C(this.msgs.afterGenerate.msg[0], evt.error ? 'red' : 'green'), this.opts.pdf);
} else {
L(M2C(this.msgs.afterGenerate.msg[1], 'gray'), evt.fmt.toUpperCase(), evt.file);
2016-01-25 15:34:57 +00:00
return;
}
}
}
2016-01-26 16:43:49 +00:00
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));
2016-01-25 15:34:57 +00:00
case HME.beforeAnalyze:
2016-01-26 16:43:49 +00:00
return L(M2C(this.msgs.beforeAnalyze.msg, 'green'), evt.fmt, evt.file);
2016-01-25 15:34:57 +00:00
case HME.afterAnalyze:
2016-01-26 16:43:49 +00:00
info = evt.info;
rawTpl = FS.readFileSync(PATH.join(__dirname, 'analyze.hbs'), 'utf8');
HANDLEBARS.registerHelper(require('../helpers/console-helpers'));
2016-01-26 16:43:49 +00:00
template = HANDLEBARS.compile(rawTpl, {
strict: false,
assumeObjects: false
});
tot = 0;
info.keywords.forEach(function(g) {
return tot += g.count;
});
2016-01-25 15:34:57 +00:00
info.keywords.totalKeywords = tot;
2016-01-26 16:43:49 +00:00
output = template(info);
return this.log(chalk.cyan(output));
2016-01-25 15:34:57 +00:00
case HME.beforeConvert:
2018-01-30 07:34:58 +00:00
return L(M2C(this.msgs.beforeConvert.msg, evt.error ? 'red' : 'green'), evt.srcFile, evt.srcFmt, evt.dstFile, evt.dstFmt);
2016-01-25 15:34:57 +00:00
case HME.afterInlineConvert:
2016-01-26 16:43:49 +00:00
return L(M2C(this.msgs.afterInlineConvert.msg, 'gray', 'white.dim'), evt.file, evt.fmt);
2016-01-25 15:34:57 +00:00
case HME.afterValidate:
style = 'red';
adj = '';
msgs = this.msgs.afterValidate.msg;
switch (evt.status) {
case 'valid':
style = 'green';
adj = msgs[1];
break;
case 'invalid':
style = 'yellow';
adj = msgs[2];
break;
case 'broken':
style = 'red';
adj = msgs[3];
break;
case 'missing':
style = 'red';
adj = msgs[4];
2016-02-13 05:11:52 +00:00
break;
case 'unknown':
style = 'red';
adj = msgs[5];
}
2016-02-13 05:11:52 +00:00
evt.schema = evt.schema.replace('jars', 'JSON Resume').toUpperCase();
2016-02-13 04:47:08 +00:00
L(M2C(msgs[0], 'white') + chalk[style].bold(adj), evt.file, evt.schema);
if (evt.violations) {
2018-02-12 09:01:00 +00:00
_.each(evt.violations, function(err) {
L(chalk.yellow.bold('--> ') + chalk.yellow(err.field.replace('data.', 'resume.').toUpperCase() + ' ' + err.message));
2016-01-25 15:34:57 +00:00
}, this);
}
break;
case HME.afterPeek:
2016-01-26 16:43:49 +00:00
sty = evt.error ? 'red' : (evt.target !== void 0 ? 'green' : 'yellow');
2018-02-12 05:05:29 +00:00
// "Peeking at 'someKey' in 'someFile'."
2016-01-26 16:43:49 +00:00
if (evt.requested) {
2016-01-25 15:34:57 +00:00
L(M2C(this.msgs.beforePeek.msg[0], sty), evt.requested, evt.file);
2016-01-26 16:43:49 +00:00
} else {
2016-01-25 15:34:57 +00:00
L(M2C(this.msgs.beforePeek.msg[1], sty), evt.file);
2016-01-26 16:43:49 +00:00
}
2018-02-12 05:05:29 +00:00
// If the key was present, print it
2016-02-04 01:08:17 +00:00
if (evt.target !== void 0 && !evt.error) {
2018-02-12 09:01:00 +00:00
// eslint-disable-next-line no-console
2016-01-26 16:43:49 +00:00
return console.dir(evt.target, {
depth: null,
colors: true
});
} else if (!evt.error) {
return L(M2C(this.msgs.afterPeek.msg, 'yellow'), evt.requested, evt.file);
2016-02-02 22:34:10 +00:00
} else if (evt.error) {
return L(chalk.red(evt.error.inner.inner));
2016-01-26 16:43:49 +00:00
}
2016-01-25 15:34:57 +00:00
}
2018-02-12 05:05:29 +00:00
}
2018-02-12 05:05:29 +00:00
};
2016-01-25 15:34:57 +00:00
2018-02-12 09:01:00 +00:00
module.exports = OutputHandler;
2016-01-26 16:43:49 +00:00
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=out.js.map