From 85331df7d5c28e53c67f656833a1fa3ec3f22e96 Mon Sep 17 00:00:00 2001 From: Julien PALAS Date: Fri, 26 Feb 2016 14:59:40 +0100 Subject: [PATCH 1/4] Modification of index.js to manage the collection type and allow to render it recursively --- index.js | 183 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 104 insertions(+), 79 deletions(-) diff --git a/index.js b/index.js index adc70b1..a7de07e 100644 --- a/index.js +++ b/index.js @@ -1,104 +1,129 @@ #!/usr/bin/env node + //sails-viewify modelname viewname config.js -// sails-viewify modelname viewname config.js var fs = require('fs'); var path = require('path'); var str = ''; jsStringEscape = require('js-string-escape'); // Init -if(process.argv[2].localeCompare("init") === 0) { - fs.exists('./.sailsrc', function(isSails){ - if(!isSails) - throw new Error('Not a sails project'); - fs.createReadStream(path.join( __dirname , 'config.js')) - .pipe(fs.createWriteStream( path.join(process.cwd() , 'config/sails-viewify.js'))); - fs.writeFileSync('viewify_input.txt' , ''); - }); +if (process.argv[2].localeCompare("init") === 0) { + fs.exists('./.sailsrc', function(isSails) { + if (!isSails) + throw new Error('not a sails project'); + fs.createReadStream(path.join(__dirname, 'config.js')) + .pipe(fs.createWriteStream(path.join(process.cwd(), 'config/sails-viewify.js'))); + fs.writeFileSync('viewify_input.txt', ''); + }); } // Escape -else if(process.argv[2].localeCompare("escape") === 0) { - fs.exists('./.sailsrc', function(isSails) { - if(!isSails) - throw new Error('Not a sails project'); - fs.readFile('viewify_input.txt', { encoding : 'utf8'}, function(err, data) { - var mystr = jsStringEscape(data); - fs.writeFile('viewify_output.txt', mystr, { encoding : 'utf8'}, function(err, data) { - if(err) - throw err; - }); +else if (process.argv[2].localeCompare("escape") === 0) { + fs.exists('./.sailsrc', function(isSails) { + if (!isSails) + throw new Error('not a sails project'); + fs.readFile('viewify_input.txt', { encoding: 'utf8' }, function(err, data) { + var mystr = jsStringEscape(data); + fs.writeFile('viewify_output.txt', mystr, { encoding: 'utf8' }, function(err, data) { + if (err) + throw err; + }); + }); }); - }); -} +} else { + // Other than init & escape + // Check if init is done + fs.exists(path.join(process.cwd(), 'config/sails-viewify.js'), function(isInit) { + if (!isInit) + new Error('sails-viewify not initialized. Try sails-viewify init'); + else { + if (process.argv[2] === undefined) + throw new Error('Specify source filename'); -else { - // Other than init & escape - // Check if init is done - fs.exists(path.join(process.cwd(), 'config/sails-viewify.js'), function(isInit) { - if(!isInit) - new Error('sails-viewify not initialized. Try sails-viewify init'); - else { - if(process.argv[2] === undefined) - throw new Error('Specify source filename'); + if (process.argv[3] === undefined) + throw new Error('Specify destination filename'); - if(process.argv[3] === undefined) - throw new Error('specify destination filename'); + var config; + if (process.argv[4] === undefined) + config = require(path.join(process.cwd(), 'config/sails-viewify.js')); + else + config = require('./' + process.argv[4]); - var config; - if(process.argv[4] === undefined) - config = require(path.join(process.cwd() , 'config/sails-viewify.js')); - else - config = require('./' + process.argv[4]); + // Dependent requires + var model = require(path.join(process.cwd(), 'api', 'models', process.argv[2] + '.js')); + var attributes = model.attributes; + var template = config.template; + var destFile = path.join('views', process.argv[3]); - // Dependent requires - var model = require(path.join(process.cwd(), 'api', 'models', process.argv[2] + '.js')); - var attributes = model.attributes ; - var template = config.template; - var destFile = path.join('views' , process.argv[3]); + var render = function(htmltext, objName, obj, template) { + var specials = template.specials; - var render = function (htmltext, objName, obj, template) { - var specials = template.specials; + for (var i = 0; i < specials.length; i++) { + var specialObj = specials[i]; - for(var i = 0 ; i < specials.length ; i++){ - var specialObj = specials[i]; + if (specialObj.replacer.localeCompare("name") === 0) { + htmltext = htmltext.split(specialObj.text).join(objName); + } else if (obj[specialObj.replacer] !== undefined) { + htmltext = htmltext.split(specialObj.text).join(obj[specialObj.replacer]); + } + } + return htmltext; + }; - if(specialObj.replacer.localeCompare("name") === 0) { - htmltext = htmltext.split(specialObj.text).join(objName); - } - else if(obj[specialObj.replacer] !== undefined) { - htmltext = - htmltext - .split(specialObj.text) - .join(obj[specialObj.replacer]); - } - } - return htmltext; - }; - var type; - var t; - var temp; - var obj; - var index; - for(obj in attributes) { - type = attributes[obj].type; + // Loop on the Model properties and render it + var renderModel = function(attributes, template) { + var type; + var collection; + var t; + var temp; + var obj; + var index; + for (obj in attributes) { + type = attributes[obj].type; + collection = attributes[obj].collection; + for (index in template) { + t = template[index]; + // If model has model or collection instead of type , then type === undefined , so + if (type !== undefined) { + if (type.localeCompare(t.type) === 0) { + temp = render(t.htmltext, obj, attributes[obj], t) + str = str.concat(temp + '\n'); + fs.writeFileSync(destFile, str); + } + } - for(index in template) { - t = template[index]; + // If model has a collection and the template is defined + if (collection !== undefined) { + if (collection.localeCompare(t.collection) === 0) { + var collectionModel; + var collectionAttributes + //1) Write begin of the collection + if (t.beginhtmltext !== undefined) { + temp = render(t.beginhtmltext, obj, attributes[obj], t) + str = str.concat(temp + '\n'); + fs.writeFileSync(destFile, str); + } + //Get the collection model to render + collectionModel = require(path.join(process.cwd(), 'api', 'models', t.collection + '.js')); + collectionAttributes = collectionModel.attributes; + //2) Write the collection + renderModel(collectionAttributes, template); + //3) Write begin of the collection + if (t.endhtmltext !== undefined) { + temp = render(t.endhtmltext, obj, attributes[obj], t) + str = str.concat(temp + '\n'); + fs.writeFileSync(destFile, str); + } + } + } - // if model has model or collection instead of type, - // then type === undefined, so - if(type !== undefined) { - if(type.localeCompare(t.type) === 0) { - temp = render(t.htmltext, obj, attributes[obj], t); - str = str.concat(temp + '\n'); - fs.writeFileSync(destFile, str); + } + } } - } + + renderModel(attributes, template); } - } - } - }); -} \ No newline at end of file + }); +} From a93f4622ea0e2ee722364912ab2edf50d85ccbf8 Mon Sep 17 00:00:00 2001 From: Julien PALAS Date: Fri, 26 Feb 2016 15:14:25 +0100 Subject: [PATCH 2/4] Include collection explanations in documentation ReadMe --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bb48d8c..0449e11 100644 --- a/README.md +++ b/README.md @@ -124,9 +124,14 @@ module.exports = { - ``template`` - [array] specifies templates to be used for different types of attributes. Note that the type refers to the types used in the model and are available within [waterline](npmjs.com/package/waterline). - ``type`` - [string] specifies the type of the attributes used in the model. - ``htmltext`` - [string] specifies the html text to be generated for the respective attribute type. - - ``specials`` - [array] specifies the special text in the ``htmltext`` field to be replaced by the respective replacer object value + - ``collection`` - [string] specifies the collection of the attributes used in the model. + - ``beginhtmltext`` - [string] specifies the html text to be generated for the respective attribute collection. + - ``endhtmltext`` - [string] specifies the html text to be generated for the respective attribute collection. + - ``specials`` - [array] specifies the special text in the ``htmltext`` field to be replaced by the respective replacer object value. - ``text`` - [string] special text that is to be replaced by the values from the model. - ``replacer`` - [string] replacer object points to the property of the attribute's object in the model. By default , sails-viewify creates name property which equals the name of the attribute in the model.Other than this , all the properties of the attribute are available. + + That's it ! You are ready to generate your view . go ahead and execute From c4e3557494947187668ee83c7cfcfa1c0574f9ff Mon Sep 17 00:00:00 2001 From: Julien PALAS Date: Tue, 1 Mar 2016 17:09:10 +0100 Subject: [PATCH 3/4] Adding infinite loop control --- index.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a7de07e..6d04e6b 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,7 @@ else if (process.argv[2].localeCompare("escape") === 0) { // Loop on the Model properties and render it - var renderModel = function(attributes, template) { + var renderModel = function(attributes, template, renderedModel) { var type; var collection; var t; @@ -95,8 +95,15 @@ else if (process.argv[2].localeCompare("escape") === 0) { } // If model has a collection and the template is defined + + if (collection !== undefined) { - if (collection.localeCompare(t.collection) === 0) { + + + if (collection.localeCompare(t.collection) === 0 && renderedModel.indexOf(collection) < 0) { + var nextRenderedModel = renderedModel.slice(); + ; + nextRenderedModel.push(collection); var collectionModel; var collectionAttributes //1) Write begin of the collection @@ -109,21 +116,25 @@ else if (process.argv[2].localeCompare("escape") === 0) { collectionModel = require(path.join(process.cwd(), 'api', 'models', t.collection + '.js')); collectionAttributes = collectionModel.attributes; //2) Write the collection - renderModel(collectionAttributes, template); + renderModel(collectionAttributes, template, nextRenderedModel); + //3) Write begin of the collection if (t.endhtmltext !== undefined) { temp = render(t.endhtmltext, obj, attributes[obj], t) str = str.concat(temp + '\n'); fs.writeFileSync(destFile, str); } + } else { + console.log("Infinite loop not treathed"); } } + } } } - - renderModel(attributes, template); + var renderedModel = new Array(process.argv[2]); + renderModel(attributes, template, renderedModel); } }); } From ed9a57cf1067caa9255e2c2581e4b9a1cb054b5d Mon Sep 17 00:00:00 2001 From: Julien PALAS Date: Tue, 1 Mar 2016 17:19:07 +0100 Subject: [PATCH 4/4] Adding log message when infinite loop skip --- index.js | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 6d04e6b..7f47d27 100644 --- a/index.js +++ b/index.js @@ -100,32 +100,34 @@ else if (process.argv[2].localeCompare("escape") === 0) { if (collection !== undefined) { - if (collection.localeCompare(t.collection) === 0 && renderedModel.indexOf(collection) < 0) { - var nextRenderedModel = renderedModel.slice(); - ; - nextRenderedModel.push(collection); - var collectionModel; - var collectionAttributes + if (collection.localeCompare(t.collection) === 0) { + if (renderedModel.indexOf(collection) < 0) { + var nextRenderedModel = renderedModel.slice(); + var collectionModel; + var collectionAttributes; + // Adding the model for next recursive control in function renderModel() + nextRenderedModel.push(collection); //1) Write begin of the collection - if (t.beginhtmltext !== undefined) { - temp = render(t.beginhtmltext, obj, attributes[obj], t) - str = str.concat(temp + '\n'); - fs.writeFileSync(destFile, str); + if (t.beginhtmltext !== undefined) { + temp = render(t.beginhtmltext, obj, attributes[obj], t) + str = str.concat(temp + '\n'); + fs.writeFileSync(destFile, str); + } + //Get the collection model to render + collectionModel = require(path.join(process.cwd(), 'api', 'models', t.collection + '.js')); + collectionAttributes = collectionModel.attributes; + //2) Write the collection + renderModel(collectionAttributes, template, nextRenderedModel); + + //3) Write begin of the collection + if (t.endhtmltext !== undefined) { + temp = render(t.endhtmltext, obj, attributes[obj], t) + str = str.concat(temp + '\n'); + fs.writeFileSync(destFile, str); + } + } else { + console.log("Warning : sails-viewify try to render collection generating an infinite loop. (model : " + collection + " in model: " + renderedModel[renderedModel.length - 1]+")"); } - //Get the collection model to render - collectionModel = require(path.join(process.cwd(), 'api', 'models', t.collection + '.js')); - collectionAttributes = collectionModel.attributes; - //2) Write the collection - renderModel(collectionAttributes, template, nextRenderedModel); - - //3) Write begin of the collection - if (t.endhtmltext !== undefined) { - temp = render(t.endhtmltext, obj, attributes[obj], t) - str = str.concat(temp + '\n'); - fs.writeFileSync(destFile, str); - } - } else { - console.log("Infinite loop not treathed"); } }