project.js
* @module greppy/helper/project
* @author Hermann Mayer <hermann.mayer92@gmail.com>
* Find a greppy project by path.
* @param {String} path - Path to start looking
Project.prototype.findAppPath = function(path, searched)
path = (require('path')).normalize(path + '/');
var greppyrc = (require('path')).normalize(path + '/.greppyrc');
if (!fs.existsSync(greppyrc)) {
* List all context of an application path.
* @param {String} path - Path to look for contexts
Project.prototype.listContexts = function(path)
var contextPath = (require('path')).normalize(path + '/app/context/');
if (!fs.existsSync(contextPath)) {
fs.readdirSync(contextPath).forEach(function(file) {
contexts.push((require('path')).basename(file, '.js'));
* List all modules of an application path.
* @param {String} path - Path to look for modules
Project.prototype.listModules = function(path)
var modulePath = (require('path')).normalize(path + '/modules/');
if (!fs.existsSync(modulePath) || !fs.statSync(modulePath).isDirectory()) {
fs.readdirSync(modulePath).forEach(function(file) {
if (!fs.statSync(modulePath + file).isDirectory()) {
* Load the contexts instances for a given context object
* which was build by ``listContexts``.
* @param {Object} contextObject - Contexts Object
Project.prototype.loadContexts = function(contextObject)
var greppy = require('../greppy.js');
contextObject.contexts.forEach(function(context) {
contextObject.instance[context] = new (require(contextObject.path + context))();
* Search the start script of a greppy application.
* @param {String} path - Path to search in
Project.prototype.findStartScript = function(path)
var appPath = (require('path')).normalize(path + '/app/');
var startScript = 'worker.js';
if (!fs.existsSync(appPath) || !fs.statSync(appPath).isDirectory()) {
fs.readdirSync(appPath).some(function(file) {
if (file.match(/master/gi) && file.match(/\.js$/gi)) {
if (fs.existsSync(appPath + startScript)) {
* List all configurations of an application path.
* @param {String} path - Path to look for configurations
Project.prototype.listConfigs = function(path)
var configPath = (require('path')).normalize(path + '/app/config/');
if (!fs.existsSync(configPath) || !fs.statSync(configPath).isDirectory()) {
fs.readdirSync(configPath).forEach(function(file) {
if (!file.match(/\.js$|\.json$/gi)) {
configs.push((require('path')).basename(file, '.js'));
* Load the configuration instances for a given config object
* which was build by ``listConfigs``.
* @param {Object} configObject - Configuration Object
Project.prototype.loadConfigs = function(configObject)
configObject.configs.forEach(function(config) {
configObject.instance[config] = require(configObject.path + config);
* List all models of a given module path.
* @param {String} path - Path to look for models
* @param {Boolean} [listAssociations] - List associations - Default: false
Project.prototype.listModels = function(path, listAssociations)
if ('undefined' === typeof listAssociations) {
var modelPath = (require('path')).normalize(
if (!fs.existsSync(modelPath) || !fs.statSync(modelPath).isDirectory()) {
fs.readdirSync(modelPath).forEach(function(namespace) {
if (!fs.statSync(modelPath + namespace).isDirectory()) {
fs.readdirSync(modelPath + namespace).forEach(function(file) {
(false === listAssociations && null !== file.match(/^Associations\.js$/))) {
models[namespace].push((require('path')).basename(file, '.js'));
* List all models of all modules of an application path.
* @param {String} path - Path to look for configurations
* @param {Boolean} [listAssociations] - List associations - Default: false
Project.prototype.listModelsForAllModules = function(path, listAssociations)
var modules = this.listModules(path);
modules.modules.forEach(function(module) {
var modelPath = (require('path')).normalize(
var result = self.listModels(modelPath, listAssociations);
* List all fixtures for a connection.
* @param {String} path - Path to look for fixtures
* @param {String} connection - Name of the connection
Project.prototype.listFixturesForConnection = function(path, connection)
var fixturesPath = (require('path')).normalize(
path + '/database/fixtures/' + connection + '/'
var basename = require('path').basename;
if (!fs.existsSync(fixturesPath) || !fs.statSync(fixturesPath).isDirectory()) {
fs.readdirSync(fixturesPath).forEach(function(file) {
fixtures.push(basename(file, '.js'));
* List all fixtures for a connection.
* @param {String} path - Path to look for fixtures
* @param {String} connection - Name of the connection
Project.prototype.listMigrationsForConnection = function(path, connection)
var migrationsPath = (require('path')).normalize(
path + '/database/migrations/' + connection + '/'
var basename = require('path').basename;
if (!fs.existsSync(migrationsPath) || !fs.statSync(migrationsPath).isDirectory()) {
fs.readdirSync(migrationsPath).forEach(function(file) {
migrations.push(basename(file, '.js'));
* Format all routes of an given context.
* @param {Object} context - Context were we format routes
Project.prototype.formatContextRoutes = function(context)
context.routes.forEach(function(route) {
maxPathLength = (route.path.length > maxPathLength)
maxMethodLength = (route.method.length > maxMethodLength)
context.routes.forEach(function(route) {
while (maxMethodLength > route.method.length) {
route.method = ' ' + route.method;
while (maxPathLength > route.path.length) {
'[' + route.method.green.bold + '] ' +
'[' + (null === route.auth ? '+'.green.bold : '-'.red.bold) + '] ' +