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 | class PackFunctions extends base_command_1.default {
|
12 | async run() {
|
13 |
|
14 | const { args } = this.parse(PackFunctions);
|
15 | const { ARCHIVE_PATH } = args;
|
16 | const target = path.resolve(ARCHIVE_PATH);
|
17 | this.debug(`Zipping to: ${target}`);
|
18 | const output = fs.createWriteStream(target);
|
19 | const archive = archiver('zip', {
|
20 | zlib: { level: 9 }
|
21 | });
|
22 | const archiveEntries = [];
|
23 | archive.pipe(output);
|
24 | const archived = new Promise((resolve, reject) => {
|
25 | output.on('close', () => {
|
26 | resolve(archiveEntries);
|
27 | });
|
28 | archive.on('error', (err) => {
|
29 | reject(err);
|
30 | });
|
31 | archive.on('entry', (entry) => {
|
32 | if (!entry.stats || !entry.stats.isDirectory()) {
|
33 | archiveEntries.push(entry);
|
34 | }
|
35 | });
|
36 | archive.on('warning', (err) => {
|
37 | this.debug(err);
|
38 | });
|
39 | });
|
40 | try {
|
41 |
|
42 | archive.directory(this.locator.buildFunctionsResourcePath('dist'), false);
|
43 |
|
44 | const functions = await this.retrieveFunctions();
|
45 | const { config } = buildLambdaDefinitions(functions);
|
46 | const finalConfig = await this.retrieveAuthConfig(config);
|
47 | archive.append(JSON.stringify(finalConfig, null, 2), { name: CONFIG_FILE });
|
48 |
|
49 | this.debug(`Generated config: ${JSON.stringify(config, null, 2)}`);
|
50 |
|
51 | archive.finalize();
|
52 | const entries = await archived;
|
53 | this.debug(`Zip content: ${entries.length} files\n * ${entries.map(e => e.name).join('\n * ')}`);
|
54 | this.success('Successfully generated lambda package');
|
55 |
|
56 | }
|
57 | catch (e) {
|
58 | this.error(e);
|
59 | }
|
60 | }
|
61 | async retrieveAuthConfig(config) {
|
62 |
|
63 | const content = await fs.readFile(this.locator.authConfigPath, { encoding: 'utf8' });
|
64 | return Object.assign({}, config, { auth: JSON.parse(content) });
|
65 | }
|
66 |
|
67 | async retrieveFunctions() {
|
68 | try {
|
69 | const config = await prepare_config_1.default(this.locator.authConfigPath, this.bearerConfig.bearerUid, this.locator.srcFunctionsDir);
|
70 | return config.functions;
|
71 | }
|
72 | catch (e) {
|
73 | throw e;
|
74 | }
|
75 | }
|
76 | }
|
77 | PackFunctions.description = 'Pack integration functions';
|
78 | PackFunctions.hidden = true;
|
79 | PackFunctions.flags = Object.assign({}, base_command_1.default.flags);
|
80 | PackFunctions.args = [{ name: 'ARCHIVE_PATH', required: true }];
|
81 | tslib_1.__decorate([
|
82 | decorators_1.RequireIntegrationFolder()
|
83 | ], PackFunctions.prototype, "run", null);
|
84 | exports.default = PackFunctions;
|
85 | function buildLambdaDefinitions(functions) {
|
86 | return {
|
87 | config: {
|
88 | functions: functions.reduce((acc, functionName) => {
|
89 | acc.push({ [functionName]: [prepare_config_1.HANDLER_NAME, functionName].join('.') });
|
90 | return acc;
|
91 | }, [])
|
92 | }
|
93 | };
|
94 | }
|
95 | exports.buildLambdaDefinitions = buildLambdaDefinitions;
|