1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const archiver = require("archiver");
|
5 | const fs = require("fs-extra");
|
6 | const path = require("path");
|
7 | const base_command_1 = require("../../base-command");
|
8 | const decorators_1 = require("../../utils/decorators");
|
9 | const prepare_config_1 = require("../../utils/prepare-config");
|
10 | const CONFIG_FILE = 'bearer.config.json';
|
11 | const HANDLER_NAME = 'index';
|
12 | const HANDLER_NAME_WITH_EXT = [HANDLER_NAME, 'js'].join('.');
|
13 | class PackFunctions extends base_command_1.default {
|
14 | async run() {
|
15 |
|
16 | const { args } = this.parse(PackFunctions);
|
17 | const { ARCHIVE_PATH } = args;
|
18 | const target = path.resolve(ARCHIVE_PATH);
|
19 | this.debug(`Zipping to: ${target}`);
|
20 | const output = fs.createWriteStream(target);
|
21 | const archive = archiver('zip', {
|
22 | zlib: { level: 9 }
|
23 | });
|
24 | const archiveEntries = [];
|
25 | archive.pipe(output);
|
26 | const archived = new Promise((resolve, reject) => {
|
27 | output.on('close', () => {
|
28 | resolve(archiveEntries);
|
29 | });
|
30 | archive.on('error', (err) => {
|
31 | reject(err);
|
32 | });
|
33 | archive.on('entry', (entry) => {
|
34 | if (!entry.stats || !entry.stats.isDirectory()) {
|
35 | archiveEntries.push(entry);
|
36 | }
|
37 | });
|
38 | archive.on('warning', (err) => {
|
39 | this.debug(err);
|
40 | });
|
41 | });
|
42 | try {
|
43 |
|
44 | archive.directory(this.locator.buildFunctionsDir, false);
|
45 |
|
46 | const functions = await this.retrieveFunctions();
|
47 | const { config, handlers } = buildLambdaDefinitions(functions);
|
48 | const finalConfig = await this.retrieveAuthConfig(config);
|
49 | archive.append(JSON.stringify(finalConfig, null, 2), { name: CONFIG_FILE });
|
50 |
|
51 | this.debug(`Generated config: ${JSON.stringify(config, null, 2)}`);
|
52 | archive.append(handlers, { name: HANDLER_NAME_WITH_EXT });
|
53 |
|
54 | archive.finalize();
|
55 | const entries = await archived;
|
56 | this.debug(`Zip content: ${entries.length} files\n * ${entries.map(e => e.name).join('\n * ')}`);
|
57 | this.success('Successfully generated lambda package');
|
58 |
|
59 | }
|
60 | catch (e) {
|
61 | this.error(e);
|
62 | }
|
63 | }
|
64 | async retrieveAuthConfig(config) {
|
65 |
|
66 | const content = await fs.readFile(this.locator.authConfigPath, { encoding: 'utf8' });
|
67 | return Object.assign({}, config, { auth: JSON.parse(content) });
|
68 | }
|
69 |
|
70 | async retrieveFunctions() {
|
71 | try {
|
72 | const config = await prepare_config_1.default(this.locator.authConfigPath, this.bearerConfig.bearerUid, this.locator.srcFunctionsDir);
|
73 | return config.functions;
|
74 | }
|
75 | catch (e) {
|
76 | throw e;
|
77 | }
|
78 | }
|
79 | }
|
80 | PackFunctions.description = 'Pack integration functions';
|
81 | PackFunctions.hidden = true;
|
82 | PackFunctions.flags = Object.assign({}, base_command_1.default.flags);
|
83 | PackFunctions.args = [{ name: 'ARCHIVE_PATH', required: true }];
|
84 | tslib_1.__decorate([
|
85 | decorators_1.RequireIntegrationFolder()
|
86 | ], PackFunctions.prototype, "run", null);
|
87 | exports.default = PackFunctions;
|
88 | function buildLambdaDefinitions(functions) {
|
89 | return {
|
90 | config: {
|
91 | functions: functions.reduce((acc, functionName) => {
|
92 | acc.push({ [functionName]: [HANDLER_NAME, functionName].join('.') });
|
93 | return acc;
|
94 | }, [])
|
95 | },
|
96 | handlers: buildLambdaIndex(functions)
|
97 | };
|
98 | }
|
99 | exports.buildLambdaDefinitions = buildLambdaDefinitions;
|
100 | function buildLambdaIndex(functions) {
|
101 | const handlers = functions
|
102 | .map((func, index) => {
|
103 | const funcConstName = `func${index}`;
|
104 | return `const ${funcConstName} = require("./dist/${func}").default;
|
105 | module.exports['${func}'] = ${funcConstName}.init();
|
106 | `;
|
107 | })
|
108 | .join('\n');
|
109 | return `const captureRequest = require('@bearer/x-ray').default
|
110 | captureRequest()
|
111 | ${handlers}
|
112 | `;
|
113 | }
|