Skip to content

Commit

Permalink
feat(core): consolidating core
Browse files Browse the repository at this point in the history
  • Loading branch information
reel committed Apr 4, 2018
1 parent 7651342 commit c7cbb1a
Show file tree
Hide file tree
Showing 32 changed files with 1,612 additions and 1,160 deletions.
11 changes: 2 additions & 9 deletions packages/cli/scripts/server.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
const main = ({ skipView }, cb) => {
global['_initialDelay'] = process.hrtime();
process.env.SUPPRESS_NO_CONFIG_WARNING = 'true';

async function init() {
try {
await require('@usehenri/core')();
require('@usehenri/user');
!skipView && (await require('@usehenri/view'));
require('@usehenri/router');
if (typeof cb === 'function') {
cb();
}
await require('@usehenri/core');
} catch (error) {
// eslint-disable-next-line no-console
console.dir(error, { colors: true });
process.exit(-1);
process.exit(1);
}
}

Expand Down
16 changes: 4 additions & 12 deletions packages/cli/scripts/start-henri.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
const main = ({ skipView }, cb) => {
global['_initialDelay'] = process.hrtime();
process.env.SUPPRESS_NO_CONFIG_WARNING = 'true';

async function init() {
try {
require('@usehenri/config');
require('@usehenri/server');
require('@usehenri/user');
await require('@usehenri/model');
await require('@usehenri/controller');
!skipView && (await require('@usehenri/view'));
require('@usehenri/router');
if (typeof cb === 'function') {
cb();
}
await require('@usehenri/core');
} catch (error) {
// eslint-disable-next-line no-console
console.dir(error, { colors: true });
process.exit(-1);
process.exit(1);
}
}

Expand Down
15 changes: 10 additions & 5 deletions packages/core/src/config.js → packages/core/src/0.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ const { importFresh } = require('./utils');
const BaseModule = require('./base/module');

class Config extends BaseModule {
constructor(henri) {
constructor() {
super();
this.reloadable = true;
this.runlevel = 1;
this.runlevel = 0;
this.name = 'config';
this.config = config;
this.reloadable = true;
this.henri = henri;
this.henri = null;

this.get = this.get.bind(this);
this.has = this.has.bind(this);
this.reload = this.reload.bind(this);
this.init = this.init.bind(this);
return this;
}

init() {
return this;
return this.name;
}

get(key, safe = false) {
Expand All @@ -36,7 +37,11 @@ class Config extends BaseModule {
reload() {
delete this.config;
this.config = importFresh('config');
return true;
return this.name;
}

stop() {
return false;
}
}

Expand Down
132 changes: 67 additions & 65 deletions packages/core/src/modules.js → packages/core/src/0.modules.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
const { stack } = require('./utils');
const BaseModule = require('./base/module');
const path = require('path');

class Modules {
constructor(henri) {
this.henri = henri;
this.modules = new Map();
this.store = [[], [], [], [], [], [], []];
this.order = [];

// legacy stores
this._loaders = [];
this._unloaders = [];
this.order = [[], [], [], [], [], [], []];
this.reloadable = [[], [], [], [], [], [], []];
this.stopOrder = [];
this.initialized = false;

this.init = this.init.bind(this);
this.reload = this.reload.bind(this);
this.add = this.add.bind(this);
this.loader = this.loader.bind(this);
this.unloader = this.unloader.bind(this);
}

add(func) {
Expand All @@ -31,6 +27,8 @@ class Modules {
return false;
}

obj.henri = this.henri;

const existing =
this.modules.get(obj.name) || typeof this.henri[obj.name] !== 'undefined';

Expand All @@ -50,74 +48,86 @@ class Modules {
return true;
}

async init(prefix = '.', level = 6) {
async init() {
const { pen } = this.henri;

if (prefix !== this.henri.prefix) {
changeCurrentDirectory(prefix, pen);
pen.warn('modules', 'cwd change', this.henri.cwd, process.cwd());
this.henri.cwd = process.cwd();
this.store.splice(parseInt(this.henri.runlevel) + 1);

// this.order = this.store.reduce((a, b) => a.concat(b));
this.stopOrder = this.store.reduceRight((a, b) => a.concat(b));

if (this.stopOrder.length < 1) {
pen.fatal('modules', 'init', 'no modules loaded before init');
}

this.henri.prefix = prefix;
this.henri.runlevel = level;
this.store.splice(parseInt(level) + 1);
let count = 0;
let size = this.stopOrder.length;
for (let level of this.store) {
if (level.length > 0) {
let runlevel = 0;

for (let obj of level) {
runlevel = obj.runlevel;
this.order[obj.runlevel].push(obj.init);
this.henri[obj.name] = obj;
if (obj.reloadable && typeof obj.reload === 'function') {
this.reloadable[obj.runlevel].push(obj.reload);
}
}

return new Promise(async resolve => {
if (this.henri.runlevel < 6) {
pen.warn('modules', 'running at limited level', this.henri.runlevel);
let result = await Promise.all(this.order[runlevel].map(f => f()));
for (let name of result) {
count++;
pen.info(`modules`, name, `loaded`, `${count}/${size}`);
}
}
}

this.order = this.store.reduce((a, b) => a.concat(b));
this.stopOrder = this.store.reduceRight((a, b) => a.concat(b));
pen.info('modules', 'loading', '...done!');

if (this.order.length < 1) {
throw new Error('available to load. why should I run?');
}
this.initialized = true;

let count = 0;
let size = this.order.length;
for (let mod of this.order) {
count++;
this.henri[mod.name] = mod;
if (typeof mod.init === 'function') {
await mod.init();
}
pen.info(mod.name, `module`, `loaded`, `${count}/${size}`);
}
pen.info('modules', 'loading', '...done!');
resolve();
});
return true;
}

reload() {
async reload() {
const { pen } = this.henri;

this.order.forEach(async (V, i, t) => {
if (V.reloadable && typeof V.reload === 'function') {
await V.reload();
}
pen.info(V.name, `module is reloaded`, `${i + 1}/${t.length}`);
});
if (!this.initialized) {
pen.warn('modules', 'cannot reload when not initialized');
return false;
}

return true;
}
for (let id of Object.keys(require.cache)) {
// istanbul ignore next
delete require.cache[id];
}

loader(func) {
if (!this.isProduction && typeof func === 'function') {
this._loaders.push(func);
let count = 0;
const max = this.reloadable.reduce((a, b) => a.concat(b));
for (let level of this.reloadable) {
if (level.length > 0) {
const result = await Promise.all(level.map(f => f()));
for (let name of result) {
count++;
pen.info(`modules`, name, `reloaded`, `${count}/${max.length}`);
}
}
}

return true;
}

unloader(func) {
async stop() {
const { pen } = this.henri;
if (typeof func === 'function') {
this._unloaders.unshift(func);
} else {
pen.error(
'modules',
'you tried to register an unloader which is not a function'
);

for (let mod of this.stopOrder) {
this.henri[mod.name] = mod;
if (typeof mod.stop === 'function') {
if (await mod.stop()) {
pen.info(`modules`, mod.name, `stopped`);
}
}
}
}
}
Expand Down Expand Up @@ -188,12 +198,4 @@ function crashOnDuplicateModule(existing, func, info, pen) {
);
}

function changeCurrentDirectory(prefix, pen) {
try {
process.chdir(path.resolve(process.cwd(), prefix));
} catch (e) {
pen.fatal('modules', `unable to change current directory to ${prefix}`);
}
}

module.exports = Modules;
39 changes: 10 additions & 29 deletions packages/core/src/pen.js → packages/core/src/0.pen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,11 @@ class Pen extends BaseModule {
this.notTest = process.env.NODE_ENV !== 'test';
this.longest = 12;
this.buffer = [];
this._time = process.uptime();
this._time =
process.env.NODE_ENV === 'test' ? '42424242' : process.uptime();
this._timeSkipped = 0;
this.initialized = false;
this.inTesting = inTesting;

this.time = this.time.bind(this);
this.error = this.error.bind(this);
this.warn = this.warn.bind(this);
this.info = this.info.bind(this);
this.verbose = this.verbose.bind(this);
this.debug = this.debug.bind(this);
this.silly = this.silly.bind(this);
this.output = this.output.bind(this);
this.dequeue = this.dequeue.bind(this);
}

error(name, ...args) {
Expand Down Expand Up @@ -149,11 +140,13 @@ class Pen extends BaseModule {
if (!this.initialized) {
this.initialized = true;
this.line(1);
this.info(
'henri',
henri.release,
henri.isProduction ? chalk.green('production') : chalk.red('dev')
);
if (typeof global['henri'] !== 'undefined') {
this.info(
'henri',
henri.release,
henri.isProduction ? chalk.green('production') : chalk.red('dev')
);
}
this.line(1);
}
if (this.longest < name.length) {
Expand All @@ -179,23 +172,11 @@ class Pen extends BaseModule {
return { data, space };
}

queue(data) {
this.buffer.push(data);
}

dequeue() {
const data = this.buffer.splice(0);
if (data.length > 0) {
// eslint-disable-next-line no-console
data.map(v => console.log(v));
}
}

notify(title = null, message = null) {
if (!title && !message) {
return false;
}
if (henri.isDev) {
if (henri && henri.isDev) {
notifier.notify({
title,
message,
Expand Down

0 comments on commit c7cbb1a

Please sign in to comment.