Skip to content

Commit b8a14e1

Browse files
committed
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
1 parent 724455b commit b8a14e1

File tree

18 files changed

+464
-246
lines changed

18 files changed

+464
-246
lines changed

__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__/runtime/integration.test.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
jest.unmock('twilio');
22

3+
import { Express } from 'express';
4+
import { readdirSync } from 'fs';
5+
import { basename, resolve } from 'path';
36
import request from 'supertest';
7+
import { StartCliConfig } from '../../src/config/start';
48
import { createServer } from '../../src/runtime/server';
5-
import { resolve, basename } from 'path';
6-
import cheerio from 'cheerio';
7-
import { readdirSync } from 'fs';
8-
import { Response as ExpressResponse, Express } from 'express';
9-
import { StartCliConfig } from '../../src/runtime/cli/config';
109

1110
const TEST_DIR = resolve(__dirname, '../../fixtures');
1211

__tests__/runtime/route.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from 'express';
66
import { Response as MockResponse } from 'jest-express/lib/response';
77
import { twiml } from 'twilio';
8-
import { StartCliConfig } from '../../src/runtime/cli/config';
8+
import { StartCliConfig } from '../../src/config/start';
99
import { Response } from '../../src/runtime/internal/response';
1010
import {
1111
constructContext,

0 commit comments

Comments
 (0)