From 8d8ecdd920b8a13016a1304484222528a2f76a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Korecki?= Date: Tue, 10 Sep 2013 21:02:45 +0000 Subject: [PATCH 1/3] add support for ctags compat -f switch and - as stdout --- lib/javascript_ctags.js | 9 ++++++++- lib/main.js | 5 +++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/javascript_ctags.js b/lib/javascript_ctags.js index deb6e8d..972a328 100644 --- a/lib/javascript_ctags.js +++ b/lib/javascript_ctags.js @@ -27,8 +27,15 @@ function parse(files) { } function writeFile(filename, tags) { - fs.writeFileSync(filename, tags.join('\n')); + var content = tags.join("\n"); + if(filename == "-") { + console.log(content); + } else { + fs.writeFileSync(filename, contents); + } } + + module.exports = parseAndGenerate; module.exports.files = files; diff --git a/lib/main.js b/lib/main.js index f615026..58e33f8 100644 --- a/lib/main.js +++ b/lib/main.js @@ -18,19 +18,20 @@ function parseArgs(args) { }; var shortcuts = { t: '--tagfile', + f: '--tagfile', h: '--help', '?': '--help' }; var options = nopt(longOptions, shortcuts, args); var rest = options.argv.remain; - options.pattern = rest.length > 0 ? rest[0] : '*.js'; + options.pattern = rest.length > 0 ? rest[0] : './*.js'; if (!options.tagfile) options.tagfile = 'tags'; return options; } 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(' fileglob\t A glob pattern (supports **/*.js), (default *.js)'); From 24df39baa54fd0d4e236cba997040a3e9e1487bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Korecki?= Date: Wed, 11 Sep 2013 10:24:34 +0100 Subject: [PATCH 2/3] Add test for writeFile --- lib/javascript_ctags.js | 5 +++-- lib/main.js | 2 +- package.json | 3 ++- test/javascript_ctags_spec.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/javascript_ctags.js b/lib/javascript_ctags.js index 972a328..41ad1c1 100644 --- a/lib/javascript_ctags.js +++ b/lib/javascript_ctags.js @@ -27,9 +27,9 @@ function parse(files) { } function writeFile(filename, tags) { - var content = tags.join("\n"); + var contents = tags.join("\n"); if(filename == "-") { - console.log(content); + console.log(contents); } else { fs.writeFileSync(filename, contents); } @@ -39,3 +39,4 @@ function writeFile(filename, tags) { module.exports = parseAndGenerate; module.exports.files = files; +module.exports.writeFile = writeFile; diff --git a/lib/main.js b/lib/main.js index 58e33f8..f518152 100644 --- a/lib/main.js +++ b/lib/main.js @@ -25,7 +25,7 @@ function parseArgs(args) { var options = nopt(longOptions, shortcuts, args); var rest = options.argv.remain; - options.pattern = rest.length > 0 ? rest[0] : './*.js'; + options.pattern = rest.length > 0 ? rest[0] : '*.js'; if (!options.tagfile) options.tagfile = 'tags'; return options; } 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); + }); + }); }); From f2512c9adebdf2835a232a302a9b2aa78d36a4bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Korecki?= Date: Wed, 11 Sep 2013 10:31:49 +0100 Subject: [PATCH 3/3] updated readme and cmd line help --- README.md | 4 ++-- lib/main.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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/main.js b/lib/main.js index f518152..a7352dd 100644 --- a/lib/main.js +++ b/lib/main.js @@ -33,7 +33,7 @@ function parseArgs(args) { function usage() { 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)'); }