ria
Version:
Node tool for developing RIA Apps using the RIA app framework. Helps initialize the app and create modules using UI templates and archetecture.
491 lines (438 loc) • 13.1 kB
JavaScript
var fs = require('fs'),
path = require('path'),
config = {
modulePath : 'app/module/',
corePath : 'app/core/',
root : process.cwd()
},
files = {
app:{
index : {
name:'index.html'
}
},
core:{
css : {
name:'app.css'
},
js : {
name:'app.js'
},
data: {
name:'app.data.js'
},
layout: {
name:'app.layout.js'
}
},
module:{
css : {
name:'module.css'
},
html: {
name:'module.html'
},
js : {
name:'module.js'
},
example:{
name:'_example.html'
}
}
},
moduleData ={
name : false,
html : 'true',
css : 'true'
};
process.stdout.write('\n\n# Welcome to the RIA App tool.\nv2.0.0\nThis tool is backwards compatible with v1.*, howerver');
process.stdout.write('the framework itself is not. To upgarde to the version 2.0.0 app framework, simply run init in the v1.* root dir. You will not lose any data');
process.stdout.write('\n\n for v1.* Modules, you will need to rename the css, html, and js files in the module to module.css/js/html.\n\n');
process.stdout.write('v2.0.0 makes it easier to rename your modules if you need to by decoupling the module name from the files.');
process.stdout.write('To rename a module in v2.0.0, just change the module name in the modules module.js file and rename the module dir to match.');
process.stdin.setEncoding('utf8');
fs.exists(
path.resolve(
config.root,
'.config'
),
function(exists){
if(!exists)
return;
fs.readFile(
path.resolve(
config.root,
'.config'
),
{
encoding:'UTF-8'
},
function(err,data){
if(err){
return;
}
try{
config=JSON.parse(data);
}catch(err){
console.log('!!! app .config file error');
console.log(err);
return;
}
console.log('app config loaded\n>');
}
);
}
)
process.stdin.on(
'readable',
function() {
var chunk = process.stdin.read();
if (chunk !== null) {
chunk = chunk.replace( /\s\s+/g, ' ' ).trim();
var lowerChunk=chunk.toLowerCase().split(' ');
chunk=chunk.split(' ');
switch(lowerChunk[0]){
case 'create' :
switch(lowerChunk[1]){
case 'module' :
createModule(chunk,lowerChunk);
break;
}
break;
case 'init' :
init(chunk,lowerChunk);
break;
case 'set' :
setVar(chunk,lowerChunk);
break;
case 'help' :
help();
break;
}
process.stdout.write('>');
}
}
);
function setVar(chunk,lowerChunk){
if(lowerChunk[1]=='root'){
config.root=chunk[2];
console.log('## now refrencing '+chunk[2]+' as app root')
}
}
function init(chunk,lowerChunk){
for(var i=1; i<lowerChunk.length; i++){
if(lowerChunk[i].indexOf('root=')>-1){
config.root=chunk[i].split('=')[1];
continue;
}
if(lowerChunk[i].indexOf('modulepath=')>-1){
config.modulePath=chunk[i].split('=')[1];
continue;
}
}
createNewApp();
}
function createModule(chunk,lowerChunk){
if(
!fs.existsSync(
path.resolve(
config.root,
'app'
)
)
){
console.log(config.root+' is not an RIA App directory. If you are not in the root of the RIA App you wish to create the module in, set your app root with the set command. type help for more info on set.')
return;
}
if(!lowerChunk[2]){
guidedModuleCreation();
return;
}
moduleData.name=chunk[2];
for(var i=3; i<lowerChunk.length; i++){
if(lowerChunk[i].indexOf('no-css')){
moduleData.css='false';
continue;
}
if(lowerChunk[i].indexOf('no-html')){
moduleData.html='false';
continue;
}
}
createNewModule();
}
function guidedModuleCreation(){
process.stdout.write('Invalid syntax, missing module name. Type help for more information.');
}
function createNewModule(){
process.stdout.write('creating module for app in '+
path.resolve(
config.root,
config.modulePath
)
);
var modulePath=path.resolve(
config.root,
config.modulePath+'/'+
moduleData.name+'/'
);
if(
fs.existsSync(
modulePath
)
){
console.log('!!! module'+moduleData.name+' already exists, not creating!\n\nIf you want to create this module delete the current one first.');
return;
}else{
fs.mkdirSync(
modulePath
);
}
for(var i=0,
keys=Object.keys(files.module);
i<keys.length;
i++
){
var fileData={
path: path.resolve(
__dirname,
'template/app/module/template/'+
files.module[
keys[i]
].name
),
type: 'module',
key : keys[i]
}
fs.readFile(
fileData.path,
{
encoding:'UTF-8'
},
(
function(fileData){
return function(err,data){
if(err){
failedToReadFile.apply(
{},
arguments
);
}
gotFile.call(
{},
fileData.path,
fileData.type,
fileData.key,
data
);
}
}
)(fileData)
);
}
}
function createNewApp(){
process.stdout.write('initializing app in '+config.root);
var paths={
app : path.resolve(
config.root,
'app'
),
core : path.resolve(
config.root,
config.corePath
),
module : path.resolve(
config.root,
config.modulePath
)
}
if(fs.existsSync(paths.app)){
console.log('!!! app folder already exists');
}else{
fs.mkdirSync(
paths.app
);
}
if(fs.existsSync(paths.core)){
console.log('!!! core folder already exists');
}else{
fs.mkdirSync(
paths.core
);
}
if(fs.existsSync(paths.module)){
console.log('!!! module folder already exists');
}else{
fs.mkdirSync(
paths.module
);
}
fs.writeFile(
path.resolve(
config.root,
'.config'
),
JSON.stringify(config)
);
for(var i=0,
keys=Object.keys(files.core);
i<keys.length;
i++
){
var fileData={
path: path.resolve(
__dirname,
'template/app/core/'+
files.core[
keys[i]
].name
),
type: 'core',
key : keys[i]
}
fs.readFile(
fileData.path,
{
encoding:'UTF-8'
},
(
function(fileData){
return function(err,data){
if(err){
failedToReadFile.apply(
{},
arguments
);
}
gotFile.call(
{},
fileData.path,
fileData.type,
fileData.key,
data
);
}
}
)(fileData)
);
}
for(var i=0,
keys=Object.keys(files.app);
i<keys.length;
i++
){
var fileData={
path: path.resolve(
__dirname,
'template/'+
files.app[
keys[i]
].name
),
type: 'app',
key : keys[i]
}
fs.readFile(
fileData.path,
{
encoding:'UTF-8'
},
(
function(fileData){
return function(err,data){
if(err){
failedToReadFile.apply(
{},
arguments
);
}
gotFile.call(
{},
fileData.path,
fileData.type,
fileData.key,
data
);
}
}
)(fileData)
);
}
}
function failedToReadFile(err){
console.log(
'\n\n!!!! ERROR START !!!!\n\nUnable to load : '+
err.path+
'\nerrno : '+
err.errno+
'\ncode : '+
err.code+
'\n\n!!!! ERROR END !!!!\n\n'
);
}
function gotFile(filePath,type,key,data){
var toPath=false;
switch(type){
case 'core' :
var fileName=path.basename(filePath);
if(fileName=='app.js'){
data=data.replace(/\$\{modulePath\}/g,config.modulePath)
}
toPath=path.resolve(
config.root,
config.corePath+'/'+
fileName
);
break;
case 'module' :
var fileName='module'+path.extname(filePath);
if(path.basename(filePath)=='_example.html'){
fileName='_example.html';
}
data=data.replace(/\$\{moduleName\}/g,moduleData.name)
.replace(/\$\{css\}/g,moduleData.css)
.replace(/\$\{html\}/g,moduleData.html);
toPath=path.resolve(
config.root,
config.modulePath+'/'+
moduleData.name+'/'+
fileName
);
break;
case 'app' :
var fileName=path.basename(filePath);
toPath=path.resolve(
config.root,
fileName
);
break;
default :
console.log('\n\n!!! ERROR : file type of '+type+'not supported in gotFile function.\nFile write failed!\n\n');
return;
}
files[type][key].data=data;
//console.log(toPath,files[type][key])
fs.writeFile(
toPath,
data
);
console.log('### Writing '+toPath);
}
function help(){
process.stdout.write('\n\n#############################\n');
process.stdout.write('\n\nInitialize a new app : \n');
process.stdout.write('>init\n');
process.stdout.write('>init root=path/to/app/dir/here/\n');
process.stdout.write('>init modulePath=path/to/modules/here\n');
process.stdout.write('>init root=path/to/app/dir/here/ modulePath=path/to/modules/here/\n');
process.stdout.write('\n');
process.stdout.write('Create a new module : \n');
process.stdout.write('>create module moduleName\n');
process.stdout.write('>create module moduleName no-css no-html\n');
process.stdout.write('\n');
process.stdout.write('Set app options : \n');
process.stdout.write('>set root /path to my/app/root (this dir should contain the "app" folder)\n');
process.stdout.write('#############################\n\n');
}