Skip to content

Commit

Permalink
refactor(build): isolate build functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Simply moving functions from `index.ts` to `build.ts`.

fix #104
  • Loading branch information
nfroidure committed Feb 28, 2021
1 parent f4569eb commit 17f810e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 64 deletions.
7 changes: 3 additions & 4 deletions packages/whook-aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ npm i --save-dev @whook/http-transaction babel-loader babel-plugin-knifecycle we
```

Add this module to your Whook plugins and tweak the 2 build functions in your
`index.ts` main file:
`build.ts` main file:

```diff
+import {
Expand Down Expand Up @@ -84,7 +84,7 @@ export async function prepareBuildEnvironment(

- // Usually, here you call the installed build env
- // $ = await prepareBaseBuildEnvironment($);
+ // Calling the AWS specific build
+ // Calling the AWS specific build env
+ $ = await prepareBaseBuildEnvironment($);


Expand All @@ -93,8 +93,7 @@ export async function prepareBuildEnvironment(
// remove the need to create an injector
$.register(
constant('INITIALIZER_PATH_MAP', {
ENV: '@whook/whook/dist/services/ProxyedENV',
apm: '@whook/http-transaction/dist/services/apm',
// (...)
obfuscator: '@whook/http-transaction/dist/services/obfuscator',
- log: 'common-services/dist/log',
+ log: '@whook/aws-lambda/dist/services/log',
Expand Down
2 changes: 1 addition & 1 deletion packages/whook-example/bin/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env node

const { runBuild } = require('../dist/index');
const { runBuild } = require('../dist/build');

runBuild();
51 changes: 51 additions & 0 deletions packages/whook-example/src/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Knifecycle, { constant, alsoInject } from 'knifecycle';
import { initBuildConstants } from '@whook/whook';
import { prepareEnvironment } from '.';
import YError from 'yerror';
import type { Dependencies } from 'knifecycle';

// Per convention a Whook server build file must export
// the following 2 functions to be composable:

// The `runBuild` function is intended to build the
// project
export async function runBuild(
innerPrepareEnvironment = prepareBuildEnvironment,
): Promise<void> {
throw new YError('E_NO_BUILD_IMPLEMENTED');

// Usually, here you call the installed build
// return runBaseBuild(innerPrepareEnvironment);
}

// The `prepareBuildEnvironment` create the build
// environment
export async function prepareBuildEnvironment<
T extends Knifecycle<Dependencies>
>($: T = new Knifecycle() as T): Promise<T> {
$ = await prepareEnvironment($);

// Usually, here you call the installed build env
// $ = await prepareBaseBuildEnvironment($);

// The build often need to know were initializer
// can be found to create a static build and
// remove the need to create an injector
$.register(
constant('INITIALIZER_PATH_MAP', {
ENV: '@whook/whook/dist/services/ProxyedENV',
apm: '@whook/http-transaction/dist/services/apm',
obfuscator: '@whook/http-transaction/dist/services/obfuscator',
errorHandler: '@whook/http-router/dist/services/errorHandler',
log: 'common-services/dist/log',
time: 'common-services/dist/time',
delay: 'common-services/dist/delay',
}),
);

// Finally, some constants can be serialized instead of being
// initialized in the target build saving some time at boot
$.register(alsoInject(['API_DEFINITIONS'], initBuildConstants));

return $;
}
50 changes: 1 addition & 49 deletions packages/whook-example/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Knifecycle, { constant, alsoInject } from 'knifecycle';
import Knifecycle, { constant } from 'knifecycle';
import {
runServer as runBaseServer,
prepareServer as prepareBaseServer,
prepareEnvironment as prepareBaseEnvironment,
initAutoload,
initAPIDefinitions,
initBuildConstants,
} from '@whook/whook';
import initHTTPRouter from '@whook/http-router';
import wrapHTTPRouterWithSwaggerUI from '@whook/swagger-ui';
import YError from 'yerror';
import type { DependencyDeclaration, Dependencies } from 'knifecycle';

// Per convention a Whook server main file must export
Expand Down Expand Up @@ -85,49 +83,3 @@ export async function prepareEnvironment<T extends Knifecycle<Dependencies>>(

return $;
}

// Additionnally, 2 others functions can be exported
// for building purpose

// The `runBuild` function is intended to build the
// project
export async function runBuild(
innerPrepareEnvironment = prepareBuildEnvironment,
): Promise<void> {
throw new YError('E_NO_BUILD_IMPLEMENTED');

// Usually, here you call the installed build
// return runBaseBuild(innerPrepareEnvironment);
}

// The `prepareBuildEnvironment` create the build
// environment
export async function prepareBuildEnvironment<
T extends Knifecycle<Dependencies>
>($: T = new Knifecycle() as T): Promise<T> {
$ = await prepareEnvironment($);

// Usually, here you call the installed build env
// $ = await prepareBaseBuildEnvironment($);

// The build often need to know were initializer
// can be found to create a static build and
// remove the need to create an injector
$.register(
constant('INITIALIZER_PATH_MAP', {
ENV: '@whook/whook/dist/services/ProxyedENV',
apm: '@whook/http-transaction/dist/services/apm',
obfuscator: '@whook/http-transaction/dist/services/obfuscator',
errorHandler: '@whook/http-router/dist/services/errorHandler',
log: 'common-services/dist/log',
time: 'common-services/dist/time',
delay: 'common-services/dist/delay',
}),
);

// Finally, some constants can be serialized instead of being
// initialized in the target build saving some time at boot
$.register(alsoInject(['API_DEFINITIONS'], initBuildConstants));

return $;
}
9 changes: 4 additions & 5 deletions packages/whook-gcp-functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ npm i --save-dev @whook/http-transaction babel-loader babel-plugin-knifecycle we
```

Add this module to your Whook plugins and tweak the 2 build functions in your
`index.ts` main file:
`build.ts` main file:

```diff
+import {
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function prepareBuildEnvironment(

- // Usually, here you call the installed build env
- // $ = await prepareBaseBuildEnvironment($);
+ // Calling the GCP specific build
+ // Calling the GCP specific build env
+ $ = await prepareBaseBuildEnvironment($);


Expand All @@ -94,8 +94,7 @@ export async function prepareBuildEnvironment(
// remove the need to create an injector
$.register(
constant('INITIALIZER_PATH_MAP', {
ENV: '@whook/whook/dist/services/ProxyedENV',
apm: '@whook/http-transaction/dist/services/apm',
// (...)
obfuscator: '@whook/http-transaction/dist/services/obfuscator',
- log: 'common-services/dist/log',
+ log: '@whook/gcp-functions/dist/services/log',
Expand Down Expand Up @@ -130,7 +129,7 @@ const CONFIG: AppConfigs = {
+ ignoredModules: [],
+ extensions: ['.ts', '.js', '.json'],
+ mainFields: ['browser', 'main'],
+ target: '12.13',
+ target: '12',
+ },
};

Expand Down
10 changes: 5 additions & 5 deletions packages/whook-swagger-ui/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Object {
<head>
<meta charset=\\"UTF-8\\">
<title>Swagger UI</title>
<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"./swagger-ui.css\\" >
<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"./swagger-ui.css\\" />
<link rel=\\"icon\\" type=\\"image/png\\" href=\\"./favicon-32x32.png\\" sizes=\\"32x32\\" />
<link rel=\\"icon\\" type=\\"image/png\\" href=\\"./favicon-16x16.png\\" sizes=\\"16x16\\" />
<style>
Expand Down Expand Up @@ -243,11 +243,11 @@ Object {
SwaggerUIBundle.plugins.DownloadUrl
],
layout: \\"StandaloneLayout\\"
})
});
// End Swagger UI call region
window.ui = ui
}
window.ui = ui;
};
</script>
</body>
</html>
Expand Down Expand Up @@ -297,7 +297,7 @@ Object {
"headers": Object {
"cache-control": "max-age=3600",
"connection": "close",
"content-length": "1456",
"content-length": "1460",
"content-type": "text/html",
"date": undefined,
"etag": undefined,
Expand Down

0 comments on commit 17f810e

Please sign in to comment.