-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.ts
105 lines (97 loc) · 2.92 KB
/
options.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* getOptions from package.json
*/
import Ajv, { AjvError } from 'ajv'
import betterAjvErrors from 'better-ajv-errors'
import fs from 'fs-extra'
import path from 'path'
import groupBy from 'lodash/groupBy'
import { ProxyConfig } from './proxy'
import chalk from 'chalk'
export interface ImportPluginConfig {
libraryName: string
style?: 'css' | boolean
libraryDirectory?: boolean
camel2DashComponentName?: boolean
}
export interface JMOptions {
electron?: boolean
// IE 8 模式
ie8?: boolean
proxy?: ProxyConfig
importPlugin?: ImportPluginConfig | ImportPluginConfig[]
enableDuplicatePackageCheck: boolean
enableTypescriptCheck: boolean
enableTypescriptAsyncCheck: boolean
enableCircularDependencyCheck: boolean
enableDllInProduction?: boolean
enableESlintInProduction?: boolean
inject?: { [key: string]: string }
useBuiltIns: 'entry' | 'usage' | false
alias?: { [key: string]: string }
electronExternalsWhitelist?: string[]
dll?: {
include?: string[]
exclude?: string[]
}
}
const defaultOptions: JMOptions = {
enableDuplicatePackageCheck: false,
enableTypescriptAsyncCheck: false,
enableTypescriptCheck: true,
enableESlintInProduction: false,
enableCircularDependencyCheck: false,
inject: {},
useBuiltIns: 'usage',
}
const key = 'jm'
let options: JMOptions | undefined
export default function getOptions(pkg: { [key: string]: any }): JMOptions {
if (options) {
return options
}
const schemaPath = path.join(__dirname, '../lib/option.schema.json')
const schema = fs.readJsonSync(schemaPath)
if (key in pkg) {
const ajv = new Ajv({ jsonPointers: true })
const validate = ajv.compile(schema)
if (validate(pkg[key])) {
return (options = { ...defaultOptions, ...pkg[key] })
}
const errors = prepareErrors(validate.errors)
const output = betterAjvErrors(schema, pkg[key], errors, { indent: 2 })
console.log(
chalk.red(`❌ Configuration error. Check property ${chalk.cyan(key)} of the ${chalk.cyan('package.json')}`),
)
console.log(output)
process.exit(1)
}
return (options = defaultOptions)
}
function prepareErrors(errors: AjvError[]) {
const removeKeywords = ['anyOf', 'oneOf']
const newErrors = errors.filter(i => removeKeywords.indexOf(i.keyword) === -1)
// merge same path error
const groups = groupBy(newErrors, 'dataPath')
return Object.keys(groups)
.map(k => {
return {
...groups[k][0],
message: groups[k].map(i => i.message).join(' or '),
}
})
.reduce<AjvError[]>((all, cur) => {
const index = all.findIndex(i => i.dataPath.startsWith(cur.dataPath) || cur.dataPath.startsWith(i.dataPath))
if (index !== -1) {
const existed = all[index]
// has more specific error
if (existed.dataPath.length > cur.dataPath.length) {
return all
}
all.splice(index, 1, cur)
} else {
all.push(cur)
}
return all
}, [])
}