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 | const config = Object.assign({}, baseConfig, {
|
26 |
|
27 |
|
28 |
|
29 | entry: getEntries(files), output: {
|
30 | libraryTarget: 'commonjs2',
|
31 | filename: '[name].js',
|
32 | path: distPath
|
33 | } });
|
34 | webpack(config, (err, stats) => {
|
35 | if (err || stats.hasErrors()) {
|
36 | reject(stats.toString({
|
37 | builtAt: false,
|
38 | entrypoints: false,
|
39 | assets: false,
|
40 | version: false,
|
41 | timings: false,
|
42 | hash: false,
|
43 | modules: false,
|
44 | chunks: false,
|
45 | colors: true
|
46 | }));
|
47 | }
|
48 | else {
|
49 | resolve(files);
|
50 | }
|
51 | });
|
52 | }
|
53 | catch (e) {
|
54 | reject(e);
|
55 | }
|
56 | });
|
57 | };
|
58 | }
|
59 | async run() {
|
60 | const { flags } = this.parse(BuildFunctions);
|
61 | const tasks = [
|
62 | {
|
63 | title: 'Generate functions',
|
64 | task: async (ctx, _task) => {
|
65 | ctx.files = await this.transpile(this.locator.srcFunctionsDir, this.locator.buildFunctionsResourcePath('dist'));
|
66 | }
|
67 | },
|
68 | {
|
69 | title: 'Generate openapi.json',
|
70 | task: async (_ctx, _task) => {
|
71 | await api_documentation_1.default.run([]);
|
72 | }
|
73 | }
|
74 | ];
|
75 | if (!flags[skipInstall]) {
|
76 | tasks.unshift(install_dependencies_1.default({ cwd: this.locator.integrationRoot }));
|
77 | }
|
78 | try {
|
79 | const ctx = await new Listr(tasks).run();
|
80 | this.debug('Transpiled :\n', ctx.files.join('\n * '));
|
81 | this.success('Built functions');
|
82 | }
|
83 | catch (e) {
|
84 | this.error(e);
|
85 | }
|
86 | }
|
87 | }
|
88 | BuildFunctions.description = 'Build integration functions';
|
89 |
|
90 | BuildFunctions.aliases = ['build:intents', 'b:i'];
|
91 | BuildFunctions.hidden = true;
|
92 | BuildFunctions.flags = Object.assign({}, base_command_1.default.flags, { [skipInstall]: command_1.flags.boolean({}) });
|
93 | BuildFunctions.args = [];
|
94 | tslib_1.__decorate([
|
95 | decorators_1.RequireIntegrationFolder()
|
96 | ], BuildFunctions.prototype, "run", null);
|
97 | exports.default = BuildFunctions;
|
98 | function getEntries(files) {
|
99 | return files.reduceRight((entriesAcc, file) => (Object.assign({}, entriesAcc, { [path.basename(file).split('.')[0]]: file })), {});
|
100 | }
|
101 | const baseConfig = {
|
102 | mode: 'production',
|
103 | module: {
|
104 | rules: [
|
105 | {
|
106 | test: /\.tsx?$/,
|
107 | loader: 'ts-loader',
|
108 | exclude: /node_modules/,
|
109 | options: {
|
110 | compilerOptions: function_ts_compiler_options_1.default
|
111 | }
|
112 | }
|
113 | ]
|
114 | },
|
115 | resolve: {
|
116 | extensions: ['.tsx', '.ts', '.js', '.json']
|
117 | },
|
118 | target: 'node'
|
119 | };
|