nt-arcane
Version:
App creator for arcane framework.
105 lines (93 loc) • 2.67 kB
JavaScript
var commandLineArgs = require('command-line-args');
var inquirer = require('inquirer');
var path = require('path');
var unzip = require('unzip2');
var fs = require('fs');
var npm = require('npm');
var cli = commandLineArgs([
{name: 'name', alias: 'n', type: String},
{name: 'table', alias: 't', type: String},
{name: 'location', alias: 'l', type: String},
{name: 'connection', alias: 'c', type: String}
]);
var options = cli.parse();
var directory_exists = function(path) {
try {
stats = fs.lstatSync(path);
if (stats.isDirectory()) {
return true;
}
} catch (e) {
return false;
}
};
var create_directory = function(path) {
if(!directory_exists(path)) {
fs.mkdirSync(path);
}
};
try {
var json = require(process.cwd() + '/arcane.json');
if(json.modules.indexOf('orm') === -1) {
console.log('\nORM module is not available for this project.\n');
} else {
if(options.name) {
select_location(options);
} else {
inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Model Name'
},
{
type: 'input',
name: 'table',
message: 'Table'
}
], select_location);
}
}
} catch (err) {
console.log('\nWorking directory is not a valid acane project.\n');
}
function select_location(answers) {
create_directory(process.cwd() + '/controller');
var controller_list = fs.readdirSync(process.cwd() + '/controller');
if(controller_list.length !== 0) {
controller_list.push(new inquirer.Separator());
}
controller_list.push('Global Model Directory');
inquirer.prompt([{
type: 'list',
name: 'location',
message: 'Select Directory',
choices: controller_list
}], function(answer2) {
if(answer2.location === 'Global Model Directory') {
create_model(answers, process.cwd() + '/models');
} else {
create_model(answers, process.cwd() + '/controller/' + answer2.location + '/models');
}
});
}
function create_model(info, directory) {
var data = fs.readFileSync(__dirname + '/../files/templates/model.tpl', 'utf-8');
create_directory(directory);
var filename = info.table.replace(/\_./g, function(match) {
return match[1].toUpperCase();
}).replace(/^./g, function(match) {
return match.toUpperCase();
});
var modelName = info.name.replace(/^./g, function(match) {
return match.toUpperCase();
});
var connection = '';
var mode_data = data.replace(/\{\{name\}\}/g, modelName).replace(/\{\{connection\}\}/g, connection);
var model_filename = directory + '/' + filename + '.ts';
if(fs.existsSync(model_filename)) {
console.log('\nModel ' + filename + ' is already exists.\n');
} else {
fs.writeFileSync(directory + '/' + filename + '.ts', mode_data);
}
}