UNPKG

4.73 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const command_1 = require("@oclif/command");
5const globby = require("globby");
6const Listr = require("listr");
7const path = require("path");
8const webpack = require("webpack");
9const base_command_1 = require("../../base-command");
10const install_dependencies_1 = require("../../tasks/install-dependencies");
11const decorators_1 = require("../../utils/decorators");
12const api_documentation_1 = require("../generate/api-documentation");
13const function_ts_compiler_options_1 = require("../../utils/function-ts-compiler-options");
14const skipInstall = 'skip-install';
15class BuildFunctions extends base_command_1.default {
16 constructor() {
17 super(...arguments);
18 this.transpile = (entriesPath, distPath) => {
19 return new Promise(async (resolve, reject) => {
20 try {
21 const files = await globby([`${entriesPath}/*.ts`]);
22 if (!files.length) {
23 return reject(new Error('No func to transpile'));
24 }
25 console.log('ok');
26 const config = Object.assign({}, baseConfig, {
27 // optimization: {
28 // minimize: false
29 // },
30 entry: getEntries(files), output: {
31 libraryTarget: 'commonjs2',
32 filename: '[name].js',
33 path: distPath
34 },
35 // the sdk is already provided within the lamnda through a layer
36 externals: /aws\-sdk/ });
37 webpack(config, (err, stats) => {
38 this.debug(stats.toJson('verbose'));
39 if (err || stats.hasErrors()) {
40 reject(stats.toString({
41 builtAt: false,
42 entrypoints: false,
43 assets: false,
44 version: false,
45 timings: false,
46 hash: false,
47 modules: false,
48 chunks: false,
49 colors: true // Shows colors in the console
50 }));
51 }
52 else {
53 resolve(files);
54 }
55 });
56 }
57 catch (e) {
58 reject(e);
59 }
60 });
61 };
62 }
63 async run() {
64 const { flags } = this.parse(BuildFunctions);
65 const tasks = [
66 {
67 title: 'Generate functions',
68 task: async (ctx, _task) => {
69 ctx.files = await this.transpile(this.locator.srcFunctionsDir, this.locator.buildFunctionsResourcePath('dist'));
70 }
71 },
72 {
73 title: 'Generate openapi.json',
74 task: async (_ctx, _task) => {
75 await api_documentation_1.default.run([]);
76 }
77 }
78 ];
79 if (!flags[skipInstall]) {
80 tasks.unshift(install_dependencies_1.default({ cwd: this.locator.integrationRoot }));
81 }
82 try {
83 const ctx = await new Listr(tasks).run();
84 this.debug('Transpiled :\n', ctx.files.join('\n * '));
85 this.success('Built functions');
86 }
87 catch (e) {
88 this.error(e);
89 }
90 }
91}
92BuildFunctions.description = 'Build integration functions';
93BuildFunctions.aliases = ['b:f'];
94BuildFunctions.hidden = true;
95BuildFunctions.flags = Object.assign({}, base_command_1.default.flags, { [skipInstall]: command_1.flags.boolean({}) });
96BuildFunctions.args = [];
97tslib_1.__decorate([
98 decorators_1.RequireIntegrationFolder()
99], BuildFunctions.prototype, "run", null);
100exports.default = BuildFunctions;
101function getEntries(files) {
102 return files.reduceRight((entriesAcc, file) => (Object.assign({}, entriesAcc, { [path.basename(file).split('.')[0]]: file })), {});
103}
104const baseConfig = {
105 mode: 'production',
106 module: {
107 rules: [
108 {
109 test: /\.tsx?$/,
110 loader: 'ts-loader',
111 exclude: /node_modules/,
112 options: {
113 compilerOptions: function_ts_compiler_options_1.default
114 }
115 }
116 ]
117 },
118 resolve: {
119 extensions: ['.tsx', '.ts', '.js', '.json']
120 },
121 target: 'node'
122};