Skip to content

Commit a86f017

Browse files
authored
feat: introduce config file functionality (#15) (#38)
* feat: introduce config file functionality (#15) With this change we are starting to allow .twilio-functions for more than just storing some build outcomes. It supports grouping configs by environment, command or project or nested combinations of these. Very extended config: ```json { "startConfig": { "ngrok": "dom", "inspect": "" }, "environments": { "dev": { "deployConfig": { "env": ".env" } }, "prod": { "deployConfig": { "env": ".env.prod" } } }, "projects": { "ACc2bdaa19578061b45a518axxxxxxxxxx": { "serviceSid": "ZSd6e3037c5710c8e8c979f5xxxxxxxxxx", "latestBuild": "ZBdf824389570e3ee2ebcec7xxxxxxxxxx", "deployConfig": { "environment": "prod" } } }, "serviceSid": "ZSd6e3037c5710c8e8c979fxxxxxxxxxx", "latestBuild": "ZBdf824389570e3ee2ebcec7xxxxxxxxxx" } ``` fix: #15 * feat: add config support for list,activate,deploy (#15) This commit adds config file support for the list, activate and deploy command. It also deprecates the --functions-env flag BREAKING CHANGE: Deprecating --functions-env as an option fix: #15, fix: #27 * refactor(config): factor out common code for testability * fix(config): write serviceSid to config This creates compatibility of config files for twilio-run * test(config): add basic test for list config * feat(runtime): handle invalid account sid & new error page This change will check for valid Account SIDs in local development mode and create an appropriate error message. It also introduces a new Error response page BREAKING CHANGE: Error page layout changed fix #45 * chore(release): 2.0.0-beta.13 * fix: update code for new version of severless-api (#46) * fix: update code for new version of severless-api re twilio-labs/serverless-api#8 * chore(npm): upgrade @twilio-labs/serverless-api * fix(tests): fix integration tests Remove the reliance on stacktraces in the error integration tests * chore(release): 2.0.0-rc.0 * chore(npm): upgrade serverless-api library * chore(release): 2.0.0-rc.1 * feat(new): change from prompts to inquirer (#36) Prompts has a bug for screens that are smaller than the content. This fix hasn't been launched yet so we are switching to inquirer. fixes #36 * fix(templates): switch template list endpoint to next branch The templates are currently pulled from the next branch but the list was from master, causing some templates to crash * feat: introduce config file functionality (#15) With this change we are starting to allow .twilio-functions for more than just storing some build outcomes. It supprts grouping configs by environment, command or project or nested combinations of these fix: #15 * feat: add config support for list,activate,deploy (#15) This commit adds config file support for the list, activate and deploy command. It also deprecates the --functions-env flag BREAKING CHANGE: Deprecating --functions-env as an option fix: #15, fix: #27 * refactor(config): factor out common code for testability * fix(config): write serviceSid to config This creates compatibility of config files for twilio-run * test(config): add basic test for list config * feat(config): accept external config options * test(config): add more tests for configs * fix(config): store under accountSid not API key
1 parent 5a5030e commit a86f017

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1397
-520
lines changed
File renamed without changes.

__mocks__/window-size.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
width: 100,
3+
height: 200,
4+
};

__tests__/config/list.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getConfigFromFlags, ListCliFlags } from '../../src/config/list';
2+
3+
jest.mock('../../src/config/global');
4+
jest.mock('../../src/config/utils/mergeFlagsAndConfig');
5+
jest.mock('../../src/config/utils');
6+
7+
const baseFlags = {
8+
types: 'services',
9+
extendedOutput: false,
10+
environment: 'dev',
11+
} as ListCliFlags;
12+
13+
describe('getConfigFromFlags', () => {
14+
test('should return a config', async () => {
15+
const flags = { ...baseFlags };
16+
const config = await getConfigFromFlags(flags);
17+
expect(config).toBeDefined();
18+
});
19+
20+
describe('handle types', () => {
21+
test('should split types into an array', async () => {
22+
const flags = { ...baseFlags };
23+
const config = await getConfigFromFlags(flags);
24+
expect(config.types).toEqual(['services']);
25+
});
26+
27+
test('should split types by comma and trim', async () => {
28+
const flags = { ...baseFlags, types: 'environments, functions' };
29+
const config = await getConfigFromFlags(flags);
30+
expect(config.types).toEqual(['environments', 'functions']);
31+
});
32+
});
33+
34+
test('should call the right functions', async () => {
35+
const globalMod = require('../../src/config/global');
36+
const utilMod = require('../../src/config/utils');
37+
const config = await getConfigFromFlags({ ...baseFlags });
38+
expect(globalMod.readSpecializedConfig).toHaveBeenCalled();
39+
expect(utilMod.getServiceNameFromFlags).toHaveBeenCalled();
40+
expect(utilMod.getCredentialsFromFlags).toHaveBeenCalled();
41+
});
42+
});

