1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const command_1 = require("@oclif/command");
|
5 | const globby = require("globby");
|
6 | const Listr = require("listr");
|
7 | const path = require("path");
|
8 | const webpack = require("webpack");
|
9 | const base_command_1 = require("../../base-command");
|
10 | const install_dependencies_1 = require("../../tasks/install-dependencies");
|
11 | const decorators_1 = require("../../utils/decorators");
|
12 | const api_documentation_1 = require("../generate/api-documentation");
|
13 | const function_ts_compiler_options_1 = require("../../utils/function-ts-compiler-options");
|
14 | const skipInstall = 'skip-install';
|
15 | class 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 |
|
28 |
|
29 |
|
30 | entry: getEntries(files), output: {
|
31 | libraryTarget: 'commonjs2',
|
32 | filename: '[name].js',
|
33 | path: distPath
|
34 | },
|
35 |
|
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
|
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 | }
|
92 | BuildFunctions.description = 'Build integration functions';
|
93 | BuildFunctions.aliases = ['b:f'];
|
94 | BuildFunctions.hidden = true;
|
95 | BuildFunctions.flags = Object.assign({}, base_command_1.default.flags, { [skipInstall]: command_1.flags.boolean({}) });
|
96 | BuildFunctions.args = [];
|
97 | tslib_1.__decorate([
|
98 | decorators_1.RequireIntegrationFolder()
|
99 | ], BuildFunctions.prototype, "run", null);
|
100 | exports.default = BuildFunctions;
|
101 | function getEntries(files) {
|
102 | return files.reduceRight((entriesAcc, file) => (Object.assign({}, entriesAcc, { [path.basename(file).split('.')[0]]: file })), {});
|
103 | }
|
104 | const 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 | };
|