@friggframework/serverless-plugin 2.0.0--canary.365.52ef5c7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/LICENSE.md +9 -0
  2. package/index.js +131 -0
  3. package/package.json +15 -0
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Left Hook Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/index.js ADDED
@@ -0,0 +1,131 @@
1
+ const { spawn } = require("child_process");
2
+
3
+ class FriggServerlessPlugin {
4
+ constructor(serverless, options) {
5
+ this.serverless = serverless;
6
+ this.options = options;
7
+ this.provider = serverless.getProvider("aws");
8
+ this.hooks = {
9
+ initialize: () => this.init(),
10
+ "after:package:package": () => this.afterPackage(),
11
+ "before:deploy:deploy": () => this.beforeDeploy(),
12
+ };
13
+ }
14
+ async asyncInit() {
15
+ this.serverless.cli.log("Initializing Frigg Serverless Plugin...");
16
+ console.log("Hello from Frigg Serverless Plugin!");
17
+ if (this.serverless.processedInput.commands.includes("offline")) {
18
+ console.log("Running in offline mode. Making queues!");
19
+ const queues = Object.keys(this.serverless.service.custom)
20
+ .filter((key) => key.endsWith("Queue"))
21
+ .map((key) => {
22
+ return {
23
+ key,
24
+ name: this.serverless.service.custom[key],
25
+ };
26
+ });
27
+ console.log("Queues to be created:", queues);
28
+
29
+ const AWS = require("aws-sdk");
30
+
31
+ const endpointUrl = "localhost:4566"; // Assuming localstack is running on port 4
32
+ const region = "us-east-1";
33
+
34
+ // Configure AWS SDK
35
+ AWS.config.update({
36
+ region: region,
37
+ endpoint: endpointUrl,
38
+ });
39
+
40
+ const sqs = new AWS.SQS();
41
+ // Find the environment variables that we need to override and create an easy map
42
+ const environmentMap = {};
43
+ const environment = this.serverless.service.provider.environment;
44
+
45
+ for (const [key, value] of Object.entries(environment)) {
46
+ if (typeof value === "object" && value.Ref) {
47
+ environmentMap[value.Ref] = key;
48
+ }
49
+ }
50
+
51
+ const queueCreationPromises = queues.map((queue) => {
52
+ return new Promise((resolve, reject) => {
53
+ const params = {
54
+ QueueName: queue.name,
55
+ };
56
+
57
+ sqs.createQueue(params, (err, data) => {
58
+ if (err) {
59
+ console.error(
60
+ `Error creating queue ${queue.name}: ${err.message}`
61
+ );
62
+ reject(err);
63
+ } else {
64
+ const queueUrl = data.QueueUrl;
65
+ console.log(
66
+ `Queue ${queue.name} created successfully. URL: ${queueUrl}`
67
+ );
68
+
69
+ const environmentKey = environmentMap[queue.key];
70
+ this.serverless.extendConfiguration(
71
+ ["provider", "environment", environmentKey],
72
+ queueUrl
73
+ );
74
+ console.log(`Set ${environmentKey} to ${queueUrl}`);
75
+ resolve(queueUrl);
76
+ }
77
+ });
78
+ });
79
+ });
80
+
81
+ await Promise.all(queueCreationPromises);
82
+ } else {
83
+ console.log("Running in online mode, doing nothing");
84
+ }
85
+ }
86
+ init() {}
87
+ afterPackage() {
88
+ console.log("After package hook called");
89
+ // // const queues = Object.keys(infrastructure.custom)
90
+ // // .filter((key) => key.endsWith('Queue'))
91
+ // // .map((key) => infrastructure.custom[key]);
92
+ // // console.log('Queues to be created:', queues);
93
+ // //
94
+ // // const endpointUrl = 'http://localhost:4566'; // Assuming localstack is running on port 4
95
+ // // const region = 'us-east-1';
96
+ // // const command = 'aws';
97
+ // // queues.forEach((queue) => {
98
+ // // const args = [
99
+ // // '--endpoint-url',
100
+ // // endpointUrl,
101
+ // // 'sqs',
102
+ // // 'create-queue',
103
+ // // '--queue-name',
104
+ // // queue,
105
+ // // '--region',
106
+ // // region,
107
+ // // '--output',
108
+ // // 'table',
109
+ // // ];
110
+ // //
111
+ // // const childProcess = spawn(command, args, {
112
+ // // cwd: backendPath,
113
+ // // stdio: 'inherit',
114
+ // // });
115
+ // // childProcess.on('error', (error) => {
116
+ // // console.error(`Error executing command: ${error.message}`);
117
+ // // });
118
+ // //
119
+ // // childProcess.on('close', (code) => {
120
+ // // if (code !== 0) {
121
+ // // console.log(`Child process exited with code ${code}`);
122
+ // // }
123
+ // // });
124
+ // });
125
+ }
126
+ beforeDeploy() {
127
+ console.log("Before deploy hook called");
128
+ }
129
+ }
130
+
131
+ module.exports = FriggServerlessPlugin;
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@friggframework/serverless-plugin",
3
+ "version": "2.0.0--canary.365.52ef5c7.0",
4
+ "description": "Plugin to help dynamically load frigg resources",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "jest"
8
+ },
9
+ "author": "Left Hook, Inc.",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "gitHead": "52ef5c743e364f4ad38df48519cc69b6c262ccfd"
15
+ }