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
| const nopt = require('nopt')
|
| const { validate: validateUmask } = require('./umask.js')
|
| class Umask {}
| class Semver {}
| const semverValid = require('semver/functions/valid')
| const validateSemver = (data, k, val) => {
| const valid = semverValid(val)
| if (!valid) {
| return false
| }
| data[k] = valid
| }
|
| const noptValidatePath = nopt.typeDefs.path.validate
| const validatePath = (data, k, val) => {
| if (typeof val !== 'string') {
| return false
| }
| return noptValidatePath(data, k, val)
| }
|
| // add descriptions so we can validate more usefully
| module.exports = {
| ...nopt.typeDefs,
| semver: {
| type: Semver,
| validate: validateSemver,
| description: 'full valid SemVer string',
| },
| Umask: {
| type: Umask,
| validate: validateUmask,
| description: 'octal number in range 0o000..0o777 (0..511)',
| },
| url: {
| ...nopt.typeDefs.url,
| description: 'full url with "http://"',
| },
| path: {
| ...nopt.typeDefs.path,
| validate: validatePath,
| description: 'valid filesystem path',
| },
| Number: {
| ...nopt.typeDefs.Number,
| description: 'numeric value',
| },
| Boolean: {
| ...nopt.typeDefs.Boolean,
| description: 'boolean value (true or false)',
| },
| Date: {
| ...nopt.typeDefs.Date,
| description: 'valid Date string',
| },
| }
|
| // TODO: make nopt less of a global beast so this kludge isn't necessary
| nopt.typeDefs = module.exports
|
|