Skip to content

MVC Modules #27

@jocelimjr2000

Description

@jocelimjr2000

Hy, tath´s ok?
Firstly, sorry my english, kkkk.

My name is Jocelim Junior, i’m brazilian PHP developer and starting NodeJS world.
Currently i’m working in a project that needs to use yours "Cosign" library.
My necessity is group modules into unique folder that contains others folders used MVC pattern.

Example:
Modules > User > controllers
Modules > User > models
Modules > Produts > controllers
Modules > Produts > models

The function include(), import all files from only folder.

include('controllers')
controllers > *

I edited code to import all files from multiple folders, inside modules.

include('controllers')
Modules > Users > controllers
Modules > Products > controllers

include('models')
Modules > Users > models
Modules > Products > models

Alterations lib/cosign.js:
this._options = {
...
modules: null,
modulesIgnored: [],
modulesRequired: []
};

Consign.prototype.include = function (entity) {
this._lastOperation = 'include';

if (
	(
	  this._options.modules &&
	  this._options.modulesIgnored.length == 0 &&
	  this._options.modulesRequired.length == 0
	) ||
	(
	  this._options.modules &&
	  this._options.modulesIgnored.length > 0 &&
	  this._options.modulesIgnored.includes(entity) == false
	) ||
	(
	  this._options.modules &&
	  this._options.modulesRequired.length > 0 &&
	  this._options.modulesRequired.includes(entity) == true
	)
  ) {

	var modulesPath = path.resolve(this._options.cwd, this._options.modules);
	var dir = fs.readdirSync(modulesPath);

	for (var e in dir) {

	  let f = path.resolve(modulesPath, dir[e]);

	  if (fs.statSync(f).isDirectory()) {
		this._setLocations(f, entity, true);
	  }

	}

	return this;

} else {
	return this._setLocations(this._options.cwd, entity, true);
}

}

Consign.prototype._createNamespace = function (parent, parts, mod) {
var part = parts.shift();

if(this._options.modules && this._getKeyName(part) == this._options.modules){
parts.shift();
part = parts.shift();
}

if (!parent[part]) {
parent[this._getKeyName(part)] = parts.length ? {} : mod;
}

if (parts.length) {
parent = this._createNamespace(parent[part], parts, mod);
}

return parent;
};

Is this a good alteration to project?
I whant help you.
Thank's

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions