UNPKG

3.75 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const archiver = require("archiver");
5const fs = require("fs-extra");
6const path = require("path");
7const base_command_1 = require("../../base-command");
8const decorators_1 = require("../../utils/decorators");
9const prepare_config_1 = require("../../utils/prepare-config");
10const CONFIG_FILE = 'bearer.config.json';
11class PackFunctions extends base_command_1.default {
12 async run() {
13 // we are assuming prepare and build have been run previously
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 // add functions
42 archive.directory(this.locator.buildFunctionsResourcePath('dist'), false);
43 // add CONFIG
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 // add handlers
49 this.debug(`Generated config: ${JSON.stringify(config, null, 2)}`);
50 // ZIP
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 // log files added to zip
56 }
57 catch (e) {
58 this.error(e);
59 }
60 }
61 async retrieveAuthConfig(config) {
62 // generate config
63 const content = await fs.readFile(this.locator.authConfigPath, { encoding: 'utf8' });
64 return Object.assign({}, config, { auth: JSON.parse(content) });
65 }
66 // TODO: rewrite this using TS AST
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}
77PackFunctions.description = 'Pack integration functions';
78PackFunctions.hidden = true;
79PackFunctions.flags = Object.assign({}, base_command_1.default.flags);
80PackFunctions.args = [{ name: 'ARCHIVE_PATH', required: true }];
81tslib_1.__decorate([
82 decorators_1.RequireIntegrationFolder()
83], PackFunctions.prototype, "run", null);
84exports.default = PackFunctions;
85function 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}
95exports.buildLambdaDefinitions = buildLambdaDefinitions;