Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding cli:

  • Loading branch information...
commit 9cda744a871a31a22f13a4b0651c956ff6182b2a 1 parent 38739f1
@gradus authored
View
3  Cakefile
@@ -44,6 +44,9 @@ task 'test', ->
task 'build', ->
build -> log ":)", green
+task 'watch', ->
+ build true, -> log ":)", green
+
# TODO
# task 'auto', 'Watch and Run Spec', ->
# build true, -> spec -> log ":)", green
View
7 bin/jsoncsv
@@ -0,0 +1,7 @@
+#!/usr/bin/env node
+
+var path = require('path');
+var fs = require('fs');
+var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib/cli');
+
+require(lib);
View
22 config/config.json
@@ -0,0 +1,22 @@
+//
+// Configuring app.argv
+//
+
+app.use(flatiron.plugins.cli, {
+ //
+ // Settings used by `require('optimist').options()
+ //
+ argv: {
+ json: {
+ alias: 'j',
+ description: 'JSON',
+ string: true
+ }
+ }
+});
+
+//
+// `app.argv.j` is now available
+//
+console.log(app.argv.j);
+
View
2  example/default.coffee
@@ -1,5 +1,5 @@
fs = require 'fs'
-jsoncsv = require '../lib/index'
+jsoncsv = require '../lib/jsoncsv'
FIELDS = [
'FROM DATE','THRU DATE','CARRIER ID','ACCOUNT ID','GROUP ID',
'FACILITY ID','MEMBER ID','MEMBER First Name','MEMBER Last Name',
View
30 lib/cli.js
@@ -0,0 +1,30 @@
+(function() {
+ var app, cmdConvert, flatiron, path;
+
+ cmdConvert = require('./convert');
+
+ flatiron = require('flatiron');
+
+ path = require('path');
+
+ require('pkginfo')(module);
+
+ app = flatiron.app;
+
+ app.version = exports.version;
+
+ app.use(flatiron.plugins.cli, {
+ source: path.join(__dirname, 'lib'),
+ usage: ["", "jsoncsv v" + app.version, "Converts JSON to CSV", "", "jsoncsv convert '{\"some\": \"json\"}'"],
+ version: true
+ });
+
+ app.cmd('version', function() {
+ return console.log('jsoncsv v' + app.version);
+ });
+
+ app.cmd('convert', cmdConvert);
+
+ app.start();
+
+}).call(this);
View
25 lib/convert.js
@@ -0,0 +1,25 @@
+(function() {
+ var log, parseJson;
+
+ require('./jsoncsv');
+
+ log = console.log;
+
+ parseJson = function(json, cb) {
+ log('Parsing JSON...');
+ log(json);
+ return cb();
+ };
+
+ module.exports = function(json, cb) {
+ if (json == null) {
+ return console.log('JSON Required!\nex: jsoncsv convert \'{"some":"json"}\'');
+ } else {
+ return console.log(json);
+ }
+ return parseJson(json, function() {
+ return cb();
+ });
+ };
+
+}).call(this);
View
79 lib/index.js
@@ -1,57 +1,28 @@
(function() {
- var JsonCsv,
- __hasProp = Object.prototype.hasOwnProperty,
- __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
-
- JsonCsv = (function(_super) {
-
- __extends(JsonCsv, _super);
-
- function JsonCsv() {
- JsonCsv.__super__.constructor.apply(this, arguments);
- }
-
- JsonCsv.prototype.parse = function(data, columns, cb) {
- var column, csv, i, index, j, json, line, _i, _j, _len, _len2;
- if (columns == null) columns = {};
- if (data == null) return cb('error no data');
- json = JSON.parse(data);
- for (_i = 0, _len = json.length; _i < _len; _i++) {
- j = json[_i];
- delete j['_events'];
- delete j['_id'];
- delete j['id'];
- }
- csv = '';
- line = columns.join(',') + '\r\n';
- i = 0;
- while (i < json.length) {
- for (index in json[i]) {
- if (typeof json[i][index] === 'object') {
- for (_j = 0, _len2 = columns.length; _j < _len2; _j++) {
- column = columns[_j];
- if (json[i][index]["" + column] !== void 0) {
- line += json[i][index]["" + column] + ",";
- }
- if (json[i][index]["" + column] === null || json[i][index]["" + column] === void 0) {
- line += ",";
- }
- }
- } else {
- line += '\r\n';
- i = i + 1;
- }
- }
- }
- line.slice(0, line.Length - 1);
- csv += line + "\r\n";
- return cb(null, csv);
- };
-
- return JsonCsv;
-
- })(require('events').EventEmitter);
-
- module.exports = new JsonCsv();
+ var app, cmdConvert, flatiron;
+
+ cmdConvert = require('./convert');
+
+ flatiron = require('flatiron');
+
+ require('pkginfo')(module);
+
+ app = flatiron.app;
+
+ app.version = exports.version;
+
+ app.use(flatiron.plugins.cli, {
+ dir: __dirname,
+ usage: ["jsoncsv v" + app.version, "Converts JSON to CSV", "", "jsoncsv convert [your JSON]"],
+ version: true
+ });
+
+ app.cmd('version', function() {
+ return console.log('jsoncsv v' + app.version);
+ });
+
+ app.cmd('convert', cmdConvert);
+
+ app.start();
}).call(this);
View
57 lib/jsoncsv.js
@@ -0,0 +1,57 @@
+(function() {
+ var JsonCsv,
+ __hasProp = Object.prototype.hasOwnProperty,
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
+
+ JsonCsv = (function(_super) {
+
+ __extends(JsonCsv, _super);
+
+ function JsonCsv() {
+ JsonCsv.__super__.constructor.apply(this, arguments);
+ }
+
+ JsonCsv.prototype.parse = function(data, columns, cb) {
+ var column, csv, i, index, j, json, line, _i, _j, _len, _len2;
+ if (columns == null) columns = {};
+ if (data == null) return cb('error no data');
+ json = JSON.parse(data);
+ for (_i = 0, _len = json.length; _i < _len; _i++) {
+ j = json[_i];
+ delete j['_events'];
+ delete j['_id'];
+ delete j['id'];
+ }
+ csv = '';
+ line = columns.join(',') + '\r\n';
+ i = 0;
+ while (i < json.length) {
+ for (index in json[i]) {
+ if (typeof json[i][index] === 'object') {
+ for (_j = 0, _len2 = columns.length; _j < _len2; _j++) {
+ column = columns[_j];
+ if (json[i][index]["" + column] !== void 0) {
+ line += json[i][index]["" + column] + ",";
+ }
+ if (json[i][index]["" + column] === null || json[i][index]["" + column] === void 0) {
+ line += ",";
+ }
+ }
+ } else {
+ line += '\r\n';
+ i = i + 1;
+ }
+ }
+ }
+ line.slice(0, line.Length - 1);
+ csv += line + "\r\n";
+ return cb(null, csv);
+ };
+
+ return JsonCsv;
+
+ })(require('events').EventEmitter);
+
+ module.exports = new JsonCsv();
+
+}).call(this);
View
17 package.json
@@ -1,12 +1,20 @@
{
"name": "jsoncsv",
"description": "A JSON to csv converter for nodeJS",
- "version": "0.0.1-2",
+ "version": "0.0.2",
"homepage": "",
"author": "Kris Windham <krsiwindham@gmail.com>",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/beautifulnode/bam.git"
+ },
"private": false,
"main": "./lib",
- "dependencies": {},
+ "dependencies": {
+ "flatiron": "*",
+ "pkginfo": "*",
+ "path": "*"
+ },
"devDependencies": {
"coffee-script": ">= 1.1.x",
"mocha": "*",
@@ -14,6 +22,9 @@
"assert": "*",
"docco": ">0"
},
+ "bin": {
+ "jsoncsv": "./bin/jsoncsv"
+ },
"engines": {
"node": "0.4 || 0.6 || 0.7"
},
@@ -25,4 +36,4 @@
"scripts": {
"test": "cake test"
}
-}
+}
View
29 src/cli.coffee
@@ -0,0 +1,29 @@
+# Convert Command
+cmdConvert = require './convert'
+
+flatiron = require 'flatiron'
+path = require 'path'
+require('pkginfo')(module)
+
+app = flatiron.app
+#app.config.file({ file: path.join(__dirname, 'config', 'config.json') })
+app.version = exports.version
+
+app.use flatiron.plugins.cli,
+ source: path.join(__dirname, 'lib')
+ usage: [
+ ""
+ "jsoncsv v#{app.version}"
+ "Converts JSON to CSV"
+ ""
+ "jsoncsv convert '{\"some\": \"json\"}'"
+ ]
+ version: true
+
+app.cmd 'version', ->
+ console.log 'jsoncsv v' + app.version
+
+app.cmd 'convert', cmdConvert
+
+app.start()
+
View
15 src/convert.coffee
@@ -0,0 +1,15 @@
+require './jsoncsv'
+log = console.log
+
+parseJson = (json, cb) ->
+ log 'Parsing JSON...'
+ log json
+ cb()
+
+module.exports = (json, cb) ->
+ unless json?
+ return console.log('JSON Required!\nex: jsoncsv convert \'{"some":"json"}\'')
+ else
+ return console.log json
+ parseJson json, -> cb()
+
View
50 src/index.coffee
@@ -1,37 +1,25 @@
-# jsoncsv
-# json to csv converter
+# Commands
+cmdConvert = require './convert'
-class JsonCsv extends require('events').EventEmitter
- # parse JSON data and return csv output
- # callback returns (err, row)
+flatiron = require 'flatiron'
+require('pkginfo')(module)
+app = flatiron.app
- parse: (data, columns = {}, cb) ->
- # columns should be array of json nodes
- return cb('error no data') unless data?
+app.version = exports.version
- json = JSON.parse(data)
- for j in json
- delete j['_events']
- delete j['_id']
- delete j['id']
- csv = ''
- line = columns.join(',') + '\r\n'
- i = 0
- while i < json.length
- for index of json[i]
- if typeof(json[i][index]) == 'object'
- for column in columns
- line += json[i][index]["#{column}"] + "," if json[i][index]["#{column}"] != undefined
- line += "," if json[i][index]["#{column}"] == null or json[i][index]["#{column}"] == undefined
- else
- line += '\r\n'
- i = i + 1
- line.slice 0, line.Length - 1
- csv += line + "\r\n"
- return cb(null, csv)
-
-
-module.exports = new JsonCsv()
+app.use flatiron.plugins.cli,
+ dir: __dirname
+ usage: [
+ "jsoncsv v#{app.version}"
+ "Converts JSON to CSV"
+ ""
+ "jsoncsv convert [your JSON]"
+ ]
+ version: true
+app.cmd 'version', ->
+ console.log 'jsoncsv v' + app.version
+app.cmd 'convert', cmdConvert
+app.start()
View
37 src/jsoncsv.coffee
@@ -0,0 +1,37 @@
+# jsoncsv
+# json to csv converter
+class JsonCsv extends require('events').EventEmitter
+ # parse JSON data and return csv output
+ # callback returns (err, row)
+
+ parse: (data, columns = {}, cb) ->
+ # columns should be array of json nodes
+ return cb('error no data') unless data?
+
+ json = JSON.parse(data)
+ for j in json
+ delete j['_events']
+ delete j['_id']
+ delete j['id']
+ csv = ''
+ line = columns.join(',') + '\r\n'
+ i = 0
+ while i < json.length
+ for index of json[i]
+ if typeof(json[i][index]) == 'object'
+ for column in columns
+ line += json[i][index]["#{column}"] + "," if json[i][index]["#{column}"] != undefined
+ line += "," if json[i][index]["#{column}"] == null or json[i][index]["#{column}"] == undefined
+ else
+ line += '\r\n'
+ i = i + 1
+ line.slice 0, line.Length - 1
+ csv += line + "\r\n"
+ return cb(null, csv)
+
+
+module.exports = new JsonCsv()
+
+
+
+

0 comments on commit 9cda744

Please sign in to comment.
Something went wrong with that request. Please try again.