@whook/gcp-functions 18.0.3 → 19.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -57
- package/dist/commands/testGCPFunctionRoute.d.ts +50 -0
- package/dist/commands/{testHTTPFunction.js → testGCPFunctionRoute.js} +44 -46
- package/dist/commands/testGCPFunctionRoute.js.map +1 -0
- package/dist/index.d.ts +8 -7
- package/dist/index.js +50 -54
- package/dist/index.js.map +1 -1
- package/dist/libs/utils.d.ts +1 -1
- package/dist/libs/utils.js +1 -1
- package/dist/libs/utils.js.map +1 -1
- package/dist/services/_autoload.d.ts +16 -12
- package/dist/services/_autoload.js +49 -63
- package/dist/services/_autoload.js.map +1 -1
- package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.d.ts +19 -0
- package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.js +307 -0
- package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.js.map +1 -0
- package/package.json +11 -10
- package/src/commands/{testHTTPFunction.ts → testGCPFunctionRoute.ts} +73 -75
- package/src/index.ts +100 -104
- package/src/libs/utils.ts +2 -2
- package/src/services/_autoload.ts +90 -117
- package/src/types.d.ts +8 -0
- package/src/wrappers/wrapRouteHandlerForGoogleHTTPFunction.ts +571 -0
- package/dist/commands/testHTTPFunction.d.ts +0 -13
- package/dist/commands/testHTTPFunction.js.map +0 -1
- package/dist/services/HANDLER.d.ts +0 -11
- package/dist/services/HANDLER.js +0 -21
- package/dist/services/HANDLER.js.map +0 -1
- package/dist/services/log.d.ts +0 -5
- package/dist/services/log.js +0 -4
- package/dist/services/log.js.map +0 -1
- package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.d.ts +0 -23
- package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.js +0 -272
- package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.js.map +0 -1
- package/src/services/HANDLER.ts +0 -43
- package/src/services/log.ts +0 -7
- package/src/wrappers/wrapHandlerForGoogleHTTPFunction.ts +0 -482
|
@@ -1,82 +1,84 @@
|
|
|
1
1
|
import { loadFunction } from '../libs/utils.js';
|
|
2
|
-
import {
|
|
2
|
+
import { location, autoService } from 'knifecycle';
|
|
3
3
|
import { YError } from 'yerror';
|
|
4
|
+
import stream from 'node:stream';
|
|
4
5
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
WhookCommandDefinition,
|
|
14
|
-
WhookCompilerOptions,
|
|
6
|
+
DEFAULT_COMPILER_OPTIONS,
|
|
7
|
+
PATH_SEPARATOR,
|
|
8
|
+
SEARCH_SEPARATOR,
|
|
9
|
+
type WhookRouteHandlerParameters,
|
|
10
|
+
type WhookCommandHandler,
|
|
11
|
+
type WhookCommandDefinition,
|
|
12
|
+
type WhookCompilerOptions,
|
|
13
|
+
type WhookRoutesDefinitionsService,
|
|
15
14
|
} from '@whook/whook';
|
|
16
|
-
import type
|
|
17
|
-
import type { OpenAPIV3_1 } from 'openapi-types';
|
|
15
|
+
import { type LogService } from 'common-services';
|
|
18
16
|
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
properties: {
|
|
30
|
-
name: {
|
|
31
|
-
description: 'Name of the function to run',
|
|
17
|
+
export const definition = {
|
|
18
|
+
name: 'testGCPFunctionRoute',
|
|
19
|
+
description: 'A command for testing a GCP function route',
|
|
20
|
+
example: `whook testGCPFunctionRoute --name getPing`,
|
|
21
|
+
arguments: [
|
|
22
|
+
{
|
|
23
|
+
description: 'Name of the function to run',
|
|
24
|
+
name: 'name',
|
|
25
|
+
required: true,
|
|
26
|
+
schema: {
|
|
32
27
|
type: 'string',
|
|
33
28
|
},
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'type',
|
|
32
|
+
description: 'Type of function to test',
|
|
33
|
+
schema: {
|
|
36
34
|
type: 'string',
|
|
37
35
|
enum: ['main', 'index'],
|
|
38
36
|
default: 'index',
|
|
39
37
|
},
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'contentType',
|
|
41
|
+
description: 'Content type of the payload',
|
|
42
|
+
schema: {
|
|
42
43
|
type: 'string',
|
|
43
44
|
default: 'application/json',
|
|
44
45
|
},
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'parameters',
|
|
49
|
+
description: 'The HTTP call parameters',
|
|
50
|
+
schema: {
|
|
47
51
|
type: 'string',
|
|
48
52
|
default: '{}',
|
|
49
53
|
},
|
|
50
54
|
},
|
|
51
|
-
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
export default extra(definition, autoService(initTestHTTPFunctionCommand));
|
|
55
|
+
],
|
|
56
|
+
} as const satisfies WhookCommandDefinition;
|
|
55
57
|
|
|
56
|
-
async function
|
|
58
|
+
async function initTestGCPFunctionRouteCommand({
|
|
57
59
|
APP_ENV,
|
|
58
60
|
PROJECT_DIR,
|
|
59
61
|
COMPILER_OPTIONS = DEFAULT_COMPILER_OPTIONS,
|
|
60
|
-
|
|
62
|
+
ROUTES_DEFINITIONS,
|
|
61
63
|
log,
|
|
62
|
-
args,
|
|
63
64
|
}: {
|
|
64
65
|
APP_ENV: string;
|
|
65
66
|
PROJECT_DIR: string;
|
|
66
67
|
COMPILER_OPTIONS?: WhookCompilerOptions;
|
|
67
|
-
|
|
68
|
+
ROUTES_DEFINITIONS: WhookRoutesDefinitionsService;
|
|
68
69
|
log: LogService;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
}): Promise<
|
|
71
|
+
WhookCommandHandler<{
|
|
72
|
+
name: string;
|
|
73
|
+
type: string;
|
|
74
|
+
contentType: string;
|
|
75
|
+
parameters: string;
|
|
76
|
+
}>
|
|
77
|
+
> {
|
|
78
|
+
return async (args) => {
|
|
72
79
|
const {
|
|
73
80
|
namedArguments: { name, type, contentType, parameters: rawParameters },
|
|
74
|
-
} =
|
|
75
|
-
name: string;
|
|
76
|
-
type: string;
|
|
77
|
-
contentType: string;
|
|
78
|
-
parameters: string;
|
|
79
|
-
}>(definition.arguments, args);
|
|
81
|
+
} = args;
|
|
80
82
|
const extension = COMPILER_OPTIONS.format === 'cjs' ? '.cjs' : '.mjs';
|
|
81
83
|
const handler = await loadFunction(
|
|
82
84
|
{ APP_ENV, PROJECT_DIR, log },
|
|
@@ -84,61 +86,52 @@ async function initTestHTTPFunctionCommand({
|
|
|
84
86
|
type,
|
|
85
87
|
extension,
|
|
86
88
|
);
|
|
87
|
-
const
|
|
88
|
-
await dereferenceOpenAPIOperations(API, getOpenAPIOperations(API))
|
|
89
|
-
).find(({ operationId }) => operationId === name);
|
|
89
|
+
const handlerDefinition = ROUTES_DEFINITIONS[name]?.module?.definition;
|
|
90
90
|
|
|
91
|
-
if (!
|
|
91
|
+
if (!handlerDefinition) {
|
|
92
92
|
throw new YError('E_OPERATION_NOT_FOUND');
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
const hasBody = !!
|
|
96
|
-
const parameters = JSON.parse(rawParameters);
|
|
97
|
-
const search = (
|
|
98
|
-
(
|
|
99
|
-
|
|
100
|
-
.filter((p) => p.in === 'query')
|
|
101
|
-
.reduce((accSearch, p) => {
|
|
102
|
-
if (null != parameters[p.name]) {
|
|
95
|
+
const hasBody = !!handlerDefinition.operation.requestBody;
|
|
96
|
+
const parameters = JSON.parse(rawParameters) as WhookRouteHandlerParameters;
|
|
97
|
+
const search = Object.keys(parameters.query || {}).reduce(
|
|
98
|
+
(accSearch, name) => {
|
|
99
|
+
if (null != parameters.query[name]) {
|
|
103
100
|
return (
|
|
104
101
|
accSearch +
|
|
105
102
|
(accSearch ? '&' : '') +
|
|
106
|
-
|
|
103
|
+
name +
|
|
107
104
|
'=' +
|
|
108
|
-
parameters[
|
|
105
|
+
parameters.query[name]
|
|
109
106
|
);
|
|
110
107
|
}
|
|
111
108
|
return accSearch;
|
|
112
|
-
},
|
|
109
|
+
},
|
|
110
|
+
'',
|
|
111
|
+
);
|
|
113
112
|
|
|
114
|
-
const path =
|
|
113
|
+
const path = handlerDefinition.path
|
|
115
114
|
.split(PATH_SEPARATOR)
|
|
116
|
-
|
|
117
115
|
.map((part) => {
|
|
118
116
|
const matches = /^\{([\d\w]+)\}$/i.exec(part);
|
|
119
117
|
|
|
120
118
|
if (matches) {
|
|
121
|
-
return parameters[matches[1]];
|
|
119
|
+
return parameters.path?.[matches[1]];
|
|
122
120
|
}
|
|
123
121
|
return part;
|
|
124
122
|
})
|
|
125
123
|
.join(PATH_SEPARATOR);
|
|
126
124
|
const gcpfRequest = {
|
|
127
|
-
method:
|
|
125
|
+
method: handlerDefinition.method,
|
|
128
126
|
originalUrl: path + (search ? SEARCH_SEPARATOR + search : ''),
|
|
129
|
-
headers:
|
|
130
|
-
.filter((p) => p.in === 'header')
|
|
131
|
-
.reduce((headerParameters, p) => {
|
|
132
|
-
headerParameters[p.name] = parameters[camelCase(p.name)];
|
|
133
|
-
return headerParameters;
|
|
134
|
-
}, {}),
|
|
127
|
+
headers: parameters.header || {},
|
|
135
128
|
rawBody: Buffer.from(
|
|
136
129
|
hasBody
|
|
137
130
|
? contentType === 'application/json'
|
|
138
131
|
? parameters.body
|
|
139
132
|
? JSON.stringify(parameters.body)
|
|
140
133
|
: ''
|
|
141
|
-
: parameters.body || ''
|
|
134
|
+
: (parameters.body as string) || ''
|
|
142
135
|
: '',
|
|
143
136
|
),
|
|
144
137
|
};
|
|
@@ -183,3 +176,8 @@ async function initTestHTTPFunctionCommand({
|
|
|
183
176
|
log('info', 'SUCCESS:', response);
|
|
184
177
|
};
|
|
185
178
|
}
|
|
179
|
+
|
|
180
|
+
export default location(
|
|
181
|
+
autoService(initTestGCPFunctionRouteCommand),
|
|
182
|
+
import.meta.url,
|
|
183
|
+
);
|
package/src/index.ts
CHANGED
|
@@ -6,49 +6,46 @@ import { pathToFileURL } from 'node:url';
|
|
|
6
6
|
import { mkdirp } from 'mkdirp';
|
|
7
7
|
import cpr from 'cpr';
|
|
8
8
|
import { printStackTrace, YError } from 'yerror';
|
|
9
|
-
import { Knifecycle, constant, initInitializerBuilder } from 'knifecycle';
|
|
10
9
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
Knifecycle,
|
|
11
|
+
constant,
|
|
12
|
+
initInitializerBuilder,
|
|
13
|
+
type Autoloader,
|
|
14
|
+
type Dependencies,
|
|
15
|
+
type BuildInitializer,
|
|
16
|
+
type Initializer,
|
|
17
|
+
type Service,
|
|
18
|
+
} from 'knifecycle';
|
|
14
19
|
import initBuildAutoloader from './services/_autoload.js';
|
|
15
20
|
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
Dependencies,
|
|
22
|
-
BuildInitializer,
|
|
23
|
-
Initializer,
|
|
24
|
-
Service,
|
|
25
|
-
} from 'knifecycle';
|
|
26
|
-
import type {
|
|
27
|
-
WhookOperation,
|
|
28
|
-
WhookCompilerOptions,
|
|
29
|
-
WhookCompilerService,
|
|
21
|
+
DEFAULT_BUILD_INITIALIZER_PATH_MAP as BASE_DEFAULT_BUILD_INITIALIZER_PATH_MAP,
|
|
22
|
+
initCompiler,
|
|
23
|
+
type WhookDefinitions,
|
|
24
|
+
type WhookCompilerOptions,
|
|
25
|
+
type WhookCompilerService,
|
|
30
26
|
} from '@whook/whook';
|
|
31
|
-
import type
|
|
32
|
-
import type
|
|
33
|
-
import type { CprOptions } from 'cpr';
|
|
27
|
+
import { type LogService } from 'common-services';
|
|
28
|
+
import { type CprOptions } from 'cpr';
|
|
34
29
|
import { parseArgs } from '@whook/whook/dist/libs/args.js';
|
|
30
|
+
import initWrapRouteHandlerForGoogleHTTPFunction from './wrappers/wrapRouteHandlerForGoogleHTTPFunction.js';
|
|
35
31
|
|
|
36
32
|
export const DEFAULT_BUILD_PARALLELISM = 10;
|
|
37
33
|
export const DEFAULT_BUILD_INITIALIZER_PATH_MAP = {
|
|
38
34
|
...BASE_DEFAULT_BUILD_INITIALIZER_PATH_MAP,
|
|
39
|
-
log: '@whook/
|
|
35
|
+
log: '@whook/whook/dist/services/rawLog.js',
|
|
40
36
|
};
|
|
41
37
|
|
|
42
|
-
export type
|
|
38
|
+
export type * from './wrappers/wrapRouteHandlerForGoogleHTTPFunction.js';
|
|
39
|
+
export { initWrapRouteHandlerForGoogleHTTPFunction };
|
|
40
|
+
|
|
41
|
+
export type WhookGCPFunctionBuildConfig = {
|
|
43
42
|
BUILD_PARALLELISM?: number;
|
|
44
43
|
};
|
|
45
|
-
export type
|
|
46
|
-
type?: 'http';
|
|
47
|
-
sourceOperationId?: string;
|
|
44
|
+
export type WhookGCPFunctionBaseConfig = {
|
|
48
45
|
staticFiles?: string[];
|
|
49
46
|
compilerOptions?: WhookCompilerOptions;
|
|
50
|
-
suffix?: string;
|
|
51
47
|
};
|
|
48
|
+
export type WhookGCPFunctionRouteConfig = WhookGCPFunctionBaseConfig;
|
|
52
49
|
|
|
53
50
|
const cprAsync = promisify(cpr) as (
|
|
54
51
|
source: string,
|
|
@@ -85,15 +82,15 @@ export async function runBuild(
|
|
|
85
82
|
compiler,
|
|
86
83
|
log,
|
|
87
84
|
$autoload,
|
|
88
|
-
|
|
85
|
+
DEFINITIONS,
|
|
89
86
|
buildInitializer,
|
|
90
|
-
}:
|
|
87
|
+
}: WhookGCPFunctionBuildConfig & {
|
|
91
88
|
APP_ENV: string;
|
|
92
89
|
PROJECT_DIR: string;
|
|
93
90
|
compiler: WhookCompilerService;
|
|
94
91
|
log: LogService;
|
|
95
92
|
$autoload: Autoloader<Initializer<Dependencies, Service>>;
|
|
96
|
-
|
|
93
|
+
DEFINITIONS: WhookDefinitions;
|
|
97
94
|
buildInitializer: BuildInitializer;
|
|
98
95
|
} = await $.run([
|
|
99
96
|
'APP_ENV',
|
|
@@ -103,43 +100,30 @@ export async function runBuild(
|
|
|
103
100
|
'compiler',
|
|
104
101
|
'log',
|
|
105
102
|
'$autoload',
|
|
106
|
-
'
|
|
103
|
+
'DEFINITIONS',
|
|
107
104
|
'buildInitializer',
|
|
108
105
|
]);
|
|
109
106
|
|
|
110
107
|
log('info', 'GCP Functions build Environment initialized 🚀🌕');
|
|
111
108
|
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
getOpenAPIOperations<WhookAPIOperationGCPFunctionConfig>(API),
|
|
116
|
-
)
|
|
117
|
-
).filter((operation) => {
|
|
118
|
-
if (handlerName) {
|
|
119
|
-
const sourceOperationId =
|
|
120
|
-
operation['x-whook'] && operation['x-whook'].sourceOperationId;
|
|
121
|
-
|
|
122
|
-
return (
|
|
123
|
-
handlerName === operation.operationId ||
|
|
124
|
-
handlerName === sourceOperationId
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
return true;
|
|
128
|
-
});
|
|
109
|
+
const handlerNames = Object.keys(DEFINITIONS.configs).filter(
|
|
110
|
+
(aHandlerName) => handlerName === aHandlerName || !handlerName,
|
|
111
|
+
);
|
|
129
112
|
|
|
130
|
-
log('warning', `📃 - ${
|
|
113
|
+
log('warning', `📃 - ${handlerNames.length} handlerNames to process.`);
|
|
131
114
|
|
|
132
|
-
await
|
|
115
|
+
await processHandlers(
|
|
133
116
|
{
|
|
134
117
|
APP_ENV,
|
|
135
118
|
BUILD_PARALLELISM: BUILD_PARALLELISM || DEFAULT_BUILD_PARALLELISM,
|
|
136
119
|
PROJECT_DIR,
|
|
120
|
+
DEFINITIONS,
|
|
137
121
|
compiler,
|
|
138
122
|
log,
|
|
139
123
|
$autoload,
|
|
140
124
|
buildInitializer,
|
|
141
125
|
},
|
|
142
|
-
|
|
126
|
+
handlerNames,
|
|
143
127
|
);
|
|
144
128
|
await $.destroy();
|
|
145
129
|
} catch (err) {
|
|
@@ -150,124 +134,128 @@ export async function runBuild(
|
|
|
150
134
|
}
|
|
151
135
|
}
|
|
152
136
|
|
|
153
|
-
async function
|
|
137
|
+
async function processHandlers(
|
|
154
138
|
{
|
|
155
139
|
APP_ENV,
|
|
156
140
|
BUILD_PARALLELISM,
|
|
157
141
|
PROJECT_DIR,
|
|
142
|
+
DEFINITIONS,
|
|
158
143
|
compiler,
|
|
159
144
|
log,
|
|
160
145
|
$autoload,
|
|
161
146
|
buildInitializer,
|
|
162
|
-
}:
|
|
147
|
+
}: WhookGCPFunctionBuildConfig & {
|
|
163
148
|
APP_ENV: string;
|
|
164
149
|
PROJECT_DIR: string;
|
|
150
|
+
DEFINITIONS: WhookDefinitions;
|
|
165
151
|
compiler: WhookCompilerService;
|
|
166
152
|
log: LogService;
|
|
167
153
|
$autoload: Autoloader<Initializer<Dependencies, Service>>;
|
|
168
154
|
buildInitializer: BuildInitializer;
|
|
169
155
|
},
|
|
170
|
-
|
|
156
|
+
handlerNames: string[],
|
|
171
157
|
): Promise<void> {
|
|
172
|
-
const
|
|
158
|
+
const handlerNamesLeft = handlerNames.slice(BUILD_PARALLELISM);
|
|
173
159
|
|
|
174
160
|
await Promise.all(
|
|
175
|
-
|
|
176
|
-
|
|
161
|
+
handlerNames.slice(0, BUILD_PARALLELISM).map((handlerName) =>
|
|
162
|
+
buildHandler(
|
|
177
163
|
{
|
|
178
164
|
APP_ENV,
|
|
179
165
|
PROJECT_DIR,
|
|
166
|
+
DEFINITIONS,
|
|
180
167
|
compiler,
|
|
181
168
|
log,
|
|
182
169
|
buildInitializer,
|
|
183
170
|
},
|
|
184
|
-
|
|
171
|
+
handlerName,
|
|
185
172
|
),
|
|
186
173
|
),
|
|
187
174
|
);
|
|
188
175
|
|
|
189
|
-
if (
|
|
190
|
-
log('info', `📃 - ${
|
|
191
|
-
return
|
|
176
|
+
if (handlerNamesLeft.length) {
|
|
177
|
+
log('info', `📃 - ${handlerNamesLeft.length} handlerNames left.`);
|
|
178
|
+
return processHandlers(
|
|
192
179
|
{
|
|
193
180
|
APP_ENV,
|
|
194
181
|
BUILD_PARALLELISM,
|
|
195
182
|
PROJECT_DIR,
|
|
183
|
+
DEFINITIONS,
|
|
196
184
|
compiler,
|
|
197
185
|
log,
|
|
198
186
|
$autoload,
|
|
199
187
|
buildInitializer,
|
|
200
188
|
},
|
|
201
|
-
|
|
189
|
+
handlerNamesLeft,
|
|
202
190
|
);
|
|
203
191
|
}
|
|
204
|
-
log('info', '🤷 - No more
|
|
192
|
+
log('info', '🤷 - No more handlerNames.');
|
|
205
193
|
}
|
|
206
194
|
|
|
207
|
-
async function
|
|
195
|
+
async function buildHandler(
|
|
208
196
|
{
|
|
209
197
|
APP_ENV,
|
|
210
198
|
PROJECT_DIR,
|
|
199
|
+
DEFINITIONS,
|
|
211
200
|
compiler,
|
|
212
201
|
log,
|
|
213
202
|
buildInitializer,
|
|
214
203
|
}: {
|
|
215
204
|
APP_ENV: string;
|
|
216
205
|
PROJECT_DIR: string;
|
|
206
|
+
DEFINITIONS: WhookDefinitions;
|
|
217
207
|
compiler: WhookCompilerService;
|
|
218
208
|
log: LogService;
|
|
219
209
|
buildInitializer: BuildInitializer;
|
|
220
210
|
},
|
|
221
|
-
|
|
211
|
+
handlerName: string,
|
|
222
212
|
): Promise<void> {
|
|
223
|
-
const { operationId } = operation;
|
|
224
|
-
|
|
225
213
|
try {
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const lambdaPath = join(PROJECT_DIR, 'builds', APP_ENV, finalEntryPoint);
|
|
214
|
+
const definition = DEFINITIONS.configs[handlerName];
|
|
215
|
+
|
|
216
|
+
if (definition.type !== 'route') {
|
|
217
|
+
log('warning', `🚮 - Skipping "${handlerName}"...`);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
log('warning', `🏗 - Building "${handlerName}"...`);
|
|
222
|
+
|
|
223
|
+
const handlerPath = join(PROJECT_DIR, 'builds', APP_ENV, handlerName);
|
|
238
224
|
const srcPath = join(PROJECT_DIR, 'src');
|
|
239
|
-
const srcRelativePath = relative(
|
|
225
|
+
const srcRelativePath = relative(handlerPath, srcPath);
|
|
240
226
|
|
|
241
227
|
const initializerContent = (
|
|
242
|
-
await buildInitializer([`
|
|
228
|
+
await buildInitializer([`MAIN_HANDLER_${handlerName}`])
|
|
243
229
|
).replaceAll(pathToFileURL(srcPath).toString(), srcRelativePath);
|
|
244
|
-
const indexContent = await
|
|
245
|
-
`OPERATION_HANDLER_${finalEntryPoint}`,
|
|
246
|
-
);
|
|
230
|
+
const indexContent = await buildHandlerIndex(`MAIN_HANDLER_${handlerName}`);
|
|
247
231
|
|
|
248
|
-
await mkdirp(
|
|
232
|
+
await mkdirp(handlerPath);
|
|
249
233
|
await Promise.all([
|
|
250
234
|
copyStaticFiles(
|
|
251
235
|
{ PROJECT_DIR, log },
|
|
252
|
-
|
|
253
|
-
|
|
236
|
+
handlerPath,
|
|
237
|
+
definition.config?.staticFiles || [],
|
|
254
238
|
),
|
|
255
239
|
ensureFile(
|
|
256
240
|
{ log },
|
|
257
|
-
join(
|
|
241
|
+
join(handlerPath, 'initialize.js'),
|
|
258
242
|
initializerContent,
|
|
259
243
|
),
|
|
260
|
-
ensureFile({ log }, join(
|
|
244
|
+
ensureFile({ log }, join(handlerPath, 'main.js'), indexContent),
|
|
261
245
|
]);
|
|
262
|
-
await
|
|
246
|
+
await buildFinalHandler(
|
|
247
|
+
{ DEFINITIONS, compiler, log },
|
|
248
|
+
handlerPath,
|
|
249
|
+
handlerName,
|
|
250
|
+
);
|
|
263
251
|
} catch (err) {
|
|
264
|
-
log('error',
|
|
252
|
+
log('error', `💥 - Error building "${handlerName}"...`);
|
|
265
253
|
log('error-stack', printStackTrace(err as Error));
|
|
266
|
-
throw YError.wrap(err as Error, '
|
|
254
|
+
throw YError.wrap(err as Error, 'E_HANDLER_BUILD', handlerName);
|
|
267
255
|
}
|
|
268
256
|
}
|
|
269
257
|
|
|
270
|
-
async function
|
|
258
|
+
async function buildHandlerIndex(handlerName: string): Promise<string> {
|
|
271
259
|
return `// Automatically generated by \`@whook/gcp-functions\`
|
|
272
260
|
import { initialize } from './initialize.js';
|
|
273
261
|
|
|
@@ -275,28 +263,36 @@ const initializationPromise = initialize();
|
|
|
275
263
|
|
|
276
264
|
export default function handler (req, res) {
|
|
277
265
|
return initializationPromise
|
|
278
|
-
.then(services => services['${
|
|
266
|
+
.then(services => services['${handlerName}'](req, res));
|
|
279
267
|
};
|
|
280
268
|
`;
|
|
281
269
|
}
|
|
282
270
|
|
|
283
|
-
async function
|
|
284
|
-
{
|
|
285
|
-
|
|
286
|
-
|
|
271
|
+
async function buildFinalHandler(
|
|
272
|
+
{
|
|
273
|
+
DEFINITIONS,
|
|
274
|
+
compiler,
|
|
275
|
+
log,
|
|
276
|
+
}: {
|
|
277
|
+
DEFINITIONS: WhookDefinitions;
|
|
278
|
+
compiler: WhookCompilerService;
|
|
279
|
+
log: LogService;
|
|
280
|
+
},
|
|
281
|
+
handlerPath: string,
|
|
282
|
+
handlerName: string,
|
|
287
283
|
): Promise<void> {
|
|
288
|
-
const entryPoint = `${
|
|
284
|
+
const entryPoint = `${handlerPath}/main.js`;
|
|
289
285
|
const { extension, contents, mappings } = await compiler(
|
|
290
286
|
entryPoint,
|
|
291
|
-
|
|
287
|
+
DEFINITIONS[handlerName]?.config?.compilerOptions,
|
|
292
288
|
);
|
|
293
289
|
|
|
294
290
|
await Promise.all([
|
|
295
|
-
ensureFile({ log }, join(
|
|
291
|
+
ensureFile({ log }, join(handlerPath, `/index${extension}`), contents),
|
|
296
292
|
mappings
|
|
297
293
|
? ensureFile(
|
|
298
294
|
{ log },
|
|
299
|
-
join(
|
|
295
|
+
join(handlerPath, `/index${extension}.map`),
|
|
300
296
|
mappings,
|
|
301
297
|
)
|
|
302
298
|
: Promise.resolve(),
|
|
@@ -305,7 +301,7 @@ async function buildFinalLambda(
|
|
|
305
301
|
|
|
306
302
|
async function copyStaticFiles(
|
|
307
303
|
{ PROJECT_DIR, log }: { PROJECT_DIR: string; log: LogService },
|
|
308
|
-
|
|
304
|
+
handlerPath: string,
|
|
309
305
|
staticFiles: string[] = [],
|
|
310
306
|
): Promise<void> {
|
|
311
307
|
await Promise.all(
|
|
@@ -314,7 +310,7 @@ async function copyStaticFiles(
|
|
|
314
310
|
await copyFiles(
|
|
315
311
|
{ log },
|
|
316
312
|
join(PROJECT_DIR, 'node_modules', staticFile),
|
|
317
|
-
join(
|
|
313
|
+
join(handlerPath, 'node_modules', staticFile),
|
|
318
314
|
),
|
|
319
315
|
),
|
|
320
316
|
);
|
package/src/libs/utils.ts
CHANGED