UNPKG

9.46 kBJavaScriptView Raw
1
2/**
3Definition of the `main` function.
4@module cli/main
5@license MIT. See LICENSE.md for details.
6 */
7
8(function() {
9 var Command, EXTEND, FS, HME, HMR, HMSTATUS, OUTPUT, PAD, PATH, PKG, StringUtils, _, _opts, _out, _title, chalk, execute, initOptions, initialize, loadOptions, logMsg, main, safeLoadJSON, splitSrcDest;
10
11 HMR = require('../hmc');
12
13 PKG = require('../../package.json');
14
15 FS = require('fs');
16
17 EXTEND = require('extend');
18
19 chalk = require('chalk');
20
21 PATH = require('path');
22
23 HMSTATUS = require('../hmc/dist/core/status-codes');
24
25 HME = require('../hmc/dist/core/event-codes');
26
27 safeLoadJSON = require('../hmc/dist/utils/safe-json-loader');
28
29 StringUtils = require('../hmc/dist/utils/string.js');
30
31 _ = require('underscore');
32
33 OUTPUT = require('./out');
34
35 PAD = require('string-padding');
36
37 Command = require('commander').Command;
38
39 _opts = {};
40
41 _title = chalk.white.bold('\n*** HackMyResume v' + PKG.version + ' ***');
42
43 _out = new OUTPUT(_opts);
44
45
46 /*
47 A callable implementation of the HackMyResume CLI. Encapsulates the command
48 line interface as a single method accepting a parameter array.
49 @alias module:cli/main.main
50 @param rawArgs {Array} An array of command-line parameters. Will either be
51 process.argv (in production) or custom parameters (in test).
52 */
53
54 main = module.exports = function(rawArgs) {
55 var args, initInfo, program;
56 initInfo = initialize(rawArgs);
57 args = initInfo.args;
58 program = new Command('hackmyresume').version(PKG.version).description(chalk.yellow.bold('*** HackMyResume ***')).option('-s --silent', 'Run in silent mode').option('--no-color', 'Disable colors').option('--color', 'Enable colors').option('-d --debug', 'Enable diagnostics', false).option('-a --assert', 'Treat warnings as errors', false).option('-v --version', 'Show the version').allowUnknownOption();
59 program.jsonArgs = initInfo.options;
60 program.command('new')["arguments"]('<sources...>').option('-f --format <fmt>', 'FRESH or JRS format', 'FRESH').alias('create').description('Create resume(s) in FRESH or JSON RESUME format.').action((function(sources) {
61 execute.call(this, sources, [], this.opts(), logMsg);
62 }));
63 program.command('validate')["arguments"]('<sources...>').description('Validate a resume in FRESH or JSON RESUME format.').action(function(sources) {
64 execute.call(this, sources, [], this.opts(), logMsg);
65 });
66 program.command('convert').description('Convert a resume to/from FRESH or JSON RESUME format.').action(function() {
67 var x;
68 x = splitSrcDest.call(this);
69 execute.call(this, x.src, x.dst, this.opts(), logMsg);
70 });
71 program.command('analyze')["arguments"]('<sources...>').description('Analyze one or more resumes.').action(function(sources) {
72 execute.call(this, sources, [], this.opts(), logMsg);
73 });
74 program.command('peek')["arguments"]('<sources...>').description('Peek at a resume field or section').action(function(sources, sectionOrField) {
75 var dst;
76 dst = sources && sources.length > 1 ? [sources.pop()] : [];
77 execute.call(this, sources, dst, this.opts(), logMsg);
78 });
79 program.command('build').alias('generate').option('-t --theme <theme>', 'Theme name or path').option('-n --no-prettify', 'Disable HTML prettification', true).option('-c --css <option>', 'CSS linking / embedding').option('-p --pdf <engine>', 'PDF generation engine').option('--no-sort', 'Sort resume sections by date', false).option('--tips', 'Display theme tips and warnings.', false).description('Generate resume to multiple formats').action(function(sources, targets, options) {
80 var x;
81 x = splitSrcDest.call(this);
82 execute.call(this, x.src, x.dst, this.opts(), logMsg);
83 });
84 program.parse(args);
85 if (!program.args.length) {
86 throw {
87 fluenterror: 4
88 };
89 }
90 };
91
92
93 /* Massage command-line args and setup Commander.js. */
94
95 initialize = function(ar) {
96 var o;
97 o = initOptions(ar);
98 o.silent || logMsg(_title);
99 if (o.debug) {
100 _out.log(chalk.cyan('The -d or --debug switch was specified. DEBUG mode engaged.'));
101 _out.log('');
102 _out.log(chalk.cyan(PAD(' Platform:', 25, null, PAD.RIGHT)) + chalk.cyan.bold(process.platform === 'win32' ? 'windows' : process.platform));
103 _out.log(chalk.cyan(PAD(' Node.js:', 25, null, PAD.RIGHT)) + chalk.cyan.bold(process.version));
104 _out.log(chalk.cyan(PAD(' HackMyResume:', 25, null, PAD.RIGHT)) + chalk.cyan.bold('v' + PKG.version));
105 _out.log(chalk.cyan(PAD(' FRESCA:', 25, null, PAD.RIGHT)) + chalk.cyan.bold(PKG.dependencies.fresca));
106 _out.log('');
107 }
108 if (o.verb && !HMR.verbs[o.verb] && !HMR.alias[o.verb]) {
109 throw {
110 fluenterror: HMSTATUS.invalidCommand,
111 quit: true,
112 attempted: o.orgVerb
113 };
114 }
115 Command.prototype.missingArgument = function(name) {
116 if (this.name() !== 'new') {
117 throw {
118 fluenterror: HMSTATUS.resumeNotFound,
119 quit: true
120 };
121 }
122 };
123 Command.prototype.helpInformation = function() {
124 var manPage;
125 manPage = FS.readFileSync(PATH.join(__dirname, 'use.txt'), 'utf8');
126 return chalk.green.bold(manPage);
127 };
128 return {
129 args: o.args,
130 options: o.json
131 };
132 };
133
134
135 /* Init options prior to setting up command infrastructure. */
136
137 initOptions = function(ar) {
138 oVerb;
139 var args, cleanArgs, inf, isDebug, isMono, isSilent, oJSON, oVerb, optStr, optsIdx, verb, vidx;
140 verb = '';
141 args = ar.slice();
142 cleanArgs = args.slice(2);
143 oJSON;
144 if (cleanArgs.length) {
145 vidx = _.findIndex(cleanArgs, function(v) {
146 return v[0] !== '-';
147 });
148 if (vidx !== -1) {
149 oVerb = cleanArgs[vidx];
150 verb = args[vidx + 2] = oVerb.trim().toLowerCase();
151 }
152 optsIdx = _.findIndex(cleanArgs, function(v) {
153 return v === '-o' || v === '--options' || v === '--opts';
154 });
155 if (optsIdx !== -1) {
156 optStr = cleanArgs[optsIdx + 1];
157 args.splice(optsIdx + 2, 2);
158 if (optStr && (optStr = optStr.trim())) {
159 if (optStr[0] === '{') {
160
161 /* jshint ignore:start */
162 oJSON = eval('(' + optStr + ')');
163
164 /* jshint ignore:end */
165 } else {
166 inf = safeLoadJSON(optStr);
167 if (!inf.ex) {
168 oJSON = inf.json;
169 }
170 }
171 }
172 }
173 }
174 isDebug = _.some(args, function(v) {
175 return v === '-d' || v === '--debug';
176 });
177 isSilent = _.some(args, function(v) {
178 return v === '-s' || v === '--silent';
179 });
180 isMono = _.some(args, function(v) {
181 return v === '--no-color';
182 });
183 return {
184 color: !isMono,
185 debug: isDebug,
186 silent: isSilent,
187 orgVerb: oVerb,
188 verb: verb,
189 json: oJSON,
190 args: args
191 };
192 };
193
194
195 /* Invoke a HackMyResume verb. */
196
197 execute = function(src, dst, opts, log) {
198 var hand, v;
199 loadOptions.call(this, opts, this.parent.jsonArgs);
200 hand = require('./error');
201 hand.init(_opts.debug, _opts.assert, _opts.silent);
202 v = new HMR.verbs[this.name()]();
203 _opts.errHandler = v;
204 _out.init(_opts);
205 v.on('hmr:status', function() {
206 return _out["do"].apply(_out, arguments);
207 });
208 v.on('hmr:error', function() {
209 return hand.err.apply(hand, arguments);
210 });
211 v.invoke.call(v, src, dst, _opts, log);
212 if (v.errorCode) {
213 return process.exit(v.errorCode);
214 }
215 };
216
217
218 /*
219 Initialize HackMyResume options.
220 TODO: Options loading is a little hacky, for two reasons:
221 - Commander.js idiosyncracies
222 - Need to accept JSON inputs from the command line.
223 */
224
225 loadOptions = function(o, cmdO) {
226 if (cmdO) {
227 o = EXTEND(true, o, cmdO);
228 }
229 o = EXTEND(true, o, this.opts());
230 if (this.parent.silent !== void 0 && this.parent.silent !== null) {
231 o.silent = this.parent.silent;
232 }
233 if (this.parent.debug !== void 0 && this.parent.debug !== null) {
234 o.debug = this.parent.debug;
235 }
236 if (this.parent.assert !== void 0 && this.parent.assert !== null) {
237 o.assert = this.parent.assert;
238 }
239 if (o.debug) {
240 logMsg(chalk.cyan('OPTIONS:') + '\n');
241 _.each(o, function(val, key) {
242 return logMsg(chalk.cyan(' %s') + chalk.cyan.bold(' %s'), PAD(key, 22, null, PAD.RIGHT), val);
243 });
244 logMsg('');
245 }
246 EXTEND(true, _opts, o);
247 };
248
249
250 /* Split multiple command-line filenames by the 'TO' keyword */
251
252 splitSrcDest = function() {
253 var params, splitAt;
254 params = this.parent.args.filter(function(j) {
255 return String.is(j);
256 });
257 if (params.length === 0) {
258 throw {
259 fluenterror: HMSTATUS.resumeNotFound,
260 quit: true
261 };
262 }
263 splitAt = _.findIndex(params, function(p) {
264 return p.toLowerCase() === 'to';
265 });
266 if (splitAt === params.length - 1 && splitAt !== -1) {
267 logMsg(chalk.yellow('Please ') + chalk.yellow.bold('specify an output file') + chalk.yellow(' for this operation or ') + chalk.yellow.bold('omit the TO keyword') + chalk.yellow('.'));
268 return;
269 }
270 return {
271 src: params.slice(0, splitAt === -1 ? void 0 : splitAt),
272 dst: splitAt === -1 ? [] : params.slice(splitAt + 1)
273 };
274 };
275
276
277 /* Simple logging placeholder. */
278
279 logMsg = function() {
280 return _opts.silent || console.log.apply(console.log, arguments);
281 };
282
283}).call(this);