├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── main.js /.gitattributes: -------------------------------------------------------------------------------- 1 | *.* text -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Node.js 2 | node_modules 3 | npm-debug.log 4 | *.zip 5 | *.rar 6 | # Local dirs and files 7 | vendor 8 | temp 9 | tmp 10 | TODO.md 11 | *.sublime-* -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "asi": false, 3 | "boss": true, 4 | "curly": true, 5 | "eqeqeq": true, 6 | "eqnull": true, 7 | "esnext": true, 8 | "immed": true, 9 | "latedef": false, 10 | "laxcomma": false, 11 | "mocha": true, 12 | "newcap": true, 13 | "noarg": true, 14 | "node": true, 15 | "sub": true, 16 | "undef": true, 17 | "unused": true 18 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.8" 4 | before_script: 5 | - npm install -g grunt-cli -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Brian Woodward 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-convert [![NPM version][npm-image]][npm-url] [![Build status][build-image]][build-url] 2 | 3 | ## Work In Progress 4 | This plugin is not completely ready yet. 5 | 6 | ## Usage 7 | 8 | First, install `gulp-convert` as a development dependency: 9 | 10 | ```shell 11 | npm install --save-dev gulp-convert 12 | ``` 13 | 14 | Then, add it to your `gulpfile.js`: 15 | 16 | ```javascript 17 | var convert = require('gulp-convert'); 18 | 19 | gulp.task('csv2json', function(){ 20 | gulp.src(['data/csv/*.csv']) 21 | .pipe(convert({ 22 | from: 'csv', 23 | to: 'json' 24 | })) 25 | .pipe(gulp.dest('data/json/')); 26 | }); 27 | ``` 28 | 29 | ## API 30 | 31 | ### convert(options) 32 | 33 | #### options.from 34 | Type: `String` 35 | Default: `csv` 36 | 37 | File type converting from. 38 | 39 | #### options.to 40 | Type: `String` 41 | Default: `json` 42 | 43 | File type converting to. 44 | 45 | [build-url]: https://github.com/assemble/gulp-convert 46 | [build-image]: https://github.com/assemble/gulp-convert.png 47 | [npm-url]: https://npmjs.org/package/gulp-convert 48 | [npm-image]: https://badge.fury.io/js/gulp-convert.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * gulp-convert 3 | * 4 | * Copyright (c) 2013-2015, Brian Woodward. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | var convert = require('converter'); 9 | var gutil = require('gulp-util'); 10 | var es = require('event-stream'); 11 | var stream = require('stream'); 12 | 13 | var createReader = function(lines) { 14 | var reader = new stream.Readable(); 15 | reader._read = function() { 16 | lines.map(function(line) { 17 | reader.push(line); 18 | }); 19 | reader.push(null); 20 | }; 21 | 22 | return reader; 23 | }; 24 | 25 | var createWriter = function(finish) { 26 | var buffer = ''; 27 | var writer = new stream.Writable(); 28 | writer._write = function(chunk, enc, next) { 29 | buffer += chunk; 30 | next(); 31 | }; 32 | 33 | writer.toString = function() { 34 | return buffer; 35 | }; 36 | 37 | writer.on('finish', finish); 38 | return writer; 39 | }; 40 | 41 | /** 42 | * Do the conversion. 43 | * Most of this plugin code comes from the gulp 44 | * example. 45 | * 46 | * @param {[Object]} options [List of options to use] 47 | * @return {[Stream]} [List of File objects] 48 | */ 49 | module.exports = function(options) { 50 | var opts = options || {}; 51 | 52 | function modifyContents(file, cb) { 53 | var reader = createReader([String(file.contents)]); 54 | var writer = createWriter(function() { 55 | file.path = gutil.replaceExtension(file.path, '.' + opts.to); 56 | file.contents = new Buffer(writer.toString()); 57 | cb(null, file); 58 | }); 59 | 60 | reader.pipe(convert(opts)).pipe(writer); 61 | } 62 | 63 | return es.map(modifyContents); 64 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-convert", 3 | "description": "Gulp plugin to convert to or from JSON, YAML, XML, PLIST or CSV.", 4 | "version": "0.2.0", 5 | "author": "Brian Woodward", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/assemble/gulp-convert.git" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/assemble/gulp-convert/issues" 12 | }, 13 | "licenses": [ 14 | "MIT" 15 | ], 16 | "main": "index.js", 17 | "files": [ 18 | "index.js" 19 | ], 20 | "scripts": { 21 | "test": "mocha" 22 | }, 23 | "dependencies": { 24 | "converter": "0.0.5", 25 | "event-stream": "~3.2.2", 26 | "gulp-util": "~3.0.3" 27 | }, 28 | "devDependencies": { 29 | "mocha": "~2.1.0", 30 | "should": "~5.0.0" 31 | }, 32 | "keywords": [ 33 | "gulpplugin", 34 | "gulp", 35 | "convert" 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var File = require('gulp-util').File; 4 | var Buffer = require('buffer').Buffer; 5 | 6 | var convert = require('../'); 7 | 8 | describe('gulp-convert', function() { 9 | 10 | it('should convert csv to json', function(done) { 11 | var options = { 12 | from: 'csv', 13 | to: 'json' 14 | }; 15 | 16 | var converter = convert(options); 17 | 18 | var fakeFile = new File({ 19 | cwd: "/home/contra/", 20 | base: "/home/contra/test", 21 | path: "/home/contra/test/file.csv", 22 | contents: new Buffer("first,second,third\none,two,three\n4,5,6") 23 | }); 24 | 25 | converter.on('data', function(data) { 26 | var json = JSON.parse(data.contents); 27 | assert.deepEqual(json, [ 28 | { first: 'one', second: 'two', third: 'three' }, 29 | { first: '4', second: '5', third: '6' } 30 | ]); 31 | 32 | done(); 33 | }); 34 | 35 | converter.write(fakeFile); 36 | converter.end(); 37 | 38 | }); 39 | 40 | }); 41 | --------------------------------------------------------------------------------