gulp-csvtojson
Advanced tools
Comparing version 0.1.0 to 0.2.0
73
index.js
'use strict'; | ||
var path = require('path'); | ||
var gutil = require('gulp-util'); | ||
var through = require('through2'); | ||
var Converter=require("csvtojson").core.Converter; | ||
var path = require('path'); | ||
var Converter = require("csvtojson").Converter; | ||
var processStream = function(file,options,cb) { | ||
var csvConverter = new Converter(options); | ||
file.contents = file.contents.pipe(csvConverter); | ||
cb(); | ||
}; | ||
var processBuffer = function(file,options,cb) { | ||
var csvConverter = new Converter(options); | ||
csvConverter.fromString(file.contents.toString(), function(err, jsonObj) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
var output = JSON.stringify(jsonObj); | ||
file.contents = new Buffer(output); | ||
cb(); | ||
}); | ||
}; | ||
module.exports = function (options) { | ||
if (typeof(options) === "undefined") { | ||
options = { genjs: false }; | ||
options = { }; | ||
} | ||
// if (!options.foo) { | ||
// throw new gutil.PluginError('gulp-csvtojson', '`foo` required'); | ||
// } | ||
return through.obj(function (file, enc, cb) { | ||
var self = this; | ||
if (file.isNull()) { | ||
cb(null, file); | ||
return; | ||
return cb(null, file); | ||
} | ||
try { | ||
var done = function(err) { | ||
if (err) { throw err; } | ||
file.path = gutil.replaceExtension(file.path,'.json'); | ||
self.push(file); | ||
return cb(); | ||
}; | ||
if (file.isStream()) { | ||
cb(new gutil.PluginError('gulp-csvtojson', 'Streaming not supported')); | ||
return; | ||
} | ||
if (file.isStream()) { | ||
return processStream(file,options,done); | ||
} | ||
try { | ||
if (file.isBuffer()) { | ||
return processBuffer(file,options,done); | ||
} | ||
var csvConverter = new Converter(options); | ||
var gulpobj = this; | ||
csvConverter.fromString(file.contents.toString(), function(err, jsonObj) { | ||
var output = JSON.stringify(jsonObj); | ||
var variablename = path.basename(file.path, path.extname(file.path)); | ||
file.path = file.path.slice(0, file.path.indexOf('.')) + ".json" | ||
if (typeof(options.globalvariable) !== "undefined" && options.globalvariable !== null) { | ||
variablename = options.globalvariable; | ||
} | ||
if (options.genjs) { | ||
file.path = file.path.slice(0, file.path.indexOf('.')) + ".js" | ||
output = variablename + "=" + output + ";"; | ||
} | ||
file.contents = new Buffer(output); | ||
gulpobj.push(file); | ||
cb(); | ||
}); | ||
} catch (err) { | ||
@@ -52,4 +60,3 @@ this.emit('error', new gutil.PluginError('gulp-csvtojson', err)); | ||
} | ||
}); | ||
}; |
{ | ||
"name": "gulp-csvtojson", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "convert csv to json", | ||
@@ -28,9 +28,11 @@ "license": "MIT", | ||
"dependencies": { | ||
"csvtojson": "^0.3.21", | ||
"gulp-util": "^3.0.4", | ||
"through2": "^0.6.5" | ||
"csvtojson": "^0.5.14", | ||
"gulp-util": "^3.0.7", | ||
"through2": "^2.0.1", | ||
"vinyl": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"concat-stream": "^1.5.1", | ||
"mocha": "*" | ||
} | ||
} |
@@ -15,2 +15,4 @@ # gulp-csvtojson [](https://travis-ci.org/wizicer/gulp-csvtojson) | ||
Here is the code snippet for general usage which generate json for you. | ||
```js | ||
@@ -22,3 +24,3 @@ var gulp = require('gulp'); | ||
return gulp.src('src/file.csv') | ||
.pipe(csvtojson({genjs: true})) | ||
.pipe(csvtojson({ toArrayString: true })) | ||
.pipe(gulp.dest('dist')); | ||
@@ -28,24 +30,31 @@ }); | ||
Here is the code snippet for generating js file. | ||
## API | ||
```js | ||
var gulp = require('gulp'); | ||
var csvtojson = require('gulp-csvtojson'); | ||
var insert = require('gulp-insert'); | ||
var ext_replace = require('gulp-ext-replace'); | ||
### csvtojson(options) | ||
gulp.task('default', function () { | ||
return gulp.src('src/file.csv') | ||
.pipe(csvtojson({ toArrayString: true })) | ||
.pipe(insert.prepend('var anyVariable = ')) | ||
.pipe(insert.append(';')) | ||
.pipe(ext_replace('.js')) | ||
.pipe(gulp.dest('dist')); | ||
}); | ||
``` | ||
#### options | ||
## Options | ||
##### genjs | ||
Refer to [this list](https://github.com/Keyang/node-csvtojson#params) for full options. | ||
Type: `bool` | ||
Default: `false` | ||
**Note**: to get a valid json file, you need set `toArrayString` to `true`. | ||
When set true, output `.js` file with global variable assignment. | ||
## API | ||
##### globalvariable | ||
Since v0.2.0, API no longer supported, to achieve the same result, please adapt your code according | ||
to the second code snippet. | ||
Type: `string` | ||
Default: `null` | ||
The name which global variable will be assigned the json object. | ||
Only available when `genjs` is set to `true`. | ||
## License | ||
@@ -52,0 +61,0 @@ |
Sorry, the diff of this file is not supported yet
5036
49
62
4
2
+ Addedvinyl@^1.1.1
+ Addedasync@1.5.2(transitive)
+ Addedcsvtojson@0.5.14(transitive)
+ Addedvinyl@1.2.0(transitive)
- Removedcsvtojson@0.3.21(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedthrough2@0.6.5(transitive)
Updatedcsvtojson@^0.5.14
Updatedgulp-util@^3.0.7
Updatedthrough2@^2.0.1