Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions lib/api/folder/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,45 @@ const { lstat, readdir } = require('fs').promises;
const { Error } = require('@booljs/api');
const Reader = require('../../readers');

function camelCaseIt (name, type = 'class') {
const splittedName = name
.toLowerCase()
.split(/-|_/);

switch (type) {
case 'package': {
return splittedName
.map(name => name.charAt(0).toLowerCase() + name.slice(1))
.join('');
}
case 'class': {
return splittedName
.map(name => name.charAt(0).toUpperCase() + name.slice(1))
.join('');
}
}
}

exports.readFiles = async function (instance, component, route, files) {
const fileRegex = /([A-Za-z0-9-_ ]+)\.([A-Za-z0-9_]+)/;

for (const file of files) {
const filename = join(route, file);
const info = parse(filename);
const filenameInfo = parse(filename);

info.name = info.name
.toLowerCase()
.split(/-|_/)
.map(name => name.charAt(0).toUpperCase() + name.slice(1))
.join('');
filenameInfo.name = camelCaseIt(filenameInfo.name);

const fileStats = await lstat(filename);
if (fileStats.isDirectory()) {
info.name = info.name.charAt(0).toLowerCase() + info.name.slice(1);
instance.insertComponent(info.name, {}, component);
await module.exports(instance, component[info.name], filename);
filenameInfo.name = camelCaseIt(filenameInfo.name, 'package');
instance.insertComponent(filenameInfo.name, {}, component);
await module.exports(instance, component[filenameInfo.name], filename);
} else {
if (!fileRegex.test(file)) {
throw new Error(0, 'E_INVALIDFILENAME',
'The file name format is invalid');
throw new Error(0, 'E_INVALIDFILENAME', 'The file name format is invalid');
} else {
const reader = new Reader();

instance.insertComponent(
info.name,
await reader.readCode(info.ext, filename),
component
);
instance.insertComponent(filenameInfo.name,
await new Reader().readCode(filenameInfo.ext, filename), component);
}
}
}
Expand Down
108 changes: 53 additions & 55 deletions lib/api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict';

const _ = require('underscore');
const Readyness = require('readyness');
const { Error, App } = require('@booljs/api');

const Folder = require('./folder');
const loadConfigurations = require('./configurations');
const { loadComponents, loadDatabases, loadServer } = require('./loaders');
const deprecationNote = require('./utilities/deprecation-note');

/**
* @class BoolJSBootstrapper
Expand All @@ -11,29 +18,21 @@ const _ = require('underscore');
* @return {BoolJSBootstrapper} The instance of a bool.js loaded application
*/
module.exports = class BoolJSBootstrapper {
constructor (namespace, dependencies = []) {
const { Error, App } = require('@booljs/api');
Error = Error;
folders = new Folder.List();

this.Error = Error;
this.Folder = require('./folder');
this.ready = require('readyness');
this.Configuration = require('./configurations');
this.Loaders = require('./loaders');
databaseDrivers = [];
serverDrivers = [];
booted = false;
booting = false;
server = null;

constructor (namespace, dependencies = []) {
this.instance = App.getInstance(namespace, dependencies);

this.folders = new this.Folder.List();

this.databaseDrivers = [];
this.serverDrivers = [];
this.booted = false;
this.booting = false;
this.server = null;

if (!this.booted) {
this.instance.insertComponent(
'utilities', this.instance.getComponents().utilities.getStore()
);
this.instance.insertComponent('utilities',
this.instance.getComponents().utilities.getStore());
}
}

Expand Down Expand Up @@ -74,12 +73,7 @@ module.exports = class BoolJSBootstrapper {
* @return {BoolJSBootstrapper} The loaded application instance
*/
setDatabaseLoader (databaseLoader) {
console.log([
'\n\t\tDEPRECATION NOTE\n',
'This method is being deprecated since v0.9.0. Please stop using',
'it and instead use #setDatabaseDrivers'
].join(' '));
return this.setDatabaseDrivers(databaseLoader);
deprecationNote('0.9.0', 'setDatabaseDrivers', true);
}

/**
Expand All @@ -105,12 +99,7 @@ module.exports = class BoolJSBootstrapper {
* @return {BoolJSBootstrapper} The loaded application instance
*/
setServerLoader (serverLoader) {
console.log([
'\n\t\tDEPRECATION NOTE\n',
'This method is being deprecated since v0.9.0. Please stop using',
'it and instead use #setServerDrivers'
].join(' '));
return this.setServerDrivers(serverLoader);
deprecationNote('0.9.0', 'setServerDrivers', true);
}

/**
Expand Down Expand Up @@ -146,7 +135,7 @@ module.exports = class BoolJSBootstrapper {
* @return {Promise}
*/
readConfigurations (done, progress) {
return this.Configuration(this.instance, this.folders.configuration);
return loadConfigurations(this.instance, this.folders.configuration);
}

/**
Expand Down Expand Up @@ -179,7 +168,7 @@ module.exports = class BoolJSBootstrapper {
* @return {Promise}
*/
loadDatabase () {
return this.Loaders.database(this.instance, this.databaseDrivers);
return loadDatabases(this.instance, this.databaseDrivers);
}

/**
Expand All @@ -188,7 +177,7 @@ module.exports = class BoolJSBootstrapper {
* @return {Promise}
*/
loadComponents () {
return this.Loaders.components(this.folders, this.instance);
return loadComponents(this.folders, this.instance);
}

/**
Expand All @@ -197,39 +186,21 @@ module.exports = class BoolJSBootstrapper {
* @return {Promise}
*/
bootServer () {
return this.Loaders.server(this.instance, this.serverDrivers);
return loadServer(this.instance, this.serverDrivers);
}

/**
* @function module:booljs#run
* @description Boots up bool.js
* @description Bootstraps a new BoolJS instance
* @param {module:booljs-doneCallback} done - Executes when booting
* process is complete.
* @param {Callback} progress - Executes once a single steps have been
* executed
* @return {Promise}
*/
async run () {
if (this.booted) {
return {
app: this.instance.getComponents(),
server: this.server
};
} else if (this.booting) {
await new Promise((resolve, reject) => {
this.ready.doWhen(error => {
if (error) {
return reject(error);
}
return resolve();
});
});

return this.run();
}

async bootstrap () {
this.booting = true;
const booted = this.ready.waitFor('boot');
const booted = Readyness.waitFor('boot');

this.insertBoolError();

Expand All @@ -249,4 +220,31 @@ module.exports = class BoolJSBootstrapper {
server: this.server
};
}

/**
* @function module:booljs#run
* @description Boots BoolJS up
* @return {Promise}
*/
async run () {
if (this.booted) {
return {
app: this.instance.getComponents(),
server: this.server
};
} else if (this.booting) {
await new Promise((resolve, reject) => {
Readyness.doWhen(error => {
if (error) {
return reject(error);
}
return resolve();
});
});

return this.run();
}

this.bootstrap();
}
};
6 changes: 3 additions & 3 deletions lib/api/loaders/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @type {Object}
*/
module.exports = {
components: require('./components'),
database: require('./database'),
server: require('./server')
loadComponents: require('./components'),
loadDatabases: require('./database'),
loadServer: require('./server')
};
20 changes: 20 additions & 0 deletions lib/api/utilities/deprecation-note.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Marks a method as deprecated
* @param {String} version The version since the method is being deprecated
* @param {String} insteadUse A function to use instead this function
* @param {Boolean?} deprecated Whether this function is already deprecated
*/
module.exports = function (version, insteadUse, deprecated = false) {
const message = `
DEPRECATION NOTE
This method ${deprecated
? 'was deprecated'
: 'is being deprecated'} since v${version}.
STOP using it and instead use #${insteadUse}`;

if (deprecated) {
throw new Error(message);
} else {
console.log(message);
}
};
13 changes: 8 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@
'use strict';

const API = require('./api');
const instances = {};
const InstancesPool = Symbol('BoolJS##instancesPool');

/**
* @module booljs
* @param {String} namespace Application's namespace
* @return {Instance} A bool.js loader instance
*/
module.exports = class BoolJS {
class BoolJS {
static [InstancesPool] = {};
constructor (namespace, dependencies) {
if (!instances[namespace]) {
instances[namespace] = new API(namespace, dependencies);
if (!BoolJS[InstancesPool][namespace]) {
BoolJS[InstancesPool][namespace] = new API(namespace, dependencies);
}

return instances[namespace];
return BoolJS[InstancesPool][namespace];
}
};

module.exports = BoolJS;
32 changes: 17 additions & 15 deletions lib/readers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,39 @@
* @description Determines which readers are available to use in booljs
* bootstraping process
*/
module.exports = function () {
const staticReaders = {
module.exports = class Readers {
static staticReaders = {
'.json': require('./defaults/json'),
'.cson': require('./defaults/cson')
};

const codeReaders = {
static codeReaders = {
'.js': require('./defaults/javascript'),
'.coffee': require('./defaults/coffeescript')
};

static insertReader (field, name, reader) {
if (!this[field][name]) {
this[field][name] = reader;
}
}

/**
* Insert a static (non-code) files reader.
* @param {String} name - The extension of the file.
* @param {Function} reader - The function of the reader.
*/
this.insertStaticReader = function (name, reader) {
if (!staticReaders[name]) {
staticReaders[name] = reader;
}
static insertStaticReader (name, reader) {
this.insertReader('staticReaders', name, reader);
};

/**
* Insert a code files reader.
* @param {String} name - The extension of the code file.
* @param {Function} reader - The function of the reader.
*/
this.insertCodeReader = function (name, reader) {
if (!codeReaders[name]) {
codeReaders[name] = reader;
}
static insertCodeReader (name, reader) {
this.insertReader('codeReaders', name, reader);
};

/**
Expand All @@ -45,8 +47,8 @@ module.exports = function () {
* @param {String} route - The route of the file
* @param {Function} callback - A return function for retrieving the data.
*/
this.readStatic = function (name, route) {
return staticReaders[name](route);
static readStatic (name, route) {
return this.staticReaders[name](route);
};

/**
Expand All @@ -55,7 +57,7 @@ module.exports = function () {
* @param {String} route - The route of the file.
* @param {Function} callback - A return function for retrieving the code.
*/
this.readCode = function (name, route) {
return codeReaders[name](route);
static readCode (name, route) {
return this.codeReaders[name](route);
};
};
Loading