Skip to content

Commit

Permalink
feat(@whook/whook): improve the build autoloader
Browse files Browse the repository at this point in the history
Now that the intializers can embed their location, we have a second chance to autoload the modules
for the build.
  • Loading branch information
nfroidure committed Dec 4, 2024
1 parent 21d4987 commit 75132c3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 30 deletions.
3 changes: 3 additions & 0 deletions packages/whook-example/src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('commands should work', () => {
}).toMatchInlineSnapshot(`
{
"stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js".
🤖 - Initializing the \`$autoload\` service.
On air 🚀🌕
",
"stdout": "
Expand Down Expand Up @@ -53,6 +54,7 @@ On air 🚀🌕
}).toMatchInlineSnapshot(`
{
"stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js".
🤖 - Initializing the \`$autoload\` service.
🔴 - Running with "local" application environment.
🔂 - Running with "test" node environment.
On air 🚀🌕
Expand All @@ -74,6 +76,7 @@ On air 🚀🌕
}).toMatchInlineSnapshot(`
{
"stderr": "⚡ - Loading configurations from /whook/packages/whook-example/dist/config/local/config.js".
🤖 - Initializing the \`$autoload\` service.
On air 🚀🌕
",
"stdout": ""localhost"
Expand Down
6 changes: 3 additions & 3 deletions packages/whook-example/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1164,9 +1164,6 @@ describe('runProcess', () => {
[
"🛂 - Initializing the importer!",
],
[
"🤖 - Initializing the \`$autoload\` service.",
],
[
"🦄 - Initializing the API service!",
],
Expand Down Expand Up @@ -1199,6 +1196,9 @@ describe('runProcess', () => {
[
"🕱 -Wrapping the error handler for CORS.",
],
[
"🤖 - Initializing the \`$autoload\` service.",
],
],
}
`);
Expand Down
2 changes: 1 addition & 1 deletion packages/whook/src/services/_autoload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('$autoload', () => {
},
"logCalls": [
[
"debug",
"warning",
"🤖 - Initializing the \`$autoload\` service.",
],
[
Expand Down
2 changes: 1 addition & 1 deletion packages/whook/src/services/_autoload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async function initAutoload({
}: WhookAutoloadDependencies): Promise<
Autoloader<Initializer<unknown, Dependencies>>
> {
log('debug', '🤖 - Initializing the `$autoload` service.');
log('warning', '🤖 - Initializing the `$autoload` service.');

/* Architecture Note #2.9.2: the `API` auto loading
We cannot inject the `API` in the auto loader since
Expand Down
57 changes: 39 additions & 18 deletions packages/whook/src/services/_buildAutoload.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import initAutoload from './_autoload.js';
import { noop } from '../libs/utils.js';
import {
UNBUILDABLE_SERVICES,
SPECIAL_PROPS,
wrapInitializer,
constant,
alsoInject,
location,
type Knifecycle,
type Autoloader,
type Initializer,
type Dependencies,
type Service,
type ServiceInitializerWrapper,
} from 'knifecycle';
import type { WhookBuildConstantsService } from '../services/BUILD_CONSTANTS.js';
import type {
Knifecycle,
Autoloader,
Initializer,
Dependencies,
Service,
ServiceInitializerWrapper,
} from 'knifecycle';
import { type WhookBuildConstantsService } from '../services/BUILD_CONSTANTS.js';
import { printStackTrace } from 'yerror';
import type { LogService } from 'common-services';
import { noop, type LogService } from 'common-services';

const initializerWrapper: ServiceInitializerWrapper<
Autoloader<Initializer<Dependencies, Service>>,
Expand All @@ -40,7 +38,7 @@ const initializerWrapper: ServiceInitializerWrapper<
path: string;
}>
> => {
log('debug', '🤖 - Initializing the `$autoload` build wrapper.');
log('warning', '🤖 - Initializing the `$autoload` build wrapper.');

return async (serviceName) => {
if (UNBUILDABLE_SERVICES.includes(serviceName)) {
Expand Down Expand Up @@ -78,10 +76,30 @@ const initializerWrapper: ServiceInitializerWrapper<
return constant(serviceName, BUILD_CONSTANTS[serviceName]);
}

return $autoload(serviceName);
try {
return await $autoload(serviceName);
} catch (err) {
if (initializer && initializer[SPECIAL_PROPS.LOCATION]) {
const reshapedUrl = initializer[SPECIAL_PROPS.LOCATION].url.replace(
/^(?:.*)\/node_modules\/(.*)$/,
'$1',
);

log(
'error',
`🤖 - Could not auto load "${serviceName}", trying to find it in the initializer embedded location (${reshapedUrl}).`,
);
log('debug-stack', printStackTrace(err as Error));

// Assuming the module name is after the last `node_modules`
// folder. May not be the best approach
return location(initializer, reshapedUrl);
}
throw err;
}
} catch (err) {
log('error', `Build error while loading "${serviceName}".`);
log('error-stack', printStackTrace(err as Error));
log('debug', `🤖 - Unable to load "${serviceName}".`);
log('debug-stack', printStackTrace(err as Error));
throw err;
}
};
Expand All @@ -103,7 +121,10 @@ const initializerWrapper: ServiceInitializerWrapper<
* @return {Promise<Object>}
* A promise of an object containing the reshaped env vars.
*/
export default alsoInject(
['?BUILD_CONSTANTS', '$instance', '$injector', '?log'],
wrapInitializer(initializerWrapper as any, initAutoload),
export default location(
alsoInject(
['?BUILD_CONSTANTS', '$instance', '$injector', '?log'],
wrapInitializer(initializerWrapper as any, initAutoload),
),
import.meta.url,
);
14 changes: 7 additions & 7 deletions packages/whook/src/services/_cliAutoload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('$autoload', () => {
"injectorCalls": [],
"logCalls": [
[
"debug",
"warning",
"🤖 - Initializing the \`$autoload\` service.",
],
[
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('$autoload', () => {
"injectorCalls": [],
"logCalls": [
[
"debug",
"warning",
"🤖 - Initializing the \`$autoload\` service.",
],
[
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('$autoload', () => {
"location": undefined,
"logCalls": [
[
"debug",
"warning",
"🤖 - Initializing the \`$autoload\` service.",
],
[
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('$autoload', () => {
"location": undefined,
"logCalls": [
[
"debug",
"warning",
"🤖 - Initializing the \`$autoload\` service.",
],
[
Expand Down Expand Up @@ -314,7 +314,7 @@ describe('$autoload', () => {
"injectorCalls": [],
"logCalls": [
[
"debug",
"warning",
"🤖 - Initializing the \`$autoload\` service.",
],
[
Expand Down Expand Up @@ -368,7 +368,7 @@ describe('$autoload', () => {
"injectorCalls": [],
"logCalls": [
[
"debug",
"warning",
"🤖 - Initializing the \`$autoload\` service.",
],
],
Expand Down Expand Up @@ -419,7 +419,7 @@ describe('$autoload', () => {
"injectorCalls": [],
"logCalls": [
[
"debug",
"warning",
"🤖 - Initializing the \`$autoload\` service.",
],
],
Expand Down

0 comments on commit 75132c3

Please sign in to comment.