Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
214 changes: 126 additions & 88 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,142 @@
#!/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');

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]);

// 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;

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;
};


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');

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]);

var render = function (htmltext, objName, obj, template) {
var specials = template.specials;

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;
};

var type;
var t;
var temp;
var obj;
var index;
for(obj in attributes) {
type = attributes[obj].type;

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);
// Loop on the Model properties and render it
var renderModel = function(attributes, template, renderedModel) {
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);
}
}

// If model has a collection and the template is defined


if (collection !== undefined) {


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);
}
//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]+")");
}
}
}


}
}
}
}
var renderedModel = new Array(process.argv[2]);
renderModel(attributes, template, renderedModel);
}
}
}
});
}
});
}