Skip to content

Commit a62fa0c

Browse files
authored
Merge pull request #55 from BoolJS/develop
Fix Several Dependencies and update to ES11
2 parents bfc7560 + fa56f42 commit a62fa0c

7 files changed

Lines changed: 365 additions & 203 deletions

File tree

lib/api/folder/read.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,45 @@ const { lstat, readdir } = require('fs').promises;
55
const { Error } = require('@booljs/api');
66
const Reader = require('../../readers');
77

8+
function camelCaseIt (name, type = 'class') {
9+
const splittedName = name
10+
.toLowerCase()
11+
.split(/-|_/);
12+
13+
switch (type) {
14+
case 'package': {
15+
return splittedName
16+
.map(name => name.charAt(0).toLowerCase() + name.slice(1))
17+
.join('');
18+
}
19+
case 'class': {
20+
return splittedName
21+
.map(name => name.charAt(0).toUpperCase() + name.slice(1))
22+
.join('');
23+
}
24+
}
25+
}
26+
827
exports.readFiles = async function (instance, component, route, files) {
928
const fileRegex = /([A-Za-z0-9-_ ]+)\.([A-Za-z0-9_]+)/;
1029

1130
for (const file of files) {
1231
const filename = join(route, file);
13-
const info = parse(filename);
32+
const filenameInfo = parse(filename);
1433

15-
info.name = info.name
16-
.toLowerCase()
17-
.split(/-|_/)
18-
.map(name => name.charAt(0).toUpperCase() + name.slice(1))
19-
.join('');
34+
filenameInfo.name = camelCaseIt(filenameInfo.name);
2035

2136
const fileStats = await lstat(filename);
2237
if (fileStats.isDirectory()) {
23-
info.name = info.name.charAt(0).toLowerCase() + info.name.slice(1);
24-
instance.insertComponent(info.name, {}, component);
25-
await module.exports(instance, component[info.name], filename);
38+
filenameInfo.name = camelCaseIt(filenameInfo.name, 'package');
39+
instance.insertComponent(filenameInfo.name, {}, component);
40+
await module.exports(instance, component[filenameInfo.name], filename);
2641
} else {
2742
if (!fileRegex.test(file)) {
28-
throw new Error(0, 'E_INVALIDFILENAME',
29-
'The file name format is invalid');
43+
throw new Error(0, 'E_INVALIDFILENAME', 'The file name format is invalid');
3044
} else {
31-
const reader = new Reader();
32-
33-
instance.insertComponent(
34-
info.name,
35-
await reader.readCode(info.ext, filename),
36-
component
37-
);
45+
instance.insertComponent(filenameInfo.name,
46+
await new Reader().readCode(filenameInfo.ext, filename), component);
3847
}
3948
}
4049
}

lib/api/index.js

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
'use strict';
22

33
const _ = require('underscore');
4+
const Readyness = require('readyness');
5+
const { Error, App } = require('@booljs/api');
6+
7+
const Folder = require('./folder');
8+
const loadConfigurations = require('./configurations');
9+
const { loadComponents, loadDatabases, loadServer } = require('./loaders');
10+
const deprecationNote = require('./utilities/deprecation-note');
411

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

17-
this.Error = Error;
18-
this.Folder = require('./folder');
19-
this.ready = require('readyness');
20-
this.Configuration = require('./configurations');
21-
this.Loaders = require('./loaders');
24+
databaseDrivers = [];
25+
serverDrivers = [];
26+
booted = false;
27+
booting = false;
28+
server = null;
2229

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

25-
this.folders = new this.Folder.List();
26-
27-
this.databaseDrivers = [];
28-
this.serverDrivers = [];
29-
this.booted = false;
30-
this.booting = false;
31-
this.server = null;
32-
3333
if (!this.booted) {
34-
this.instance.insertComponent(
35-
'utilities', this.instance.getComponents().utilities.getStore()
36-
);
34+
this.instance.insertComponent('utilities',
35+
this.instance.getComponents().utilities.getStore());
3736
}
3837
}
3938

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

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

116105
/**
@@ -146,7 +135,7 @@ module.exports = class BoolJSBootstrapper {
146135
* @return {Promise}
147136
*/
148137
readConfigurations (done, progress) {
149-
return this.Configuration(this.instance, this.folders.configuration);
138+
return loadConfigurations(this.instance, this.folders.configuration);
150139
}
151140

152141
/**
@@ -179,7 +168,7 @@ module.exports = class BoolJSBootstrapper {
179168
* @return {Promise}
180169
*/
181170
loadDatabase () {
182-
return this.Loaders.database(this.instance, this.databaseDrivers);
171+
return loadDatabases(this.instance, this.databaseDrivers);
183172
}
184173

185174
/**
@@ -188,7 +177,7 @@ module.exports = class BoolJSBootstrapper {
188177
* @return {Promise}
189178
*/
190179
loadComponents () {
191-
return this.Loaders.components(this.folders, this.instance);
180+
return loadComponents(this.folders, this.instance);
192181
}
193182

194183
/**
@@ -197,39 +186,21 @@ module.exports = class BoolJSBootstrapper {
197186
* @return {Promise}
198187
*/
199188
bootServer () {
200-
return this.Loaders.server(this.instance, this.serverDrivers);
189+
return loadServer(this.instance, this.serverDrivers);
201190
}
202191

