Skip to content

Commit

Permalink
chore: adding debug and hoisting mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
reel committed Apr 27, 2018
1 parent 08760fc commit 204072d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"cookie-parser": "^1.4.3",
"cors": "^2.8.4",
"cross-spawn": "^6.0.4",
"debug": "^3.1.0",
"detect-port": "^1.2.2",
"express": "^4.16.2",
"express-session": "^1.15.6",
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/2.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ async function watch() {
'./**.json',
'./**.lock',
];
const { pen, utils: { clearConsole } } = henri;
const {
pen,
utils: { clearConsole },
} = henri;

henri.status.set('locked', true);
chokidar.watch(watching).on('all', async (event, path) => {
Expand All @@ -48,7 +51,7 @@ async function watch() {
pen.line();
pen.warn('server', 'changes detected in', path);
pen.line(2);
await henri.utils.checkSyntax(path);
await henri.utils.syntax(path);
setTimeout(() => henri.status.set('locked', false), 3000);
!henri.status.get('locked') && henri.reload();
});
Expand All @@ -75,7 +78,10 @@ async function watch() {
* @todo Move this to its own module with a menu and dynamic shortcuts
*/
function keyboardShortcuts() {
const { pen, utils: { clearConsole } } = henri;
const {
pen,
utils: { clearConsole },
} = henri;

process.stdin.resume();
process.stdin.on('data', async data => {
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/3.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const fs = require('fs');
const includeAll = require('include-all');
const bounce = require('bounce');
const debug = require('debug')('henri:model');

/**
* Model module
Expand Down Expand Up @@ -36,6 +37,7 @@ class Model extends BaseModule {
this.reload = this.reload.bind(this);
this.addToEslintRc = this.addToEslintRc.bind(this);
this.checkStoreOrDie = this.checkStoreOrDie.bind(this);
debug('constructor initialized');
}

/**
Expand All @@ -60,8 +62,11 @@ class Model extends BaseModule {
},
(err, modules) => {
if (err) {
debug('load rejected');

return reject(err);
}
debug('loaded from fs');

return resolve(modules);
}
Expand Down Expand Up @@ -134,6 +139,7 @@ class Model extends BaseModule {
this.stores = {};
this.ids = [];
this.models = [];
debug('done resetting');

return true;
}
Expand All @@ -157,6 +163,8 @@ class Model extends BaseModule {
`@usehenri/${conn}`
));

debug('loaded adapter %s (%s)', store.adapter, conn);

return Pkg;
} catch (error) {
bounce.rethrow(error, 'system');
Expand All @@ -181,6 +189,8 @@ class Model extends BaseModule {
const { config, pen } = this.henri;

if (this.stores[name]) {
debug('store %s is already loaded, returning from cache', name);

return this.stores[name];
}
const store = config.get(`stores.${name}`);
Expand Down Expand Up @@ -229,6 +239,7 @@ class Model extends BaseModule {
await this.start(
await this.configure(await Model.load('./app/models'))
);
debug('init done');
} catch (error) {
bounce.rethrow(error, 'system');
}
Expand All @@ -247,6 +258,7 @@ class Model extends BaseModule {
return new Promise(async resolve => {
try {
for (const store of Object.keys(this.stores)) {
debug('starting store %s', store);
await this.stores[store].start();
}
} catch (error) {
Expand All @@ -255,6 +267,7 @@ class Model extends BaseModule {
if (this.ids.length > 0) {
this.addToEslintRc();
}
debug('start done');

return resolve();
});
Expand All @@ -279,12 +292,17 @@ class Model extends BaseModule {
try {
for (const store of Object.keys(this.stores)) {
await this.stores[store].stop();
debug('stopped %s', store);
}
} catch (error) {
bounce.rethrow(error, 'system');
}
this.ids.forEach(name => delete global[name]);
delete this.stores;

this.ids = [];
this.stores = [];
debug('stop done');

return resolve(true);
});
Expand All @@ -299,8 +317,10 @@ class Model extends BaseModule {
*/
async reload() {
try {
debug('reloading began');
await this.stop();
await this.init();
debug('reloading done!');
} catch (error) {
bounce.rethrow(error, 'system');
henri.pen.error('model', error);
Expand Down
1 change: 1 addition & 0 deletions packages/mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"license": "MIT",
"dependencies": {
"connect-mongo": "^2.0.1",
"debug": "^3.1.0",
"mongoose": "^5.0.16"
}
}

0 comments on commit 204072d

Please sign in to comment.