1 | const jsYml = require('js-yaml');
|
2 | const fs = require('fs');
|
3 | const path = require('path');
|
4 | const merge = require('lodash.merge')
|
5 |
|
6 | class AddIntegrations {
|
7 | invoke(options, content, serverless) {
|
8 | try {
|
9 |
|
10 | if (!options.integrationPath) {
|
11 | return content;
|
12 | }
|
13 |
|
14 | if (fs.lstatSync(options.integrationPath).isFile()) {
|
15 | let integrationFile = jsYml.load(fs.readFileSync(options.integrationPath, 'utf8'));
|
16 | serverless.cli.log(
|
17 | `Merge x-amazon-apigateway-integration integration: ${options.integrationPath}`,
|
18 | 'OpenApi Integration Plugin'
|
19 | );
|
20 | return merge(content, integrationFile)
|
21 | }
|
22 |
|
23 | if (fs.lstatSync(options.integrationPath).isDirectory()) {
|
24 | const filteredFileNames = this.getFilesFromDirectory(options.integrationPath);
|
25 | filteredFileNames.forEach(function (file, index) {
|
26 | const fullFilePath = [options.integrationPath, file].join('/');
|
27 | let integrationFile = jsYml.load(fs.readFileSync(fullFilePath, 'utf8'));
|
28 | content = merge(content, integrationFile)
|
29 | serverless.cli.log(
|
30 | `Merge x-amazon-apigateway-integration integration: ${fullFilePath}`,
|
31 | 'OpenApi Integration Plugin'
|
32 | );
|
33 | });
|
34 |
|
35 | return content
|
36 | }
|
37 | } catch (e) {
|
38 | console.log(e);
|
39 | }
|
40 | }
|
41 |
|
42 | getFilesFromDirectory(directory) {
|
43 | const filenames = fs.readdirSync(directory);
|
44 | return filenames.filter(function (file) {
|
45 | return path.extname(file).toLowerCase() === '.yml' || path.extname(file).toLowerCase() === '.yaml';
|
46 | });
|
47 | }
|
48 | }
|
49 |
|
50 | module.exports = AddIntegrations
|