UNPKG

3.28 kBPlain TextView Raw
1/**
2 * This module must not import anything globally not working in web-mode! if needed, require it within the functions
3 */
4import { IConfigParseResult } from '../libs/config-parse-result';
5import {IPlugin, forwardChildIamRoleStatements} from '../libs/plugin';
6import { isStorage } from './storage-component'
7import { PARSER_MODES } from '../libs/parser';
8import * as deepmerge from 'deepmerge';
9
10
11/**
12 * Parameters that apply to the whole Plugin, passed by other plugins
13 */
14export interface IStoragePlugin {
15 /**
16 * one of the [[PARSER_MODES]]
17 */
18 parserMode: string,
19
20 /**
21 * path to a directory where we put the final bundles
22 */
23 buildPath: string,
24}
25
26/**
27 * A Plugin to detect Storage-Components
28 */
29export const StoragePlugin = (props: IStoragePlugin): IPlugin => {
30 const path = require('path');
31
32 const result: IPlugin = {
33 // identify Components
34 applies: (component): boolean => {
35
36 return isStorage(component);
37 },
38
39 // convert the component into configuration parts
40 // while the component is of Type `any`, its props must be of type `IWebApp`
41 process: (
42 component: any,
43 childConfigs: Array<IConfigParseResult>,
44 infrastructureMode: string | undefined
45 ): IConfigParseResult => {
46
47 const fs = require("fs");
48 const path = require ("path");
49
50 function createFolder() {
51 // start the sls-config
52 if (props.parserMode === PARSER_MODES.MODE_START) {
53
54 const targetFolder = ".s3";
55 //check if folder needs to be created or integrated
56 if ( !fs.existsSync( targetFolder) ) {
57 fs.mkdirSync( targetFolder, {recursive: true} );
58
59 }
60 fs.chmodSync( targetFolder, 0o777);
61 }
62 }
63
64
65
66 /**
67 * setup a local S3, only if we start it locally
68 */
69 const localS3Config = props.parserMode !== PARSER_MODES.MODE_START ? {} : {
70
71 plugins: ["serverless-s3-local"],
72
73 /* see: https://www.npmjs.com/package/serverless-dynamodb-local */
74 custom: {
75 "s3": {
76 port: 3002,
77 directory: path.join(
78 require("../../../infrastructure-scripts/dist/infra-comp-utils/system-libs").currentAbsolutePath(),".s3"
79 )
80 }
81 },
82
83 provider: {
84 staticBucket: "${self:provider.stackName}",
85
86 }
87 };
88
89 return {
90 slsConfigs: deepmerge.all([
91 localS3Config
92 ].concat(childConfigs.map(config => config.slsConfigs))),
93
94 // add the server config
95 webpackConfigs: childConfigs.reduce((result, config) => result.concat(config.webpackConfigs), []),
96
97 postBuilds: childConfigs.reduce((result, config) => result.concat(config.postBuilds), [createFolder]),
98
99 iamRoleStatements: forwardChildIamRoleStatements(childConfigs)
100 }
101 }
102 }
103
104 return result;
105
106};
\No newline at end of file