chore: logging++

This commit is contained in:
cha0s 2022-04-04 04:42:01 -05:00
parent a6dff85d99
commit 3e889db9a1
15 changed files with 68 additions and 47 deletions

View File

@ -12,6 +12,7 @@ const {
} = process.env;
const debug = D('@flecks/core/cli');
const debugSilly = debug.extend('silly');
// Guarantee local node_modules path.
const defaultNodeModules = resolve(join(FLECKS_CORE_ROOT, 'node_modules'));
@ -40,7 +41,7 @@ const environmentUpdates = (
}
: undefined;
if (environmentUpdates) {
debug('updating environment, forking with %O...', environmentUpdates);
debugSilly('updating environment, forking with %O...', environmentUpdates);
const forkOptions = {
env: {
...process.env,
@ -61,13 +62,13 @@ else {
const child = await fn(...args);
if ('object' !== typeof child) {
const code = 'undefined' !== typeof child ? child : 0;
debug('action returned code %d', code);
debugSilly('action returned code %d', code);
process.exitCode = code;
return;
}
try {
const code = await processCode(child);
debug('action exited with code %d', code);
debugSilly('action exited with code %d', code);
process.exitCode = code;
}
catch (error) {
@ -84,9 +85,9 @@ else {
.usage('[command] [...]');
// Bootstrap.
(async () => {
debug('bootstrapping flecks...');
debugSilly('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
debugSilly('bootstrapped');
// Register commands.
const commands = flecks.invokeMerge('@flecks/core.commands', program);
const keys = Object.keys(commands).sort();
@ -98,7 +99,7 @@ else {
name = keys[i],
options = [],
} = commands[keys[i]];
debug('adding command %s...', name);
debugSilly('adding command %s...', name);
const cmd = program.command(name);
cmd.description(description);
for (let i = 0; i < args.length; ++i) {

View File

@ -21,8 +21,9 @@ module.exports = (name) => {
D.formatters.o = undefined;
D.formatters.O = undefined;
}
const type = 'server' === process.env.FLECKS_CORE_BUILD_TARGET ? 'error' : 'debug';
// eslint-disable-next-line no-console
D.log = console.debug.bind(console);
D.log = console[type].bind(console);
}
return D(name);
};

View File

@ -14,6 +14,7 @@ import D from './debug';
import Middleware from './middleware';
const debug = D('@flecks/core/flecks');
const debugSilly = debug.extend('silly');
export const ById = Symbol.for('@flecks/core.byId');
export const ByType = Symbol.for('@flecks/core.byType');
@ -55,13 +56,13 @@ export default class Flecks {
this.flecks = {};
this.platforms = platforms;
const entries = Object.entries(flecks);
debug('paths: %O', entries.map(([fleck]) => fleck));
debugSilly('paths: %O', entries.map(([fleck]) => fleck));
for (let i = 0; i < entries.length; i++) {
const [fleck, M] = entries[i];
this.registerFleck(fleck, M);
}
this.configureFlecks();
debug('config: %O', this.config);
debugSilly('config: %O', this.config);
}
configureFleck(fleck) {
@ -204,7 +205,7 @@ export default class Flecks {
[ByType]: types,
};
hotGathered.set(hook, {idAttribute, gathered, typeAttribute});
debug("gathered '%s': %O", hook, gathered);
debug("gathered '%s': %O", hook, Object.keys(gathered[ByType]));
return gathered;
}
@ -257,7 +258,7 @@ export default class Flecks {
}
invokeFleck(hook, fleck, ...args) {
debug('invokeFleck(%s, %s, ...)', hook, fleck);
debugSilly('invokeFleck(%s, %s, ...)', hook, fleck);
if (!this.hooks[hook]) {
return undefined;
}
@ -346,7 +347,7 @@ export default class Flecks {
}
makeMiddleware(hook) {
debug('makeMiddleware(...): %s', hook);
debugSilly('makeMiddleware(...): %s', hook);
if (!this.hooks[hook]) {
return Promise.resolve();
}
@ -356,7 +357,7 @@ export default class Flecks {
}
const middleware = flecks
.filter((fleck) => this.fleckImplements(fleck, hook));
debug('middleware: %O', middleware);
debugSilly('middleware: %O', middleware);
const instance = new Middleware(middleware.map((fleck) => this.invokeFleck(hook, fleck)));
return async (...args) => {
const next = args.pop();
@ -422,13 +423,13 @@ export default class Flecks {
}
registerFleck(fleck, M) {
debug('registering %s...', fleck);
debugSilly('registering %s...', fleck);
this.flecks[fleck] = M;
if (M.default) {
const {default: {[Hooks]: hooks}} = M;
if (hooks) {
const keys = Object.keys(hooks);
debug("hooks for '%s': %O", fleck, keys);
debugSilly("hooks for '%s': %O", fleck, keys);
for (let j = 0; j < keys.length; j++) {
const key = keys[j];
if (!this.hooks[key]) {
@ -439,7 +440,7 @@ export default class Flecks {
}
}
else {
debug("'%s' has no default export: %O", fleck, M);
debugSilly("'%s' has no default export", fleck);
}
}

View File

@ -19,6 +19,7 @@ import {targetNeutrino} from '../commands';
import Flecks from '../flecks';
const debug = D('@flecks/core/build/webpack.config.js');
const debugSilly = debug.extend('silly');
const {
FLECKS_CORE_BUILD_LIST = '',
@ -78,6 +79,6 @@ export default (async () => {
debug('no webpack configuration found! aborting...');
await new Promise(() => {});
}
debug('webpack configurations %O', webpackConfigs);
debugSilly('webpack configurations %O', webpackConfigs);
return webpackConfigs;
})();

View File

@ -12,6 +12,7 @@ const {
} = process.env;
const debug = D('@flecks/core/commands');
const debugSilly = debug.extend('silly');
const flecksRoot = normalize(FLECKS_CORE_ROOT);
export const processCode = (child) => new Promise((resolve, reject) => {
@ -23,7 +24,8 @@ export const processCode = (child) => new Promise((resolve, reject) => {
});
export const spawnWith = (cmd, opts = {}) => {
debug("spawning: '%s' with options: %O", cmd.join(' '), opts);
debug("spawning: '%s'", cmd.join(' '));
debugSilly('with options: %O', opts);
const child = spawn(cmd[0], cmd.slice(1), {
...opts,
env: {

View File

@ -28,6 +28,7 @@ const {
} = process.env;
const debug = D('@flecks/core/flecks/server');
const debugSilly = debug.extend('silly');
export default class ServerFlecks extends Flecks {
@ -83,7 +84,8 @@ export default class ServerFlecks extends Flecks {
else {
configType = 'parameter';
}
debug('bootstrap configuration (%s): %O', configType, config);
debug('bootstrap configuration (%s)', configType);
debugSilly(config);
// Make resolver.
const resolver = this.makeResolver(config, platforms, root);
// Rewrite aliased config keys.
@ -183,7 +185,7 @@ export default class ServerFlecks extends Flecks {
static installCompilers(rcs, resolver) {
const paths = Object.keys(resolver);
debug('rcs: %O', rcs);
debugSilly('rcs: %O', rcs);
// Merge aliases;
const aliases = Object.fromEntries(
Object.entries({
@ -202,7 +204,7 @@ export default class ServerFlecks extends Flecks {
.map(([from, to]) => [from, to.endsWith('/index') ? to.slice(0, -6) : to]),
);
if (Object.keys(aliases).length > 0) {
debug('aliases: %O', aliases);
debugSilly('aliases: %O', aliases);
}
const exts = this.exts(rcs);
const enhancedResolver = enhancedResolve.create.sync({
@ -212,7 +214,7 @@ export default class ServerFlecks extends Flecks {
// Stub server-unfriendly modules.
const stubs = this.stubs(['server'], rcs);
if (stubs.length > 0) {
debug('stubbing: %O', stubs);
debugSilly('stubbing: %O', stubs);
}
// Do we need to get up in `require()`'s guts?
if (
@ -248,7 +250,7 @@ export default class ServerFlecks extends Flecks {
if (needCompilation.length > 0) {
// Augment the compilations with babel config from flecksrc.
const rcBabelConfig = babelmerge.all(this.babel(rcs).map(([, babel]) => babel));
debug('.flecksrc: babel: %O', rcBabelConfig);
debugSilly('.flecksrc: babel: %O', rcBabelConfig);
// Key flecks needing compilation by their roots, so we can compile all common roots with a
// single invocation of `@babel/register`.
const compilationRootMap = {};
@ -296,7 +298,7 @@ export default class ServerFlecks extends Flecks {
ignore: [ignore],
only: [only],
};
debug('compiling %O with %j', compiling, config);
debugSilly('compiling %O with %j', compiling, config);
compilations.push({
ignore,
only,
@ -313,7 +315,7 @@ export default class ServerFlecks extends Flecks {
}
return undefined;
};
debug('pirating exts: %O', exts);
debugSilly('pirating exts: %O', exts);
addHook(
(code, request) => {
const compilation = findCompiler(request).compile(code, request);
@ -344,7 +346,7 @@ export default class ServerFlecks extends Flecks {
const {load} = R('js-yaml');
const filename = join(resolvedRoot, 'build', FLECKS_YML);
const buffer = readFileSync(filename, 'utf8');
debug('parsing configuration from YML...');
debugSilly('parsing configuration from YML...');
return ['YML', load(buffer, {filename}) || {}];
}
catch (error) {
@ -553,7 +555,7 @@ export default class ServerFlecks extends Flecks {
.filter(([fleck]) => this.constructor.fleckIsCompiled(resolver, fleck));
if (needCompilation.length > 0) {
const rcBabel = this.babel();
debug('.flecksrc: babel: %O', rcBabel);
debugSilly('.flecksrc: babel: %O', rcBabel);
// Alias and de-externalize.
needCompilation
.sort(([l], [r]) => (l < r ? 1 : -1))
@ -565,7 +567,7 @@ export default class ServerFlecks extends Flecks {
allowlist.push(fleck);
config.resolve.alias
.set(fleck, alias);
debug('%s runtime de-externalized %s, alias: %s', runtime, fleck, alias);
debugSilly('%s runtime de-externalized %s, alias: %s', runtime, fleck, alias);
});
// Set up compilation at each root.
Array.from(new Set(
@ -579,7 +581,7 @@ export default class ServerFlecks extends Flecks {
const sourceroot = join(sourcepath, '..');
additionalModuleDirs.push(join(sourceroot, 'node_modules'));
const configFile = this.buildConfig('babel.config.js');
debug('compiling: %s with %s', root, configFile);
debugSilly('compiling: %s with %s', root, configFile);
const babel = {
configFile,
// Augment the compiler with babel config from flecksrc.

View File

@ -1,4 +1,5 @@
import {join} from 'path';
import {inspect} from 'util';
import airbnb from '@neutrinojs/airbnb';
import neutrino from 'neutrino';
@ -11,6 +12,11 @@ const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
const {defaultOptions} = inspect;
defaultOptions.breakLength = 160;
defaultOptions.compact = 6;
defaultOptions.sorted = true;
export {dump as dumpYml, load as loadYml} from 'js-yaml';
export {

View File

@ -7,10 +7,11 @@ import FlecksContext from '@flecks/react/context';
import gatherComponents from './gather-components';
const debug = D('@flecks/react/root');
const debugSilly = debug.extend('silly');
export default async (flecks, req) => {
const Roots = flecks.invoke('@flecks/react.roots', req);
debug('roots: %O', Roots);
debugSilly('roots: %O', Roots);
const Providers = await flecks.invokeSequentialAsync('@flecks/react.providers', req);
const FlattenedProviders = [];
for (let i = 0; i < Providers.length; i++) {
@ -19,7 +20,7 @@ export default async (flecks, req) => {
FlattenedProviders.push(...(Array.isArray(Provider[0]) ? Provider : [Provider]));
}
}
debug('providers: %O', FlattenedProviders);
debugSilly('providers: %O', FlattenedProviders);
return () => {
const RootElements = [[FlecksContext.Provider, {value: flecks}]]
.concat(FlattenedProviders)

View File

@ -6,6 +6,7 @@ import session from 'express-session';
import createClient from '../create-client';
const debug = D('@flecks/redis/session');
const debugSilly = debug.extend('silly');
const RedisStore = ConnectRedis(session);
@ -22,7 +23,7 @@ export default {
const pubClient = createClient(flecks);
const subClient = createClient(flecks);
await Promise.all([pubClient.connect(), subClient.connect()]);
debug('creating adapter');
debugSilly('creating adapter');
return {
adapter: redisAdapter(pubClient, subClient),
};

View File

@ -6,6 +6,7 @@ import createReducer from './store/create-reducer';
import configureStore from './store';
const debug = D('@flecks/redux/server');
const debugSilly = debug.extend('silly');
export default {
[Hooks]: {
@ -17,7 +18,7 @@ export default {
Object.values(slices).map(({hydrateServer}) => hydrateServer?.(req, flecks)),
);
const preloadedState = reducer(undefined, hydrateServer({flecks, req}));
debug(
debugSilly(
'creating redux store with slices(%O) and state(%O)',
Object.keys(slices),
preloadedState,

View File

@ -7,21 +7,22 @@ import {start} from 'repl';
import {D} from '@flecks/core';
const debug = D('@flecks/repl');
const debugSilly = debug.extend('silly');
export async function createReplServer(flecks) {
const {id} = flecks.get('@flecks/core');
const context = flecks.invokeFlat('@flecks/repl.context')
.reduce((r, vars) => ({...r, ...vars}), {flecks});
debug(
'context = %O',
Object.fromEntries(Object.entries(context).map(([key]) => [key, '...'])),
'Object.keys(context) === %O',
Object.keys(context),
);
const commands = {};
Object.entries(
flecks.invokeFlat('@flecks/repl.commands').reduce((r, commands) => ({...r, ...commands}), {}),
).forEach(([key, value]) => {
commands[key] = value;
debug('registered command: %s', key);
debugSilly('registered command: %s', key);
});
const netServer = createServer((socket) => {
debug('client connection to repl established');

View File

@ -7,7 +7,7 @@ import createIntercom from './create-intercom';
import ServerSocket from './socket';
const debug = D('@flecks/socket/server/sockets');
const debugSilly = D('@flecks/socket/server/sockets:silly');
const debugSilly = debug.extend('silly');
export default class SocketServer {
@ -16,9 +16,9 @@ export default class SocketServer {
this.flecks = flecks;
this.httpServer = httpServer;
const hooks = flecks.invokeMerge('@flecks/socket.intercom');
debug('intercom hooks(%O)', hooks);
debugSilly('intercom hooks(%O)', hooks);
this.localIntercom = async ({payload, type}, fn) => {
debug('customHook: %s(%o)', type, payload);
debugSilly('customHook: %s(%o)', type, payload);
if (hooks[type]) {
fn(await hooks[type](payload, this));
}

View File

@ -3,14 +3,15 @@ import passport from 'passport';
import LogOps from 'passport/lib/http/request';
const debug = D('@flecks/user/passport');
const debugSilly = debug.extend('silly');
export default {
[Hooks]: {
'@flecks/db/server.models': Flecks.provide(require.context('./models', false, /\.js$/)),
'@flecks/web/server.request.route': (flecks) => (req, res, next) => {
debug('@flecks/web/server.request.route: passport.initialize()');
debugSilly('@flecks/web/server.request.route: passport.initialize()');
passport.initialize()(req, res, () => {
debug('@flecks/web/server.request.route: passport.session()');
debugSilly('@flecks/web/server.request.route: passport.session()');
passport.session()(req, res, () => {
if (!req.user) {
const {User} = flecks.get('$flecks/db.models');
@ -58,9 +59,9 @@ export default {
},
}),
'@flecks/socket/server.request.socket': (flecks) => (socket, next) => {
debug('@flecks/socket/server.request.socket: passport.initialize()');
debugSilly('@flecks/socket/server.request.socket: passport.initialize()');
passport.initialize()(socket.handshake, undefined, () => {
debug('@flecks/socket/server.request.socket: passport.session()');
debugSilly('@flecks/socket/server.request.socket: passport.session()');
passport.session()(socket.handshake, undefined, async () => {
/* eslint-disable no-param-reassign */
if (!socket.handshake.user) {

View File

@ -3,6 +3,7 @@ import express from 'express';
import expressSession from 'express-session';
const debug = D('@flecks/user/session');
const debugSilly = debug.extend('silly');
export default {
[Hooks]: {
@ -19,19 +20,19 @@ export default {
'@flecks/web/server.request.route': (flecks) => {
const urle = express.urlencoded({extended: true});
return (req, res, next) => {
debug('@flecks/web/server.request.route: express.urlencoded()');
debugSilly('@flecks/web/server.request.route: express.urlencoded()');
urle(req, res, (error) => {
if (error) {
next(error);
return;
}
debug('@flecks/web/server.request.route: session()');
debugSilly('@flecks/web/server.request.route: session()');
flecks.get('$flecks/user.session')(req, res, (error) => {
if (error) {
next(error);
return;
}
debug('session ID: %s', req.session.id);
debugSilly('session ID: %s', req.session.id);
next();
});
});
@ -47,7 +48,7 @@ export default {
}));
},
'@flecks/socket/server.request.socket': (flecks) => (socket, next) => {
debug('@flecks/socket/server.request.socket: session()');
debugSilly('@flecks/socket/server.request.socket: session()');
flecks.get('$flecks/user.session')(socket.handshake, {}, () => {
const id = socket.handshake.session?.id;
socket.join(id);

View File

@ -129,6 +129,7 @@ export default {
* (webpack-dev-server) Webpack stats output.
*/
devStats: {
assets: false,
chunks: false,
colors: true,
modules: false,