UNPKG

4.49 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 const config = Object.assign({}, baseConfig, {
26 // optimization: {
27 // minimize: false
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 // Shows colors in the console
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}
88BuildFunctions.description = 'Build integration functions';
89BuildFunctions.aliases = ['b:f'];
90BuildFunctions.hidden = true;
91BuildFunctions.flags = Object.assign({}, base_command_1.default.flags, { [skipInstall]: command_1.flags.boolean({}) });
92BuildFunctions.args = [];
93tslib_1.__decorate([
94 decorators_1.RequireIntegrationFolder()
95], BuildFunctions.prototype, "run", null);
96exports.default = BuildFunctions;
97function getEntries(files) {
98 return files.reduceRight((entriesAcc, file) => (Object.assign({}, entriesAcc, { [path.basename(file).split('.')[0]]: file })), {});
99}
100const baseConfig = {
101 mode: 'production',
102 module: {
103 rules: [
104 {
105 test: /\.tsx?$/,
106 loader: 'ts-loader',
107 exclude: /node_modules/,
108 options: {
109 compilerOptions: function_ts_compiler_options_1.default
110 }
111 }
112 ]
113 },
114 resolve: {
115 extensions: ['.tsx', '.ts', '.js', '.json']
116 },
117 target: 'node'
118};