Skip to content

Commit 5db8d95

Browse files
committed
fix(config): don't allow nodeEnv in file + fix save to file
- save to file should now work and save to file one time (even if it's in a env.json file) - removed nodeEnv from env.json schema because it's weird :-)
1 parent a68ad17 commit 5db8d95

File tree

4 files changed

+28
-58
lines changed

4 files changed

+28
-58
lines changed

env.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"properties":{"nodeEnv":{"description":"Tells which env file to use","type":"string","enum":["development","production","test","devcontainer"]},"port":{"description":"Set server port","type":"number"},"dbUrl":{"description":"DB connection URL. Expects a mongodb db for connections","format":"url","type":"string"},"webhookProxyUrl":{"description":"Used to create a custom repeatable smee webhook url instead of Generating a random one","format":"url","type":"string","pattern":"^https:\\/\\/(?:www\\.)?smee\\.io\\/[a-zA-Z0-9_-]+\\/?"},"webhookDestinationUrl":{"description":"proxy should sent events to this url for achievibit","pattern":"^([\\w]+)?(\\/[\\w-]+)*$","type":"string"},"saveToFile":{"description":"Create a file made out of the internal config. This is mostly for merging command line, environment, and file variables to a single instance","type":"boolean"}},"type":"object","required":["nodeEnv","port","webhookProxyUrl","webhookDestinationUrl","saveToFile"]}
1+
{"properties":{"port":{"description":"Set server port","type":"number"},"dbUrl":{"description":"DB connection URL. Expects a mongodb db for connections","format":"url","type":"string"},"webhookProxyUrl":{"description":"Used to create a custom repeatable smee webhook url instead of Generating a random one","format":"url","type":"string","pattern":"^https:\\/\\/(?:www\\.)?smee\\.io\\/[a-zA-Z0-9_-]+\\/?"},"webhookDestinationUrl":{"description":"proxy should sent events to this url for achievibit","pattern":"^([\\w]+)?(\\/[\\w-]+)*$","type":"string"},"saveToFile":{"description":"Create a file made out of the internal config. This is mostly for merging command line, environment, and file variables to a single instance","type":"boolean"}},"type":"object","required":["port","webhookProxyUrl","webhookDestinationUrl","saveToFile"]}

server/src/config/achievibit-config.model.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ export class AchievibitConfig {
9191
}
9292
}).AchievibitConfig;
9393

94+
delete configJsonSchema.properties.nodeEnv;
95+
configJsonSchema.required.splice(
96+
configJsonSchema.required.indexOf('nodeEnv'),
97+
1
98+
);
99+
100+
// console.log(configJsonSchema);
101+
94102
return configJsonSchema;
95103
}
96104
}

server/src/config/config.service.spec.ts

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,31 @@ describe('ConfigService', () => {
2121
});
2222
});
2323