203192
/**
204193
* @function module:booljs#run
205-
* @description Boots up bool.js
194+
* @description Bootstraps a new BoolJS instance
206195
* @param {module:booljs-doneCallback} done - Executes when booting
207196
* process is complete.
208197
* @param {Callback} progress - Executes once a single steps have been
209198
* executed
210199
* @return {Promise}
211200
*/
212-
async run () {
213-
if (this.booted) {
214-
return {
215-
app: this.instance.getComponents(),
216-
server: this.server
217-
};
218-
} else if (this.booting) {
219-
await new Promise((resolve, reject) => {
220-
this.ready.doWhen(error => {
221-
if (error) {
222-
return reject(error);
223-
}
224-
return resolve();
225-
});
226-
});
227-
228-
return this.run();
229-
}
230-
201+
async bootstrap () {
231202
this.booting = true;
232-
const booted = this.ready.waitFor('boot');
203+
const booted = Readyness.waitFor('boot');
233204

234205
this.insertBoolError();
235206

@@ -249,4 +220,31 @@ module.exports = class BoolJSBootstrapper {
249220
server: this.server
250221
};
251222
}
223+
224+
/**
225+
* @function module:booljs#run
226+
* @description Boots BoolJS up
227+
* @return {Promise}
228+
*/
229+
async run () {
230+
if (this.booted) {
231+
return {
232+
app: this.instance.getComponents(),
233+
server: this.server
234+
};
235+
} else if (this.booting) {
236+
await new Promise((resolve, reject) => {
237+
Readyness.doWhen(error => {
238+
if (error) {
239+
return reject(error);
240+
}
241+
return resolve();
242+
});
243+
});
244+
245+
return this.run();
246+
}
247+
248+
this.bootstrap();
249+
}
252250
};

lib/api/loaders/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @type {Object}
77
*/
88
module.exports = {
9-
components: require('./components'),
10-
database: require('./database'),
11-
server: require('./server')
9+
loadComponents: require('./components'),
10+
loadDatabases: require('./database'),
11+
loadServer: require('./server')
1212
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Marks a method as deprecated
3+
* @param {String} version The version since the method is being deprecated
4+
* @param {String} insteadUse A function to use instead this function
5+
* @param {Boolean?} deprecated Whether this function is already deprecated
6+
*/
7+
module.exports = function (version, insteadUse, deprecated = false) {
8+
const message = `
9+
DEPRECATION NOTE
10+
This method ${deprecated
11+
? 'was deprecated'
12+
: 'is being deprecated'} since v${version}.
13+
STOP using it and instead use #${insteadUse}`;
14+
15+
if (deprecated) {
16+
throw new Error(message);
17+
} else {
18+
console.log(message);
19+
}
20+
};

lib/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@
2121
'use strict';
2222

2323
const API = require('./api');
24-
const instances = {};
24+
const InstancesPool = Symbol('BoolJS##instancesPool');
2525

2626
/**
2727
* @module booljs
2828
* @param {String} namespace Application's namespace
2929
* @return {Instance} A bool.js loader instance
3030
*/
31-
module.exports = class BoolJS {
31+
class BoolJS {
32+
static [InstancesPool] = {};
3233
constructor (namespace, dependencies) {
33-
if (!instances[namespace]) {
34-
instances[namespace] = new API(namespace, dependencies);
34+
if (!BoolJS[InstancesPool][namespace]) {
35+
BoolJS[InstancesPool][namespace] = new API(namespace, dependencies);
3536
}
3637

37-
return instances[namespace];
38+
return BoolJS[InstancesPool][namespace];
3839
}
3940
};
41+
42+
module.exports = BoolJS;

lib/readers/index.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,39 @@
66
* @description Determines which readers are available to use in booljs
77
* bootstraping process
88
*/
9-
module.exports = function () {
10-
const staticReaders = {
9+
module.exports = class Readers {
10+
static staticReaders = {
1111
'.json': require('./defaults/json'),
1212
'.cson': require('./defaults/cson')
1313
};
1414

15-
const codeReaders = {
15+
static codeReaders = {
1616
'.js': require('./defaults/javascript'),
1717
'.coffee': require('./defaults/coffeescript')
1818
};
1919

20+
static insertReader (field, name, reader) {
21+
if (!this[field][name]) {
22+
this[field][name] = reader;
23+
}
24+
}
25+
2026
/**
2127
* Insert a static (non-code) files reader.
2228
* @param {String} name - The extension of the file.
2329
* @param {Function} reader - The function of the reader.
2430
*/
25-
this.insertStaticReader = function (name, reader) {
26-
if (!staticReaders[name]) {
27-
staticReaders[name] = reader;
28-
}
31+
static insertStaticReader (name, reader) {
32+
this.insertReader('staticReaders', name, reader);
2933
};
3034

3135
/**
3236
* Insert a code files reader.
3337
* @param {String} name - The extension of the code file.
3438
* @param {Function} reader - The function of the reader.
3539
*/
36-
this.insertCodeReader = function (name, reader) {
37-
if (!codeReaders[name]) {
38-
codeReaders[name] = reader;
39-
}
40+
static insertCodeReader (name, reader) {
41+
this.insertReader('codeReaders', name, reader);
4042
};
4143

4244
/**
@@ -45,8 +47,8 @@ module.exports = function () {
4547
* @param {String} route - The route of the file
4648
* @param {Function} callback - A return function for retrieving the data.
4749
*/
48-
this.readStatic = function (name, route) {
49-
return staticReaders[name](route);
50+
static readStatic (name, route) {
51+
return this.staticReaders[name](route);
5052
};
5153

5254
/**
@@ -55,7 +57,7 @@ module.exports = function () {
5557
* @param {String} route - The route of the file.
5658
* @param {Function} callback - A return function for retrieving the code.
5759
*/
58-
this.readCode = function (name, route) {
59-
return codeReaders[name](route);
60+
static readCode (name, route) {
61+
return this.codeReaders[name](route);
6062
};
6163
};

0 commit comments

Comments
 (0)