UNPKG

1.58 kBJavaScriptView Raw
1'use strict'
2
3const { validate } = require('./schema')
4const checkMethod = require('./checkMethod')
5const {
6 $configurationInterface,
7 $handlerMethod,
8 $handlerSchema,
9 $mappingChecked,
10 $mappingMethod
11} = require('./symbols')
12
13function checkCwd (mapping) {
14 if (!mapping.cwd) {
15 mapping.cwd = process.cwd()
16 }
17}
18
19function invalidMatch () {
20 throw new Error('Invalid mapping match')
21}
22
23const matchTypes = {
24 undefined: mapping => {
25 mapping.match = /(.*)/
26 },
27 string: (mapping, match) => {
28 mapping.match = new RegExp(mapping.match)
29 },
30 object: (mapping, match) => {
31 if (!(mapping.match instanceof RegExp)) {
32 throw new Error('')
33 }
34 }
35}
36
37'boolean,number,bigint,symbol,function'
38 .split(',')
39 .forEach(type => {
40 matchTypes[type] = invalidMatch
41 })
42
43function checkMatch (mapping) {
44 matchTypes[typeof mapping.match](mapping, mapping.match)
45}
46
47function checkHandler (configuration, mapping) {
48 const { handler } = configuration.handler(mapping)
49 if (!handler) {
50 throw new Error('Unknown handler for mapping: ' + JSON.stringify(mapping))
51 }
52 return handler
53}
54
55module.exports = {
56 async check (configuration, mapping) {
57 checkCwd(mapping)
58 checkMatch(mapping)
59 const handler = checkHandler(configuration, mapping)
60 checkMethod(mapping, $mappingMethod, handler[$handlerMethod])
61 if (handler[$handlerSchema]) {
62 validate(handler[$handlerSchema], mapping)
63 }
64 if (handler.validate) {
65 await handler.validate(mapping, configuration[$configurationInterface])
66 }
67 mapping[$mappingChecked] = true
68 }
69}