-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
read-config.js
64 lines (60 loc) · 1.72 KB
/
read-config.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
import { exists } from 'fs-extra'
import { join } from 'path'
import slugify from '@sindresorhus/slugify'
import { constants } from './constants'
const defaultConfig = {
apiVersion: constants.apiVersion,
name: constants.packageJson.defaultPluginName,
id: constants.packageJson.defaultPluginId,
command: 'index.js'
}
export async function readConfig () {
const packageJsonPath = join(process.cwd(), 'package.json')
if ((await exists(packageJsonPath)) === false) {
return defaultConfig
}
const packageJson = require(packageJsonPath)
const config = packageJson[constants.packageJson.configKey]
if (typeof config === 'undefined' || Object.keys(config).length === 0) {
return defaultConfig
}
return {
...createMenuItem(config),
apiVersion:
typeof config.apiVersion !== 'undefined'
? config.apiVersion
: constants.apiVersion,
id: typeof config.id === 'undefined' ? slugify(config.name) : config.id
}
}
function createMenuItem (config) {
const result = {}
result.name = config.name
if (typeof config.command !== 'undefined') {
result.command = config.command
const handler =
typeof config.handler !== 'undefined' ? config.handler : 'default'
result.handler = handler
result.id = `${config.command}--${handler}`
}
if (typeof config.ui !== 'undefined') {
result.ui = config.ui
}
if (typeof config.menu !== 'undefined') {
result.menu = normaliseMenu(config.menu)
}
return result
}
function normaliseMenu (menu) {
const result = []
menu.forEach(function (item) {
if (item === '-') {
result.push({ separator: true })
return
}
if (typeof item !== 'undefined') {
result.push(createMenuItem(item))
}
})
return result
}