@friggframework/serverless-plugin 2.0.0-next.27 → 2.0.0-next.29
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.
- package/index.js +38 -2
- package/index.test.js +183 -0
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,16 +1,30 @@
|
|
|
1
1
|
const { spawn } = require("child_process");
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Frigg Serverless Plugin - Handles AWS discovery and local development setup
|
|
5
|
+
*/
|
|
3
6
|
class FriggServerlessPlugin {
|
|
7
|
+
/**
|
|
8
|
+
* Creates an instance of FriggServerlessPlugin
|
|
9
|
+
* @param {Object} serverless - Serverless framework instance
|
|
10
|
+
* @param {Object} options - Plugin options
|
|
11
|
+
*/
|
|
4
12
|
constructor(serverless, options) {
|
|
5
13
|
this.serverless = serverless;
|
|
6
14
|
this.options = options;
|
|
7
15
|
this.provider = serverless.getProvider("aws");
|
|
8
16
|
this.hooks = {
|
|
9
17
|
initialize: () => this.init(),
|
|
18
|
+
"before:package:initialize": () => this.beforePackageInitialize(),
|
|
10
19
|
"after:package:package": () => this.afterPackage(),
|
|
11
20
|
"before:deploy:deploy": () => this.beforeDeploy(),
|
|
12
21
|
};
|
|
13
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Asynchronous initialization for offline mode
|
|
25
|
+
* Creates SQS queues in localstack for local development
|
|
26
|
+
* @returns {Promise<void>}
|
|
27
|
+
*/
|
|
14
28
|
async asyncInit() {
|
|
15
29
|
this.serverless.cli.log("Initializing Frigg Serverless Plugin...");
|
|
16
30
|
console.log("Hello from Frigg Serverless Plugin!");
|
|
@@ -38,7 +52,7 @@ class FriggServerlessPlugin {
|
|
|
38
52
|
});
|
|
39
53
|
|
|
40
54
|
const sqs = new AWS.SQS();
|
|
41
|
-
// Find the environment variables that we need to override and create an easy map
|
|
55
|
+
// Find the environment variables that we need to override and create an easy map
|
|
42
56
|
const environmentMap = {};
|
|
43
57
|
const environment = this.serverless.service.provider.environment;
|
|
44
58
|
|
|
@@ -83,7 +97,26 @@ class FriggServerlessPlugin {
|
|
|
83
97
|
console.log("Running in online mode, doing nothing");
|
|
84
98
|
}
|
|
85
99
|
}
|
|
86
|
-
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Hook that runs before serverless package initialization
|
|
103
|
+
* AWS discovery is now handled in serverless-template.js
|
|
104
|
+
* @returns {Promise<void>}
|
|
105
|
+
*/
|
|
106
|
+
async beforePackageInitialize() {
|
|
107
|
+
// AWS discovery is now handled directly in serverless-template.js
|
|
108
|
+
// This hook remains for potential future use or other pre-package tasks
|
|
109
|
+
this.serverless.cli.log("Frigg Serverless Plugin: Pre-package hook");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Initialization hook (currently empty)
|
|
115
|
+
*/
|
|
116
|
+
init() { }
|
|
117
|
+
/**
|
|
118
|
+
* Hook that runs after serverless package
|
|
119
|
+
*/
|
|
87
120
|
afterPackage() {
|
|
88
121
|
console.log("After package hook called");
|
|
89
122
|
// // const queues = Object.keys(infrastructure.custom)
|
|
@@ -123,6 +156,9 @@ class FriggServerlessPlugin {
|
|
|
123
156
|
// // });
|
|
124
157
|
// });
|
|
125
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Hook that runs before serverless deploy
|
|
161
|
+
*/
|
|
126
162
|
beforeDeploy() {
|
|
127
163
|
console.log("Before deploy hook called");
|
|
128
164
|
}
|
package/index.test.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
const FriggServerlessPlugin = require('./index');
|
|
2
|
+
|
|
3
|
+
// Mock dependencies
|
|
4
|
+
jest.mock('aws-sdk');
|
|
5
|
+
|
|
6
|
+
describe('FriggServerlessPlugin', () => {
|
|
7
|
+
let plugin;
|
|
8
|
+
let mockServerless;
|
|
9
|
+
let mockOptions;
|
|
10
|
+
const originalEnv = process.env;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
|
|
14
|
+
// Mock serverless object
|
|
15
|
+
mockServerless = {
|
|
16
|
+
cli: {
|
|
17
|
+
log: jest.fn()
|
|
18
|
+
},
|
|
19
|
+
service: {
|
|
20
|
+
provider: {
|
|
21
|
+
name: 'aws',
|
|
22
|
+
region: 'us-east-1'
|
|
23
|
+
},
|
|
24
|
+
plugins: [],
|
|
25
|
+
custom: {},
|
|
26
|
+
functions: {}
|
|
27
|
+
},
|
|
28
|
+
processedInput: {
|
|
29
|
+
commands: []
|
|
30
|
+
},
|
|
31
|
+
getProvider: jest.fn(() => ({})),
|
|
32
|
+
extendConfiguration: jest.fn()
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
mockOptions = {
|
|
36
|
+
stage: 'test'
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Reset environment
|
|
40
|
+
process.env = { ...originalEnv };
|
|
41
|
+
|
|
42
|
+
jest.clearAllMocks();
|
|
43
|
+
|
|
44
|
+
plugin = new FriggServerlessPlugin(mockServerless, mockOptions);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
process.env = originalEnv;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('constructor', () => {
|
|
52
|
+
it('should initialize plugin with correct hooks', () => {
|
|
53
|
+
expect(plugin.serverless).toBe(mockServerless);
|
|
54
|
+
expect(plugin.options).toBe(mockOptions);
|
|
55
|
+
expect(plugin.hooks).toEqual({
|
|
56
|
+
initialize: expect.any(Function),
|
|
57
|
+
'before:package:initialize': expect.any(Function),
|
|
58
|
+
'after:package:package': expect.any(Function),
|
|
59
|
+
'before:deploy:deploy': expect.any(Function)
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should get AWS provider from serverless', () => {
|
|
64
|
+
expect(mockServerless.getProvider).toHaveBeenCalledWith('aws');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('beforePackageInitialize', () => {
|
|
69
|
+
it('should log pre-package hook message', async () => {
|
|
70
|
+
await plugin.beforePackageInitialize();
|
|
71
|
+
|
|
72
|
+
expect(mockServerless.cli.log).toHaveBeenCalledWith('Frigg Serverless Plugin: Pre-package hook');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
describe('asyncInit (offline mode)', () => {
|
|
78
|
+
beforeEach(() => {
|
|
79
|
+
// Mock AWS SDK
|
|
80
|
+
const mockSQS = {
|
|
81
|
+
createQueue: jest.fn()
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
jest.doMock('aws-sdk', () => ({
|
|
85
|
+
SQS: jest.fn(() => mockSQS),
|
|
86
|
+
config: {
|
|
87
|
+
update: jest.fn()
|
|
88
|
+
}
|
|
89
|
+
}));
|
|
90
|
+
|
|
91
|
+
mockServerless.service.custom = {
|
|
92
|
+
TestQueue: 'test-queue-name',
|
|
93
|
+
AnotherQueue: 'another-queue-name'
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
mockServerless.service.provider.environment = {
|
|
97
|
+
TEST_QUEUE_URL: { Ref: 'TestQueue' },
|
|
98
|
+
ANOTHER_QUEUE_URL: { Ref: 'AnotherQueue' },
|
|
99
|
+
NORMAL_VAR: 'normal-value'
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should create queues in offline mode', async () => {
|
|
104
|
+
mockServerless.processedInput.commands = ['offline'];
|
|
105
|
+
|
|
106
|
+
const AWS = require('aws-sdk');
|
|
107
|
+
const mockSQS = new AWS.SQS();
|
|
108
|
+
mockSQS.createQueue.mockImplementation((params, callback) => {
|
|
109
|
+
callback(null, { QueueUrl: `http://localhost:4566/000000000000/${params.QueueName}` });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
await plugin.asyncInit();
|
|
113
|
+
|
|
114
|
+
expect(AWS.config.update).toHaveBeenCalledWith({
|
|
115
|
+
region: 'us-east-1',
|
|
116
|
+
endpoint: 'localhost:4566'
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(mockSQS.createQueue).toHaveBeenCalledWith(
|
|
120
|
+
{ QueueName: 'test-queue-name' },
|
|
121
|
+
expect.any(Function)
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(mockSQS.createQueue).toHaveBeenCalledWith(
|
|
125
|
+
{ QueueName: 'another-queue-name' },
|
|
126
|
+
expect.any(Function)
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
expect(mockServerless.extendConfiguration).toHaveBeenCalled();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should skip queue creation in online mode', async () => {
|
|
133
|
+
mockServerless.processedInput.commands = ['deploy'];
|
|
134
|
+
|
|
135
|
+
await plugin.asyncInit();
|
|
136
|
+
|
|
137
|
+
const AWS = require('aws-sdk');
|
|
138
|
+
expect(AWS.config.update).not.toHaveBeenCalled();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should handle queue creation errors', async () => {
|
|
142
|
+
mockServerless.processedInput.commands = ['offline'];
|
|
143
|
+
|
|
144
|
+
const AWS = require('aws-sdk');
|
|
145
|
+
const mockSQS = new AWS.SQS();
|
|
146
|
+
mockSQS.createQueue.mockImplementation((params, callback) => {
|
|
147
|
+
callback(new Error('Queue creation failed'));
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
await expect(plugin.asyncInit()).rejects.toThrow('Queue creation failed');
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('hook methods', () => {
|
|
155
|
+
it('should have init method', () => {
|
|
156
|
+
expect(plugin.init).toBeDefined();
|
|
157
|
+
expect(typeof plugin.init).toBe('function');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should have afterPackage method', () => {
|
|
161
|
+
expect(plugin.afterPackage).toBeDefined();
|
|
162
|
+
expect(typeof plugin.afterPackage).toBe('function');
|
|
163
|
+
|
|
164
|
+
// Test that it logs correctly
|
|
165
|
+
const consoleSpy = jest.spyOn(console, 'log');
|
|
166
|
+
plugin.afterPackage();
|
|
167
|
+
expect(consoleSpy).toHaveBeenCalledWith('After package hook called');
|
|
168
|
+
consoleSpy.mockRestore();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('should have beforeDeploy method', () => {
|
|
172
|
+
expect(plugin.beforeDeploy).toBeDefined();
|
|
173
|
+
expect(typeof plugin.beforeDeploy).toBe('function');
|
|
174
|
+
|
|
175
|
+
// Test that it logs correctly
|
|
176
|
+
const consoleSpy = jest.spyOn(console, 'log');
|
|
177
|
+
plugin.beforeDeploy();
|
|
178
|
+
expect(consoleSpy).toHaveBeenCalledWith('Before deploy hook called');
|
|
179
|
+
consoleSpy.mockRestore();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/serverless-plugin",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.29",
|
|
4
4
|
"description": "Plugin to help dynamically load frigg resources",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -11,5 +11,5 @@
|
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|
|
14
|
-
"gitHead": "
|
|
14
|
+
"gitHead": "c6395f376547bd7e969797a2f92f380ba1ada0d5"
|
|
15
15
|
}
|