-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.ts
55 lines (43 loc) · 1.26 KB
/
index.ts
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
import cucumber from './cucumber'
import Parser from './parser'
import { resolve, extname } from 'path'
import multi from './multi'
import standard from './standard'
type ParserList = {
[key: string]: Parser
}
const all: ParserList = { cucumber, multi, standard }
function handleObject (parserObject: any) {
if (typeof parserObject.parse !== 'function') {
throw new Error(`Invalid Parser Object specified. Your parser must define a \`parse\` method`)
}
return parserObject
}
function handlePath (parserPath: string) {
// 'my-custom-parser' or './my-custom-parser'
try {
return require(parserPath)
} catch (e) {}
// /path/to/parser or ../path/to/parser
try {
return require(resolve(parserPath))
} catch (e) {}
throw new Error(`Invalid Custom Parser Path Specified: ${parserPath}`)
}
function handleFlakeParser (parserName: string) {
if (all[parserName]) {
return all[parserName]
} else {
throw new Error(`Invalid Parser Specified: ${parserName}`)
}
}
function getParser (parser: (Parser | string) = '') {
if (parser.hasOwnProperty('parse')) {
return handleObject(parser)
}
if (extname(parser as string)) {
return handlePath(parser as string)
}
return handleFlakeParser(parser as string)
}
export { all, getParser }