This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
util.js
80 lines (70 loc) · 2 KB
/
util.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
var path = require('path'),
configparser = require('gemini-configparser'),
GeminiError = require('../errors/gemini-error'),
option = configparser.option,
map = configparser.map;
function is(type) {
return function(value) {
if (typeof value !== type) {
throw new GeminiError('a value must be ' + type);
}
};
}
exports.assertNonNegative = (value, optName) => {
is('number')(value);
if (value < 0) {
throw new GeminiError(`"${optName}" must be non-negative`);
}
};
function resolveWithProjectRoot(value, config) {
return path.resolve(config.system.projectRoot, value);
}
function booleanOption(defaultValue) {
return option({
parseCli: parseBoolean,
parseEnv: parseBoolean,
validate: is('boolean'),
defaultValue: defaultValue
});
}
function parseBoolean(value) {
switch (value.toLowerCase()) {
case '1':
case 'yes':
case 'true':
return true;
case '0':
case 'no':
case 'false':
return false;
default:
throw new GeminiError('Unexpected value for boolean option ' + value);
}
}
function positiveIntegerOption(defaultValue) {
return option({
parseEnv: Number,
parseCli: Number,
defaultValue: defaultValue,
validate: function(value) {
if (typeof value !== 'number') {
throw new GeminiError('Field must be an integer number');
}
if (value <= 0) {
throw new GeminiError('Field must be positive');
}
if (Math.floor(value) !== value) {
throw new GeminiError('Field must be an integer number');
}
}
});
}
function anyObject() {
return map(option({}));
}
exports.is = is;
exports.resolveWithProjectRoot = resolveWithProjectRoot;
exports.anyObject = anyObject;
exports.booleanOption = booleanOption;
exports.positiveIntegerOption = positiveIntegerOption;