Permalink
Browse files

Refactored to use coa 0.1.1 API

  • Loading branch information...
1 parent 128be7a commit 91d0690a71f550bcaec94f4a4cca7fe8e0391ae2 @arikon arikon committed Sep 30, 2011
Showing with 56 additions and 48 deletions.
  1. +56 −48 lib/cli.js
View
104 lib/cli.js
@@ -13,33 +13,38 @@ exports.main = function () {
.long('version')
.flag()
.act(function(opts) {
- this.exit(
- JSON.parse(require('fs').readFileSync(__dirname + '/../package.json'))
- .version);
+ return JSON.parse(require('fs').readFileSync(__dirname + '/../package.json'))
+ .version;
})
.end()
.opt()
.name('input')
.title('Input file. Defaults to stdin')
.short('i')
.long('input')
+ .input()
.end()
.opt()
.name('output')
.title('Output file. Defaults to stdout')
.short('o')
.long('output')
+ .output()
.end()
.opt()
.name('lang')
.title('Language: js, xml')
.short('l')
.long('lang')
+ .req()
.val(function(value) {
// FIXME: don't use private API _usage()
- !value && this.end().errorExit("Missing required option value\n" + this._usage());
- (['js', 'xml'].indexOf(value) == -1) && this.end()
- .errorExit('Wrong language "' + value + '" specified, must be one of "js" or "xml"');
+ if (!value) {
+ return this.reject("Missing required option value\n" + this._usage());
+ }
+ if (['js', 'xml'].indexOf(value) == -1) {
+ return this.reject('Wrong language "' + value + '" specified, must be one of "js" or "xml"');
+ }
return value;
})
.end()
@@ -51,9 +56,12 @@ exports.main = function () {
.def('html')
.val(function(value) {
// FIXME: don't use private API _usage()
- !value && this.end().errorExit("Missing required option value\n" + this._usage());
- (['html', 'bemjson'].indexOf(value) == -1) && this.end()
- .errorExit('Wrong output format "' + value + '" specified, must be one of "html" or "bemjson"');
+ if (!value) {
+ return this.reject("Missing required option value\n" + this._usage());
+ }
+ if (['html', 'bemjson'].indexOf(value) == -1) {
+ return this.reject('Wrong output format "' + value + '" specified, must be one of "html" or "bemjson"');
+ }
return value;
})
.end()
@@ -65,45 +73,45 @@ exports.main = function () {
.flag()
.end()
.act(function(opts) {
- // FIXME: don't use private API _optsByKey
- !opts.lang && (this.errorExit(this._optsByKey['--lang']._requiredText()));
- (opts.input ?
- // if input file
- function(inputFn) {
- fs.readFile(opts.input, 'utf8', function(err, input){
- if (err) throw err;
- inputFn(input);
- });
- } :
- // if STDIN
- function(inputFn) {
- var input = '';
- process.openStdin()
- .on('data', function(s) { input += s })
- .on('end', function() { inputFn(input) });
- })(function(input){
- try {
- var ohl = require('./ometa-highlighter'),
- ast = ohl.OmetaHighlighter.matchAll(input, opts.lang),
- result = (opts.format == 'html' ?
- ohl.OmetaHighlighterToHtml.match(ast, 'topLevel') :
- JSON.stringify(ohl.OmetaHighlighterToBemjson.match(ast, 'topLevel'), null, 4) + '\n');
- opts.output ?
- fs.writeFile(opts.output, result, 'utf8', function(err) {
- if (err) throw err;
- opts.verbose && sys.error(' create : ' + opts.output);
- }) :
- process.stdout.write(result);
- } catch (e) {
- e.errorPos != undefined &&
- sys.error(
- input.slice(0, e.errorPos) +
- "\n--- Parse error ->" +
- input.slice(e.errorPos) + '\n');
- throw e;
- }
- });
+ readStream(opts.input, function(err, input) {
+ if (err) throw err;
+
+ // remove BOM if present
+ if (input.charCodeAt(0) === 0xFEFF) input = input.slice(1);
+
+ try {
+ var ohl = require('./ometa-highlighter'),
+ ast = ohl.OmetaHighlighter.matchAll(input, opts.lang),
+ result = (opts.format == 'html' ?
+ ohl.OmetaHighlighterToHtml.match(ast, 'topLevel') :
+ JSON.stringify(ohl.OmetaHighlighterToBemjson.match(ast, 'topLevel'), null, 4)) + '\n';
+ opts.output.end(result);
+ } catch (e) {
+ e.errorPos != undefined &&
+ sys.error(
+ input.slice(0, e.errorPos) +
+ "\n--- Parse error ->" +
+ input.slice(e.errorPos) + '\n');
+ throw e;
+ }
+ });
+ opts.input.resume();
+
})
- .parse(process.argv.slice(2));
+ .run();
};
+
+function readStream(stream, callback) {
+ var data = '';
+ stream
+ .on('data', function(chunk) {
+ data += chunk;
+ })
+ .on('error', function(err) {
+ callback(err);
+ })
+ .on('end', function() {
+ callback(null, data);
+ });
+}

0 comments on commit 91d0690

Please sign in to comment.