-
Notifications
You must be signed in to change notification settings - Fork 709
/
index.js
46 lines (42 loc) · 1.46 KB
/
index.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
'use strict'
module.exports = presetLoader(require)
module.exports.presetLoader = presetLoader
function presetLoader (requireMethod) {
return path => {
let name = ''
let scope = ''
if (typeof path === 'string') {
name = path.toLowerCase()
} else if (typeof path === 'object' && path.name) {
// Rather than a string preset name, options.preset can be an object
// with a "name" key indicating the preset to load; additinoal key/value
// pairs are assumed to be configuration for the preset. See the documentation
// for a given preset for configuration available.
name = path.name.toLowerCase()
} else {
throw Error('preset must be string or object with key name')
}
if (name[0] === `@`) {
const parts = name.split(`/`)
scope = parts.shift() + `/`
name = parts.join(`/`)
}
if (!name.startsWith('conventional-changelog-')) {
name = `conventional-changelog-${name}`
}
try {
const config = requireMethod(`${scope}${name}`)
// rather than returning a promise, presets can return a builder function
// which accepts a config object (allowing for customization) and returns
// a promise.
if (config && !config.then && typeof path === 'object') {
return config(path)
} else {
// require returned a promise that resolves to a config object.
return config
}
} catch (_) {
throw Error('does not exist')
}
}
}