forked from TryGhost/Ghost-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-config.js
54 lines (45 loc) · 1.61 KB
/
validate-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
const get = require('lodash/get');
const path = require('path');
const filter = require('lodash/filter');
const Promise = require('bluebird')
const errors = require('../../../errors');
const Config = require('../../../utils/config');
const advancedOptions = require('../../config/advanced');
const taskTitle = 'Validating config';
function validateConfig(ctx) {
const config = Config.exists(path.join(process.cwd(), `config.${ctx.system.environment}.json`));
if (config === false) {
return Promise.reject(new errors.ConfigError({
environment: ctx.system.environment,
message: 'Config file is not valid JSON',
task: taskTitle
}));
}
const configValidations = filter(advancedOptions, cfg => cfg.validate);
return Promise.each(configValidations, (configItem) => {
const key = configItem.configPath || configItem.name
const value = get(config, key);
if (!value) {
return;
}
return Promise.resolve(configItem.validate(value)).then((validated) => {
if (validated !== true) {
return Promise.reject(new errors.ConfigError({
config: {
[key]: value
},
message: validated,
environment: ctx.system.environment,
task: taskTitle
}));
}
});
});
}
module.exports = {
title: taskTitle,
task: validateConfig,
skip: (ctx) => ctx.instance && ctx.instance.process.isRunning(ctx.instance.dir),
category: ['start']
}