titan-cli
Version:
Command Line Interface for TitanJS Scaffolding
119 lines (107 loc) • 3.42 kB
JavaScript
/**
* @public
* @controller
* @class
* @constructor
* @param {!String} viewname
* @param {!String} binpath
*/
var ViewManager = function(viewname, binpath) {
//noinspection All
var self = this,
path = require('path'),
fs = require('fs'),
mu = require('mu2'),
viewTemplateName = "vtemplate.html",
viewmodelTemplateName = "vmtemplate.js",
templatesPath = path.join(binpath, 'templates'),
belt = require('../misc/utilbelt');
//noinspection JSUnusedGlobalSymbols
/**
* @public
* @method
*/
this.makeJob = function() {
self.createView();
};
/**
* @public
* @method
* @param templatename
* @param scaffoldFilename
* @param projectRelativePath
* @param hashmap
* @param atSuccess
*/
this.scaffold = function(templatename, scaffoldFilename, projectRelativePath, hashmap, atSuccess) {
var compiledText = "";
mu.root = templatesPath;
var stream = mu.compileAndRender(templatename, hashmap);
//noinspection JSUnresolvedFunction
stream.on('data', function(data) {
compiledText += data.toString();
});
//noinspection JSUnresolvedFunction
stream.on('end', function() {
//noinspection All
var newFilePath = path.join(projectRelativePath, scaffoldFilename);
//noinspection All
fs.exists(projectRelativePath, function(exists) {
function writeProcess() {
//noinspection JSUnresolvedFunction
fs.writeFile(newFilePath, compiledText, function(error) {
if (error) throw error;
console.log("Success!");
belt.attempt(atSuccess);
});
}
if (exists) {
writeProcess();
} else {
console.log("'" + projectRelativePath + "' directory doesn't exist");
//noinspection JSUnresolvedFunction
fs.mkdir(projectRelativePath, function() {
console.log("'" + projectRelativePath + "' directory has been created. Trying again...");
writeProcess();
});
}
});
});
};
/**
* @public
* @method
*/
this.createView = function() {
var hashMap = {
'chaptername': viewname.toLowerCase()
};
console.log("Creating view for '" + viewname + "'");
self.scaffold(viewTemplateName, viewname.toLowerCase() + ".html", "views", hashMap, function() {
self.createViewModel();
});
};
/**
* @public
* @method
*/
this.createViewModel = function() {
var hashMap = {
'chaptername': viewname
};
console.log("Creating viewmodel for '" + viewname + "'");
self.scaffold(viewmodelTemplateName, viewname + "ViewModel.js", "viewmodels", hashMap, function() {
console.log("The chapter has been created successfully");
});
};
/**
* @public
* @method
* @returns {string}
*/
this.holaMundo = function() {
return "Hola Mundox";
};
};
//noinspection JSUnresolvedVariable
module.exports = ViewManager;