Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kobezzza committed Apr 28, 2015
1 parent d21ae28 commit 38e640e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
42 changes: 29 additions & 13 deletions bin/monic.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,43 @@ function action(file, input) {
console.time('Time');

if (!file) {
line(true);
console.error('Invalid input data');
line(true);
process.exit(1);
error('Invalid input data');
}

function toObj(res, el) {
res[el] = true;
return res;
}

function url(url) {
return path.normalize(path.relative(root, path.resolve(url)));
}

function line(opt_error) {
console[opt_error ? 'error' : 'log'](new Array(80).join(opt_error ? '!' : '~'));
}

function date(opt_error) {
console[opt_error ? 'error' : 'log']('[[ ' + new Date().toString() + ' ]]');
}

function error(err) {
line(true);
console.error(err.message || err);

if (err.file) {
console.error('File: ' + url(err.file));
}

if (err.line) {
console.error('Line: ' + err.line);
}

date(true);
line(true);
process.exit(1);
}

function parse(val) {
switch (val) {
case 'true':
Expand Down Expand Up @@ -88,20 +110,14 @@ function action(file, input) {

}, function (err, data) {
if (err) {
line(true);
console.error(err.message);
line(true);
process.exit(1);
error(err);
}

if (out) {
var
from = path.normalize(path.relative(root, path.resolve(file))),
to = path.normalize(path.relative(root, path.resolve(out)));

line();
console.log('File "' + from + '" has been successfully builded "' + to + '".');
console.log('File "' + url(file) + '" has been successfully builded "' + url(out) + '"');
console.timeEnd('Time');
date();
line();

} else {
Expand Down
19 changes: 7 additions & 12 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
*/
export class MonicError {
/**
* @param {string} msg - an error text
* @param {string} file - a path to a file in which the error occurred
* @param {number} line - a line number where the error occurred
* @param {(string|!Error)} msg - an error text or an error object
* @param {string=} [opt_file] - a path to a file in which the error occurred
* @param {number=} [opt_line] - a line number where the error occurred
*/
constructor(msg, file, line) {
this.message = msg;
this.file = file;
this.line = line;
}

/** @return {string} */
toString() {
return `Error: ${this.message} (${this.file}: ${this.line})`;
constructor(msg, opt_file, opt_line) {
this.message = msg.message || msg;
this.file = opt_file;
this.line = opt_line;
}
}
24 changes: 11 additions & 13 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ export default class Parser {
replacer(content, file, (err, res) => next(err, err ? undefined : content = res));

} else {
content = replacer(content, file);
next();
try {
content = replacer(content, file);
next();

} catch (err) {
next(new MonicError(err, file));
}
}
});
});
Expand Down Expand Up @@ -182,24 +187,17 @@ export default class Parser {

this.cache[file] = fileStructure;
const parseLines = (start) => {
const
errors = [];

let
info,
i;

function appendError(err) {
const
msg = err.message;

errors.push(new MonicError(msg, file, i + 1));
fileStructure.error(msg, info);
function error(err) {
callback(new MonicError(err, file, i + 1));
}

function asyncParseCallback(err) {
if (err) {
appendError(err);
return error(err);
}

parseLines(i + 1);
Expand Down Expand Up @@ -289,7 +287,7 @@ export default class Parser {
this[key](fileStructure, params);

} catch (err) {
appendError(err);
return error(err);
}

} else {
Expand Down

0 comments on commit 38e640e

Please sign in to comment.