+29
| var gulp = require('gulp'); | ||
| var gutil = require('gulp-util'); | ||
| var mocha = require('gulp-mocha'); | ||
| var coffee = require('gulp-coffee'); | ||
| gulp.task('coffee', function() { | ||
| gulp.src('./src/**/*.coffee') | ||
| .pipe(coffee({bare: true}).on('error', gutil.log)) | ||
| .pipe(gulp.dest('./lib/')) | ||
| }); | ||
| gulp.task('copy-templates', function(){ | ||
| gulp.src('./src/**/*.hbs') | ||
| .pipe(gulp.dest('./lib/')); | ||
| }); | ||
| gulp.task('test', function(){ | ||
| gulp.src('./test/index.js') | ||
| .pipe(mocha( | ||
| { | ||
| require: ['chai', 'chai-as-promised'], | ||
| reporter: 'nyan', | ||
| growl: true | ||
| } | ||
| )); | ||
| }); | ||
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Cat", | ||
| "description": "A cat from Atoms catalog", | ||
| "type": "object", | ||
| "properties": { | ||
| "id": { | ||
| "description": "The unique identifier for a cat", | ||
| "type": "integer" | ||
| }, | ||
| "name": { | ||
| "description": "Name of the cat", | ||
| "type": "string" | ||
| }, | ||
| "ownerName":{ | ||
| "description": "Name of the owner", | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": ["id", "name"] | ||
| } |
Sorry, the diff of this file is not supported yet
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Cats", | ||
| "description": "A cats collection", | ||
| "type": "array", | ||
| "items": { "$ref": "#/cat" } | ||
| } |
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Error", | ||
| "description": "A server error", | ||
| "type": "object", | ||
| "properties": { | ||
| "error": { | ||
| "description": "The unique identifier for a cat", | ||
| "properties": { | ||
| "code": {"type": "string"}, | ||
| "messages": {"type": "string"}, | ||
| "status": {"type": "integer"} | ||
| }, | ||
| "required": ["code", "messages", "status"] | ||
| } | ||
| } | ||
| } | ||
+47
-61
@@ -1,73 +0,59 @@ | ||
| (function() { | ||
| var Handlebars; | ||
| var Handlebars; | ||
| Handlebars = require("handlebars"); | ||
| Handlebars = require("handlebars"); | ||
| module.exports.process = function(data, gen) { | ||
| var dataParsed, e, helper, obj, partial, result, template, _i, _j, _k, _len, _len1, _len2, _ref, _ref1; | ||
| if (gen.handleRender && typeof gen.handleRender === 'function') { | ||
| if (gen.helpers && gen.helpers instanceof Array) { | ||
| _ref = gen.helpers; | ||
| for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
| helper = _ref[_i]; | ||
| if (helper.name && helper.fn && typeof helper.fn === 'function') { | ||
| Handlebars.registerHelper(helper.name, helper.fn); | ||
| } else { | ||
| console.error("helpers not are in incorrect format"); | ||
| } | ||
| module.exports.process = function(data, gen) { | ||
| var dataParsed, e, helper, obj, partial, result, template, _i, _j, _k, _len, _len1, _len2, _ref, _ref1; | ||
| if (gen.handleRender && typeof gen.handleRender === 'function') { | ||
| if (gen.helpers && gen.helpers instanceof Array) { | ||
| _ref = gen.helpers; | ||
| for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
| helper = _ref[_i]; | ||
| if (helper.name && helper.fn && typeof helper.fn === 'function') { | ||
| Handlebars.registerHelper(helper.name, helper.fn); | ||
| } else { | ||
| console.error("helpers not are in incorrect format"); | ||
| } | ||
| } | ||
| if (gen.partials) { | ||
| _ref1 = gen.partials; | ||
| for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
| partial = _ref1[_j]; | ||
| if (partial.name && partial.str) { | ||
| Handlebars.registerPartial(partial.name, partial.str); | ||
| } | ||
| } | ||
| if (gen.partials) { | ||
| _ref1 = gen.partials; | ||
| for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
| partial = _ref1[_j]; | ||
| if (partial.name && partial.str) { | ||
| Handlebars.registerPartial(partial.name, partial.str); | ||
| } | ||
| } | ||
| if (typeof gen.parser === 'function') { | ||
| try { | ||
| dataParsed = gen.parser(data); | ||
| } catch (_error) { | ||
| e = _error; | ||
| console.log("Parser error", e); | ||
| } | ||
| } else { | ||
| dataParsed = data; | ||
| } | ||
| } | ||
| if (typeof gen.parser === 'function') { | ||
| try { | ||
| template = Handlebars.compile(gen.template); | ||
| if (Array.isArray(dataParsed)) { | ||
| result = []; | ||
| for (_k = 0, _len2 = dataParsed.length; _k < _len2; _k++) { | ||
| obj = dataParsed[_k]; | ||
| result.push({ | ||
| name: obj.name, | ||
| str: template(obj.model) | ||
| }); | ||
| } | ||
| return gen.handleRender(result); | ||
| } else { | ||
| if (dataParsed.name && dataParsed.model) { | ||
| return gen.hanleRender([ | ||
| { | ||
| name: dataParsed.name, | ||
| str: template(dataParsed.model) | ||
| } | ||
| ]); | ||
| } else { | ||
| return gen.handleRender([template(dataParsed)]); | ||
| } | ||
| } | ||
| dataParsed = gen.parser(data); | ||
| } catch (_error) { | ||
| e = _error; | ||
| console.log(e); | ||
| return null; | ||
| console.log("Parser error", e); | ||
| } | ||
| } else { | ||
| return console.error("generator.handleRender not defined"); | ||
| dataParsed = data; | ||
| } | ||
| }; | ||
| }).call(this); | ||
| template = Handlebars.compile(gen.template); | ||
| if (Array.isArray(dataParsed)) { | ||
| result = []; | ||
| for (_k = 0, _len2 = dataParsed.length; _k < _len2; _k++) { | ||
| obj = dataParsed[_k]; | ||
| if (obj.name && obj.model) { | ||
| result.push({ | ||
| name: obj.name, | ||
| content: template(obj.model) | ||
| }); | ||
| } else { | ||
| throw new Error("Data element doesn't have name or model please"); | ||
| } | ||
| } | ||
| return gen.handleRender(result); | ||
| } else { | ||
| throw new Error("Digest data is not and Array"); | ||
| } | ||
| } else { | ||
| throw new Error("generator.handleRender not defined"); | ||
| } | ||
| }; |
+6
-13
| { | ||
| "name": "data2code", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "description": "Codegenerator for data", | ||
@@ -10,16 +10,9 @@ "main": "lib/data2code.js", | ||
| "devDependencies": { | ||
| "coffeeify": "~0.6.0", | ||
| "grunt": "~0.4.5", | ||
| "grunt-contrib-clean": "~0.4.0", | ||
| "grunt-cli": "~0.1.13", | ||
| "grunt-coffeelint": "0.0.10", | ||
| "grunt-contrib-coffee": "~0.10.1", | ||
| "load-grunt-tasks": "~0.6.0", | ||
| "gulp": "3.8.7", | ||
| "gulp-coffee": "2.2.0", | ||
| "gulp-mocha" : "1.0.0", | ||
| "chai": "~1.9.1", | ||
| "mocha": "~1.20.1", | ||
| "grunt-mocha-cli": "~1.9.0", | ||
| "chai-as-promised": "~4.1.1", | ||
| "raml-parser": "~0.8.7", | ||
| "grunt-contrib-watch": "~0.6.1", | ||
| "grunt-contrib-copy": "~0.5.0" | ||
| "raml-parser": "0.8.7", | ||
| "gulp-util": "3.0.1" | ||
| }, | ||
@@ -26,0 +19,0 @@ "scripts": { |
+12
-1
@@ -15,5 +15,16 @@ # data to code generator | ||
| * template -> Handlebars template. | ||
| * parser(data) -> Function it receives RAML parsed data, returns parsed data | ||
| The parser must return and array of object each object must have name and content properties | ||
| Sample: | ||
| ```javascript | ||
| [ { name: "test.test", | ||
| model: { | ||
| title:data.title + " finos" | ||
| } | ||
| } | ||
| ] | ||
| ``` | ||
| * Optional properties: | ||
| * handleRender([str]) -> This function handles the render results, usually writes to disk. | ||
| * parser(data) -> Function it receives RAML parsed data, returns parsed data. | ||
| * helpers -> Handlebars helpers. | ||
@@ -20,0 +31,0 @@ * partials -> Handlebars partials. |
+13
-17
| Handlebars = require("handlebars") | ||
| module.exports.process = (data , gen) -> | ||
| # console.log "data2Code#process.gen", gen | ||
| # console.log "Data2Code#process.data", data | ||
| if gen.handleRender and typeof gen.handleRender is 'function' | ||
@@ -27,19 +25,17 @@ | ||
| dataParsed = data | ||
| try | ||
| template = Handlebars.compile(gen.template) | ||
| if Array.isArray dataParsed | ||
| result = [] | ||
| for obj in dataParsed | ||
| result.push {name: obj.name, str :template(obj.model)} | ||
| gen.handleRender(result) | ||
| else | ||
| if dataParsed.name and dataParsed.model | ||
| gen.hanleRender([{name:dataParsed.name,str: template(dataParsed.model) }]) | ||
| template = Handlebars.compile(gen.template) | ||
| if Array.isArray dataParsed | ||
| result = [] | ||
| for obj in dataParsed | ||
| if obj.name and obj.model | ||
| result.push {name: obj.name, content: template(obj.model)} | ||
| else | ||
| gen.handleRender([template(dataParsed)]) | ||
| catch e | ||
| console.log e | ||
| null | ||
| throw new Error("Data element doesn't have name or model") | ||
| gen.handleRender(result) | ||
| else | ||
| throw new Error("Digest data is not and Array") | ||
| else | ||
| console.error "generator.handleRender not defined" | ||
| throw new Error("generator.handleRender not defined") | ||
@@ -46,0 +42,0 @@ |
+32
-30
| 'use strict'; | ||
| var raml = require('raml-parser'), expect = require('chai').expect; | ||
| var data2Code = new require('../lib/data2code'); | ||
| var data2Code = require('..'); | ||
@@ -9,3 +9,4 @@ describe('data2code basic test', function () { | ||
| try { | ||
| expect(result[0]).to.be.equal(expectedResult); | ||
| result[0].content.should.equal(expectedResult); | ||
| result[0].name.should.equal("test.test"); | ||
| done(); | ||
@@ -17,2 +18,4 @@ } catch (x) { | ||
| var sampleData = [{ name: "test.test", model: {title: "Compra venta de gatitos"}}]; | ||
| it('should generate something', function (done) { | ||
@@ -23,10 +26,5 @@ var simpleGen = {}; | ||
| raml.loadFile('./test/cats.raml').then(function (data) { | ||
| data2Code.process(data, simpleGen); | ||
| }, function (error) { | ||
| console.log('Error parsing: ' + error); | ||
| }); | ||
| data2Code.process(sampleData, simpleGen); | ||
| }); | ||
| it('testing parser', function (done) { | ||
@@ -36,13 +34,25 @@ var simpleGen = {}; | ||
| simpleGen.parser = function (data) { | ||
| return {title: data.title + " finos"} | ||
| return sampleData | ||
| }; | ||
| simpleGen.handleRender = handleRender.bind(undefined, done, "Compra venta de gatitos finos"); | ||
| simpleGen.handleRender = handleRender.bind(undefined, done, "Compra venta de gatitos"); | ||
| data2Code.process({}, simpleGen); | ||
| raml.loadFile('./test/cats.raml').then(function (data) { | ||
| data2Code.process(data, simpleGen); | ||
| }, function (error) { | ||
| console.log('Error parsing: ' + error); | ||
| }); | ||
| }); | ||
| it('testing parser when returning Arrasy', function (done) { | ||
| var simpleGen = {}; | ||
| simpleGen.template = '{{title}}'; | ||
| simpleGen.parser = function (data) { | ||
| return [{ name: "test.test", model: {title:data[0].model.title + " finos"}}] | ||
| }; | ||
| simpleGen.handleRender = function(data){ | ||
| data[0].content.should.equal("Compra venta de gatitos finos"); | ||
| data[0].name.should.equal("test.test"); | ||
| done() | ||
| }; | ||
| data2Code.process(sampleData, simpleGen); | ||
| }); | ||
@@ -54,14 +64,11 @@ | ||
| simpleGen.helpers = [ | ||
| {name: 'msg', fn: function (msg) { | ||
| return 'Renta, ' + msg | ||
| } } | ||
| {name: 'msg', | ||
| fn: function (msg) { | ||
| return 'Renta, ' + msg | ||
| } | ||
| } | ||
| ]; | ||
| simpleGen.handleRender = handleRender.bind(undefined, done, "Renta, Compra venta de gatitos"); | ||
| data2Code.process(sampleData, simpleGen); | ||
| raml.loadFile('./test/cats.raml').then(function (data) { | ||
| data2Code.process(data, simpleGen); | ||
| }, function (error) { | ||
| console.log('Error parsing: ' + error); | ||
| }); | ||
| }); | ||
@@ -76,9 +83,4 @@ | ||
| simpleGen.handleRender = handleRender.bind(undefined, done, "Testing partial"); | ||
| data2Code.process(sampleData, simpleGen); | ||
| raml.loadFile('./test/cats.raml').then(function (data) { | ||
| data2Code.process(data, simpleGen); | ||
| }, function (error) { | ||
| console.log('Error parsing: ' + error); | ||
| }); | ||
| }); | ||
@@ -85,0 +87,0 @@ |
+2
-2
@@ -7,5 +7,5 @@ 'use strict'; | ||
| var mocha = new Mocha(); | ||
| mocha.addFile(__dirname + '/data2Code-test'); | ||
| mocha.addFile(__dirname + '/data2Code-spec'); | ||
| chai.should(); | ||
| mocha.run(); | ||
| mocha.run(); |
-78
| module.exports = function(grunt) { | ||
| require('load-grunt-tasks')(grunt); | ||
| grunt.initConfig({ | ||
| pkg: grunt.file.readJSON('package.json'), | ||
| copy: { | ||
| main: { | ||
| files: [ | ||
| {expand: true, cwd: 'src/generators', src: ['*.hbs', '**/*.hbs'], dest: 'lib/generators', filter: 'isFile'} | ||
| ] | ||
| } | ||
| }, | ||
| coffee: { | ||
| compile : { | ||
| files: { | ||
| 'lib/data2code.js' : 'src/data2code.coffee', | ||
| } | ||
| } | ||
| }, | ||
| watch: { | ||
| coffee: { | ||
| files: ['src/*.coffee', 'src/**/*.coffee', 'src/**/*.hbs' ], | ||
| tasks: ['copy', 'test'] | ||
| }, | ||
| test: { | ||
| files: ['test/*.js'], | ||
| tasks: ['copy','test'] | ||
| } | ||
| }, | ||
| coffeelint: { | ||
| options: { | ||
| max_line_length: { | ||
| level: 'ignore' | ||
| } | ||
| }, | ||
| app: ['src/*.coffee', 'src/**/*.coffee'] | ||
| }, | ||
| mochacli: { | ||
| options: { | ||
| require: ['chai', 'chai-as-promised'], | ||
| reporter: 'dot', | ||
| bail: true, | ||
| growl: true, | ||
| grep: grunt.option('grep') || '' | ||
| }, | ||
| all: ['test/*spec.js'] | ||
| }, | ||
| clean: { | ||
| build: ["lib"] | ||
| } | ||
| }); | ||
| grunt.registerTask('compile', [ | ||
| 'coffeelint', | ||
| 'coffee' | ||
| ]); | ||
| grunt.registerTask('build', [ | ||
| 'clean', | ||
| 'compile', | ||
| 'copy' | ||
| ]); | ||
| grunt.registerTask('test', [ | ||
| 'build', | ||
| 'mochacli' | ||
| ]); | ||
| grunt.registerTask('default', [ | ||
| 'build', 'watch' | ||
| ]); | ||
| }; |
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Cat", | ||
| "description": "A cat from Atoms catalog", | ||
| "type": "object", | ||
| "properties": { | ||
| "id": { | ||
| "description": "The unique identifier for a cat", | ||
| "type": "integer" | ||
| }, | ||
| "name": { | ||
| "description": "Name of the cat", | ||
| "type": "string" | ||
| }, | ||
| "ownerName":{ | ||
| "description": "Name of the owner", | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": ["id", "name"] | ||
| } |
Sorry, the diff of this file is not supported yet
| { | ||
| "$schema": "http://json-schema.org/draft-04/schema#", | ||
| "title": "Cats", | ||
| "description": "A cats collection", | ||
| "type": "array", | ||
| "items": { "$ref": "#/cat" } | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
7
-50%13
8.33%53
26.19%0
-100%11421
-6.02%197
-16.17%1
Infinity%