diff --git a/README.md b/README.md index 970073d..92d94df 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ Currently it indexes all functions, both public and private. ## Usage $ bin/javascript-ctags -? - javascript-ctags [-?] [-t tagfile] [fileglob] + javascript-ctags [-?] [-t/-f tagfile] [fileglob] --help, -h, -? show this - --tagfile, -t The generated tagfile (default tags) + --tagfile, -t, -f The generated tagfile (default tags), use - for STDOUT fileglob A glob pattern (supports **/*.js), (default *.js) diff --git a/lib/javascript_ctags.js b/lib/javascript_ctags.js index deb6e8d..41ad1c1 100644 --- a/lib/javascript_ctags.js +++ b/lib/javascript_ctags.js @@ -27,8 +27,16 @@ function parse(files) { } function writeFile(filename, tags) { - fs.writeFileSync(filename, tags.join('\n')); + var contents = tags.join("\n"); + if(filename == "-") { + console.log(contents); + } else { + fs.writeFileSync(filename, contents); + } } + + module.exports = parseAndGenerate; module.exports.files = files; +module.exports.writeFile = writeFile; diff --git a/lib/main.js b/lib/main.js index f615026..a7352dd 100644 --- a/lib/main.js +++ b/lib/main.js @@ -18,6 +18,7 @@ function parseArgs(args) { }; var shortcuts = { t: '--tagfile', + f: '--tagfile', h: '--help', '?': '--help' }; @@ -30,9 +31,9 @@ function parseArgs(args) { } function usage() { - console.log('javascript-ctags [-?] [-t tagfile] [fileglob]'); + console.log('javascript-ctags [-?] [-t or -f tagfile] [fileglob]'); console.log('--help, -h, -?\t show this'); - console.log('--tagfile, -t\t The generated tagfile (default tags)'); + console.log('--tagfile, -t, -f\t The generated tagfile (default tags), use - for STDOUT'); console.log(' fileglob\t A glob pattern (supports **/*.js), (default *.js)'); } diff --git a/package.json b/package.json index 0e28774..e483793 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ }, "devDependencies": { "mocha": "~1.7.0", - "chai": "~1.3.0" + "chai": "~1.3.0", + "sinon": "~1.7" }, "bin": { "javascript-ctags": "./bin/javascript-ctags" } } diff --git a/test/javascript_ctags_spec.js b/test/javascript_ctags_spec.js index adeedce..24992b6 100644 --- a/test/javascript_ctags_spec.js +++ b/test/javascript_ctags_spec.js @@ -1,7 +1,9 @@ "use strict"; require('mocha'); +var sinon = require('sinon'); var expect = require('chai').expect; +var fs = require('fs'); var javascriptCtags = require('../lib/javascript_ctags'); @@ -13,4 +15,32 @@ describe('main', function() { expect(files).to.have.length(5); }); }); + + describe("#writeFile", function(){ + var consoleSpy, + fsSpy, + tags = []; + + beforeEach(function() { + consoleSpy = sinon.spy(console, 'log'); + fsSpy = sinon.spy(fs, 'writeFileSync'); + }); + + afterEach(function() { + console.log.restore(); + fs.writeFileSync.restore(); + }); + + it("writes contents to console if filename is -", function(){ + javascriptCtags.writeFile("-", tags); + expect(consoleSpy.called).to.equal(true); + expect(fsSpy.called).to.equal(false); + }); + + it("writes contents to given file", function(){ + javascriptCtags.writeFile("tags", tags); + expect(fsSpy.called).to.equal(true); + expect(consoleSpy.called).to.equal(false); + }); + }); });