refactor: Flecks.from

This commit is contained in:
cha0s 2024-01-13 02:17:18 -06:00
parent 51abba45c6
commit ccc147ce22
8 changed files with 38 additions and 34 deletions

View File

@ -9,6 +9,7 @@ import {
import get from 'lodash.get';
import set from 'lodash.set';
import compose from './compose';
import D from './debug';
import Digraph from './digraph';
import Middleware from './middleware';
@ -374,6 +375,20 @@ export default class Flecks {
return graph;
}
/**
* Create a mixed instance of flecks.
* @param {Object} config Configuration.
* @returns {Flecks} A flecks instance.
*/
static from(config) {
const {flecks} = config;
const mixins = Object.entries(flecks)
.map(([, M]) => M.hooks?.['@flecks/core.mixin'])
.filter((e) => e);
const Flecks = compose(...mixins)(this);
return new Flecks(config);
}
/**
* Gather and register class types.
*

View File

@ -19,7 +19,6 @@ import enhancedResolve from 'enhanced-resolve';
import {dump as dumpYml, load as loadYml} from 'js-yaml';
import {addHook} from 'pirates';
import compose from '../compose';
import D from '../debug';
import Flecks from '../flecks';
import R from '../require';
@ -124,14 +123,13 @@ export default class ServerFlecks extends Flecks {
// Load RCs.
const rcs = this.loadRcs(resolver);
this.installCompilers(rcs, resolver);
// Load the flecks.
const entries = Object.keys(resolver).map((path) => [path, R(this.resolve(resolver, path))]);
// Flecks mixins.
const mixins = entries.map(([, M]) => M.hooks?.['@flecks/core.mixin']).filter((e) => e);
const MixedServerFlecks = compose(...mixins)(ServerFlecks);
return new MixedServerFlecks({
// Instantiate with mixins.
return ServerFlecks.from({
config,
flecks: Object.fromEntries(entries),
flecks: Object.fromEntries(
Object.keys(resolver)
.map((path) => [path, R(this.resolve(resolver, path))]),
),
platforms,
rcs,
resolver,

View File

@ -6,7 +6,7 @@ const testOne = require('./one');
const testTwo = require('./two');
it('can gather', () => {
const flecks = new Flecks({
const flecks = Flecks.from({
flecks: {
'@flecks/core/one': testOne,
'@flecks/core/two': testTwo,

View File

@ -16,12 +16,12 @@ it('can create an empty instance', () => {
it('can gather config', () => {
let flecks;
flecks = new Flecks({
flecks = Flecks.from({
flecks: {'@flecks/core/one': testOne},
});
expect(flecks.get(['@flecks/core/one']))
.to.contain({foo: 'bar'});
flecks = new Flecks({
flecks = Flecks.from({
config: {'@flecks/core/one': {foo: 'baz'}},
flecks: {'@flecks/core/one': testOne},
});

View File

@ -13,7 +13,7 @@ const testTwo = require('./two');
let flecks;
beforeEach(() => {
flecks = new Flecks({
flecks = Flecks.from({
flecks: {
'@flecks/core/one': testOne,
'@flecks/core/two': testTwo,

View File

@ -7,7 +7,7 @@ const testTwo = require('./two');
const testThree = require('./three');
it('can make middleware', (done) => {
const flecks = new Flecks({
const flecks = Flecks.from({
config: {
'@flecks/core/test': {
middleware: [
@ -30,7 +30,7 @@ it('can make middleware', (done) => {
});
it('respects explicit middleware configuration', (done) => {
const flecks = new Flecks({
const flecks = Flecks.from({
config: {
'@flecks/core/test': {
middleware: [
@ -53,7 +53,7 @@ it('respects explicit middleware configuration', (done) => {
});
it('respects middleware elision', (done) => {
const flecks = new Flecks({
const flecks = Flecks.from({
config: {
'@flecks/core/test': {
middleware: [
@ -75,7 +75,7 @@ it('respects middleware elision', (done) => {
});
it('throws on elision graph cycle', () => {
const flecks = new Flecks({
const flecks = Flecks.from({
config: {
'@flecks/core/test': {
middleware: [

View File

@ -2,8 +2,8 @@ import {mkdir} from 'fs/promises';
import {tmpdir} from 'os';
import {join} from 'path';
import {compose, D} from '@flecks/core';
import {Flecks as BaseFlecks} from '@flecks/core/server';
import {D} from '@flecks/core';
import {Flecks} from '@flecks/core/server';
const {version} = require('../package.json');
@ -23,17 +23,12 @@ const {version} = require('../package.json');
debug('starting server...');
// Make resolver.
// Flecks mixins.
const resolver = BaseFlecks.makeResolver(config);
const rcs = BaseFlecks.loadRcs(resolver);
BaseFlecks.installCompilers(rcs, resolver);
const flecks = await loadFlecks();
const mixins = Object.entries(flecks)
.map(([, M]) => M.hooks?.['@flecks/core.mixin'])
.filter((e) => e);
const Flecks = compose(...mixins)(BaseFlecks);
global.flecks = new Flecks({
const resolver = Flecks.makeResolver(config);
const rcs = Flecks.loadRcs(resolver);
Flecks.installCompilers(rcs, resolver);
global.flecks = Flecks.from({
config,
flecks,
flecks: await loadFlecks(),
platforms,
resolver,
rcs,

View File

@ -1,4 +1,4 @@
import {compose, D, Flecks as BaseFlecks} from '@flecks/core';
import {D, Flecks} from '@flecks/core';
// eslint-disable-next-line import/no-extraneous-dependencies, import/no-unresolved
const {version} = require('@flecks/web/package.json');
@ -54,11 +54,7 @@ const {version} = require('@flecks/web/package.json');
progress.finish();
debug('starting client...');
// Flecks mixins.
const mixins = Object.entries(runtime.flecks)
.map(([, M]) => M.hooks?.['@flecks/core.mixin'])
.filter((e) => e);
const Flecks = compose(...mixins)(BaseFlecks);
const flecks = new Flecks(runtime);
const flecks = Flecks.from(runtime);
window.flecks = flecks;
try {
await Promise.all(flecks.invokeFlat('@flecks/core.starting'));