24-
afterEach(async (done) => {
24+
afterEach(() => {
2525
configService.closeEvents();
26-
27-
done();
2826
});
2927

30-
it('should be defined', async (done) => {
28+
it('should be defined', () => {
3129
expect(configService).toBeDefined();
32-
33-
done();
3430
});
3531

3632
it('should return the same instance if initiated without an input',
37-
async (done) => {
33+
() => {
3834
expect(new ConfigService()).toBe(configService);
39-
40-
done();
4135
});
4236

43-
it('should create a new instance when passed an override', async (done) => {
37+
it('should create a new instance when passed an override', () => {
4438
expect(new ConfigService({} as AchievibitConfig)).not.toBe(configService);
45-
46-
done();
4739
});
4840

4941
it('should set default values to everything that needs one',
50-
async (done) => {
42+
() => {
5143
expect(configService.toPlainObject()).toMatchSnapshot();
52-
53-
done();
5444
});
5545
});
5646

5747
describe('Smee & Events', () => {
58-
it('should NOT initial smee and events on production', async (done) => {
48+
it('should NOT initial smee and events on production', () => {
5949
const productionService = new ConfigService({
6050
nodeEnv: 'production'
6151
});
@@ -64,11 +54,9 @@ describe('ConfigService', () => {
6454
expect(productionService.events).toBeUndefined();
6555

6656
productionService.closeEvents();
67-
68-
done();
6957
});
7058

71-
it('should initial smee and events on development', async (done) => {
59+
it('should initial smee and events on development', () => {
7260
const productionService = new ConfigService({
7361
nodeEnv: 'development'
7462
});
@@ -77,8 +65,6 @@ describe('ConfigService', () => {
7765
expect(productionService.events).toBeDefined();
7866

7967
productionService.closeEvents();
80-
81-
done();
8268
});
8369
});
8470

@@ -91,19 +77,17 @@ describe('ConfigService', () => {
9177

9278
chain(NODE_ENVIRONMENT_OPTIONS)
9379
.forEach((nodeEnv: string) => {
94-
it(`should ACCEPT ${ nodeEnv }`, async (done) => {
80+
it(`should ACCEPT ${ nodeEnv }`, () => {
9581
const serviceWrapper = () => new ConfigService({ nodeEnv });
9682

9783
expect(serviceWrapper().toPlainObject).toBeDefined();
9884

9985
serviceWrapper().closeEvents();
100-
101-
done();
10286
});
10387
})
10488
.value();
10589

106-
it('should REJECT other values', async (done) => {
90+
it('should REJECT other values', () => {
10791

10892
const nodeEnv = 'value_not_allowed';
10993

@@ -119,23 +103,19 @@ describe('ConfigService', () => {
119103

120104
expect(wrongEnvType).toThrowError(ConfigValidationError);
121105
expect(wrongEnvType).toThrowErrorMatchingSnapshot();
122-
123-
done();
124106
});
125107

126108
});
127109

128110
describe('port', () => {
129-
it('should ACCEPT numbers', async (done) => {
111+
it('should ACCEPT numbers', () => {
130112
const port = DtoMockGenerator.integer();
131113
const service = new ConfigService({ port });
132114

133115
expect(service.port).toBe(port);
134-
135-
done();
136116
});
137117

138-
it('should REJECT values other than numbers', async (done) => {
118+
it('should REJECT values other than numbers', () => {
139119
const stringPort = () => new (ConfigService as any)({ port: 'hello' });
140120
const ObjectPort = () => new (ConfigService as any)({ port: {} });
141121

@@ -144,34 +124,28 @@ describe('ConfigService', () => {
144124

145125
expect(ObjectPort).toThrowError(ConfigValidationError);
146126
expect(ObjectPort).toThrowErrorMatchingSnapshot();
147-
148-
done();
149127
});
150128

151129
});
152130

153131
describe('dbUrl', () => {
154-
it('should ACCEPT empty value', async (done) => {
132+
it('should ACCEPT empty value', () => {
155133
const service = new ConfigService({ dbUrl: undefined });
156134

157135
expect(service.dbUrl).toBeUndefined();
158136

159137
service.closeEvents();
160-
161-
done();
162138
});
163139

164-
it('should ACCEPT localhost mongodb URL', async (done) => {
140+
it('should ACCEPT localhost mongodb URL', () => {
165141
const localMongodbUrl = 'mongodb://localhost:27017';
166142

167143
const configService = new ConfigService({ dbUrl: localMongodbUrl });
168144

169145
expect(configService.dbUrl).toBe(localMongodbUrl);
170-
171-
done();
172146
});
173147

174-
it('should ACCEPT valid mongodb URLS', async (done) => {
148+
it('should ACCEPT valid mongodb URLS', () => {
175149
const mongodbProtocolUrls =
176150
times(10, () => DtoMockGenerator.mongodbUrl());
177151

@@ -182,28 +156,22 @@ describe('ConfigService', () => {
182156
expect(configService.dbUrl).toBe(mongodbUrl);
183157
})
184158
.value();
185-
186-
done();
187159
});
188160

189-
it('should REJECT non-mongodb URLS', async (done) => {
161+
it('should REJECT non-mongodb URLS', () => {
190162
const invalidUrl =
191163
() => new ConfigService({ dbUrl: 'https://google.com/' });
192164

193165
expect(invalidUrl).toThrowError(ConfigValidationError);
194166
expect(invalidUrl).toThrowErrorMatchingSnapshot();
195-
196-
done();
197167
});
198168
});
199169

200170
describe('webhookProxyUrl', () => {
201-
it('should ACCEPT smee.io URLS', async (done) => {
171+
it('should ACCEPT smee.io URLS', () => {
202172
const smeeProtocolUrls =
203173
times(10, () => DtoMockGenerator.randexp(SMEE_IO_REGEX).gen());
204174

205-
console.log(smeeProtocolUrls);
206-
207175
chain(smeeProtocolUrls)
208176
.keyBy()
209177
.mapValues((smeeProtocolUrl) => new ConfigService({
@@ -214,28 +182,22 @@ describe('ConfigService', () => {
214182
expect(configService.webhookProxyUrl).toBe(smeeProtocolUrl);
215183
})
216184
.value();
217-
218-
done();
219185
});
220186

221-
it('should REJECT non-smee URLS', async (done) => {
187+
it('should REJECT non-smee URLS', () => {
222188
const invalidUrl =
223189
() => new ConfigService({ webhookProxyUrl: 'https://google.com/' });
224190

225191
expect(invalidUrl).toThrowError(ConfigValidationError);
226192
expect(invalidUrl).toThrowErrorMatchingSnapshot();
227-
228-
done();
229193
});
230194

231-
it('should REJECT non-URLS', async (done) => {
195+
it('should REJECT non-URLS', () => {
232196
const invalidUrl =
233197
() => new ConfigService({ webhookProxyUrl: 'hello world' });
234198

235199
expect(invalidUrl).toThrowError(ConfigValidationError);
236200
expect(invalidUrl).toThrowErrorMatchingSnapshot();
237-
238-
done();
239201
});
240202
});
241203
});

server/src/config/config.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class ConfigService extends AchievibitConfig {
113113
events = undefined;
114114
}
115115

116-
if (this.saveToFile) {
116+
if (config.saveToFile) {
117117
writeJson(configFilePath, classToPlain(this), { spaces: 2 });
118118
}
119119

0 commit comments

Comments
 (0)