__tests__/runtime/cli/config.test.ts renamed to __tests__/config/start.test.ts

+53-90
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import {
33
getInspectInfo,
44
getPort,
55
getUrl,
6-
WrappedStartCliFlags,
7-
} from '../../../src/runtime/cli/config';
6+
StartCliConfig,
7+
StartCliFlags,
8+
} from '../../src/config/start';
89

910
jest.mock('ngrok', () => {
1011
return {
@@ -20,32 +21,26 @@ jest.mock('ngrok', () => {
2021
describe('getUrl', () => {
2122
test('returns localhost if ngrok is not passed', async () => {
2223
const config = ({
23-
flags: {
24-
ngrok: undefined,
25-
},
26-
} as unknown) as WrappedStartCliFlags;
24+
ngrok: undefined,
25+
} as unknown) as StartCliFlags;
2726

2827
const url = await getUrl(config, 3000);
2928
expect(url).toBe('http://localhost:3000');
3029
});
3130

3231
test('calls ngrok if ngrok is defined', async () => {
3332
const config = ({
34-
flags: {
35-
ngrok: '',
36-
},
37-
} as unknown) as WrappedStartCliFlags;
33+
ngrok: '',
34+
} as unknown) as StartCliFlags;
3835

3936
const url = await getUrl(config, 3000);
4037
expect(url).toBe('https://random.ngrok.io');
4138
});
4239

4340
test('calls ngrok with custom subdomain if passed', async () => {
4441
const config = ({
45-
flags: {
46-
ngrok: 'dom',
47-
},
48-
} as unknown) as WrappedStartCliFlags;
42+
ngrok: 'dom',
43+
} as unknown) as StartCliFlags;
4944

5045
const url = await getUrl(config, 3000);
5146
expect(url).toBe('https://dom.ngrok.io');
@@ -69,10 +64,8 @@ describe('getPort', () => {
6964

7065
test('returns default 3000 if nothing is passed', () => {
7166
const config = ({
72-
flags: {
73-
port: undefined,
74-
},
75-
} as unknown) as WrappedStartCliFlags;
67+
port: undefined,
68+
} as unknown) as StartCliFlags;
7669

7770
delete process.env.PORT;
7871
const port = getPort(config);
@@ -81,10 +74,8 @@ describe('getPort', () => {
8174

8275
test('checks for process.env.PORT and returns number', () => {
8376
const config = ({
84-
flags: {
85-
port: undefined,
86-
},
87-
} as unknown) as WrappedStartCliFlags;
77+
port: undefined,
78+
} as unknown) as StartCliFlags;
8879

8980
process.env.PORT = '9999';
9081
const port = getPort(config);
@@ -94,10 +85,8 @@ describe('getPort', () => {
9485

9586
test('port passed via flag takes preference', () => {
9687
const config = ({
97-
flags: {
98-
port: 1234,
99-
},
100-
} as unknown) as WrappedStartCliFlags;
88+
port: 1234,
89+
} as unknown) as StartCliFlags;
10190

10291
process.env.PORT = '9999';
10392
const port = getPort(config);
@@ -107,10 +96,8 @@ describe('getPort', () => {
10796

10897
test('handles strings and returns number', () => {
10998
const config = ({
110-
flags: {
111-
port: '8080',
112-
},
113-
} as unknown) as WrappedStartCliFlags;
99+
port: '8080',
100+
} as unknown) as StartCliFlags;
114101

115102
process.env.PORT = '9999';
116103
const port = getPort(config);
@@ -138,79 +125,65 @@ describe('getBaseDirectory', () => {
138125

139126
test('handles current working directory if none is passed', () => {
140127
const config = ({
141-
flags: {
142-
dir: undefined,
143-
cwd: undefined,
144-
},
145-
} as unknown) as WrappedStartCliFlags;
128+
dir: undefined,
129+
cwd: undefined,
130+
} as unknown) as StartCliFlags;
146131

147132
const result = getBaseDirectory(config);
148133
expect(result).toBe('/home');
149134
});
150135

151136
test('supports dir argument', () => {
152137
const config = ({
153-
flags: {
154-
dir: '/usr/local',
155-
cwd: undefined,
156-
},
157-
} as unknown) as WrappedStartCliFlags;
138+
dir: '/usr/local',
139+
cwd: undefined,
140+
} as unknown) as StartCliFlags;
158141

159142
const result = getBaseDirectory(config);
160143
expect(result).toBe('/usr/local');
161144
});
162145

163146
test('prefers cwd over dir argument', () => {
164147
const config = ({
165-
flags: {
166-
dir: '/usr/local',
167-
cwd: '/usr/bin',
168-
},
169-
} as unknown) as WrappedStartCliFlags;
148+
dir: '/usr/local',
149+
cwd: '/usr/bin',
150+
} as unknown) as StartCliFlags;
170151

171152
const result = getBaseDirectory(config);
172153
expect(result).toBe('/usr/bin');
173154
});
174155

175156
test('handles relative path for dir', () => {
176157
let config = ({
177-
flags: {
178-
dir: 'demo',
179-
cwd: undefined,
180-
},
181-
} as unknown) as WrappedStartCliFlags;
158+
dir: 'demo',
159+
cwd: undefined,
160+
} as unknown) as StartCliFlags;
182161

183162
let result = getBaseDirectory(config);
184163
expect(result).toBe('/home/demo');
185164

186165
config = ({
187-
flags: {
188-
dir: '../demo',
189-
cwd: undefined,
190-
},
191-
} as unknown) as WrappedStartCliFlags;
166+
dir: '../demo',
167+
cwd: undefined,
168+
} as unknown) as StartCliFlags;
192169

193170
result = getBaseDirectory(config);
194171
expect(result).toBe('/demo');
195172
});
196173

197174
test('handles relative path for cwd', () => {
198175
let config = ({
199-
flags: {
200-
dir: undefined,
201-
cwd: 'demo',
202-
},
203-
} as unknown) as WrappedStartCliFlags;
176+
dir: undefined,
177+
cwd: 'demo',
178+
} as unknown) as StartCliFlags;
204179

205180
let result = getBaseDirectory(config);
206181
expect(result).toBe('/home/demo');
207182

208183
config = ({
209-
flags: {
210-
dir: undefined,
211-
cwd: '../demo',
212-
},
213-
} as unknown) as WrappedStartCliFlags;
184+
dir: undefined,
185+
cwd: '../demo',
186+
} as unknown) as StartCliFlags;
214187

215188
result = getBaseDirectory(config);
216189
expect(result).toBe('/demo');
@@ -220,59 +193,49 @@ describe('getBaseDirectory', () => {
220193
describe('getInspectInfo', () => {
221194
test('returns undefined if nothing is passed', () => {
222195
const config = ({
223-
flags: {
224-
inspect: undefined,
225-
inspectBrk: undefined,
226-
},
227-
} as unknown) as WrappedStartCliFlags;
196+
inspect: undefined,
197+
inspectBrk: undefined,
198+
} as unknown) as StartCliFlags;
228199

229200
const result = getInspectInfo(config);
230201
expect(result).toBeUndefined();
231202
});
232203

233204
test('handles present but empty inspect flag', () => {
234205
const config = ({
235-
flags: {
236-
inspect: '',
237-
inspectBrk: undefined,
238-
},
239-
} as unknown) as WrappedStartCliFlags;
206+
inspect: '',
207+
inspectBrk: undefined,
208+
} as unknown) as StartCliFlags;
240209

241210
const result = getInspectInfo(config);
242211
expect(result).toEqual({ hostPort: '', break: false });
243212
});
244213

245214
test('handles present but empty inspectBrk flag', () => {
246215
const config = ({
247-
flags: {
248-
inspect: undefined,
249-
inspectBrk: '',
250-
},
251-
} as unknown) as WrappedStartCliFlags;
216+
inspect: undefined,
217+
inspectBrk: '',
218+
} as unknown) as StartCliFlags;
252219

253220
const result = getInspectInfo(config);
254221
expect(result).toEqual({ hostPort: '', break: true });
255222
});
256223

257224
test('handles passed port in inspect flag', () => {
258225
const config = ({
259-
flags: {
260-
inspect: '9999',
261-
inspectBrk: undefined,
262-
},
263-
} as unknown) as WrappedStartCliFlags;
226+
inspect: '9999',
227+
inspectBrk: undefined,
228+
} as unknown) as StartCliFlags;
264229

265230
const result = getInspectInfo(config);
266231
expect(result).toEqual({ hostPort: '9999', break: false });
267232
});
268233

269234
test('handles passed port in inspect flag', () => {
270235
const config = ({
271-
flags: {
272-
inspect: undefined,
273-
inspectBrk: '1234',
274-
},
275-
} as unknown) as WrappedStartCliFlags;
236+
inspect: undefined,
237+
inspectBrk: '1234',
238+
} as unknown) as StartCliFlags;
276239

277240
const result = getInspectInfo(config);
278241
expect(result).toEqual({ hostPort: '1234', break: true });

__tests__/config/utils.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('getServiceNameFromFlags', () => {
2+
test('TODO', () => {
3+
expect(true).toBeTruthy();
4+
});
5+
});

0 commit comments

Comments
 (0)