forked from shashkovdanil/clean-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-config.js
138 lines (127 loc) · 4.21 KB
/
get-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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Spaghetti were carefully borrowed from:
* https://github.com/ai/size-limit/blob/master/cli.js
*/
const path = require('path')
const cosmiconfig = require('cosmiconfig')
const PACKAGE_ERRORS = {
notObject: 'The `"clean-publish"` section of package.json ' +
'must be `an object`',
empty: 'The `"clean-publish"` section of package.json must `not be empty`',
filesNotStringsOrRegExps: 'The `files` in the `"clean-publish"` section ' +
'of package.json must be ' +
'`an array of strings or RegExps`',
fieldsNotStrings: 'The `fields` in the `"clean-publish"` section ' +
'of package.json must be `an array of strings`'
}
const FILE_ERRORS = {
notObject: 'Clean Publish config must contain `an object`',
empty: 'Clean Publish config must `not be empty`',
filesNotStringsOrRegExps: 'The `files` in the Clean Publish config ' +
'must be `an array of strings or RegExps`',
fieldsNotStrings: 'The `fields` in Clean Publish config ' +
'must be `an array of strings`'
}
const PACKAGE_EXAMPLE = '\n' +
' "clean-publish": {\n' +
' "files": ["file1.js", "file2.js"],\n' +
' "packageManager": "yarn"\n' +
' }'
const FILE_EXAMPLE = '\n' +
' {\n' +
' "files": ["file1.js", "file2.js"],\n' +
' "packageManager": "yarn"\n' +
' }'
function isStrings (value) {
if (!Array.isArray(value)) return false
return value.every(i => typeof i === 'string')
}
function isStringsOrRegExps (value) {
if (!Array.isArray(value)) return false
return value.every(i => typeof i === 'string' || i instanceof RegExp)
}
function isStringsOrUndefined (value) {
return typeof value === 'undefined' || isStrings(value)
}
function isStringsOrRegExpsOrUndefined (value) {
return typeof value === 'undefined' || isStringsOrRegExps(value)
}
function capitalize (str) {
return str[0].toUpperCase() + str.slice(1)
}
function configError (config) {
if (!config || typeof config !== 'object') {
return 'notObject'
}
if (Object.keys(config).length === 0) {
return 'empty'
}
if (!isStringsOrRegExpsOrUndefined(config.files)) {
return 'filesNotStringsOrRegExps'
}
if (!isStringsOrUndefined(config.fields)) {
return 'fieldsNotStrings'
}
return false
}
function getConfig () {
const explorer = cosmiconfig('clean-publish', {
searchPlaces: [
'package.json',
'.clean-publish',
'.clean-publish.js'
]
})
return explorer
.search()
.catch(err => {
if (err.name === 'JSONError') {
const regexp = /JSON\s?Error\sin\s[^\n]+:\s+([^\n]+)( while parsing)/
let message = err.message
if (regexp.test(message)) {
message = message.match(regexp)[1]
}
throw new Error(
'Can not parse `package.json`. ' +
message + '. ' +
'Change config according to Clean Publish docs.\n' +
PACKAGE_EXAMPLE + '\n'
)
} else if (err.reason && err.mark && err.mark.name) {
const file = path.relative(process.cwd(), err.mark.name)
const position = err.mark.line + ':' + err.mark.column
throw new Error(
'Can not parse `' + file + '` at ' + position + '. ' +
capitalize(err.reason) + '. ' +
'Change config according to Clean Publish docs.\n' +
FILE_EXAMPLE + '\n'
)
} else {
throw err
}
})
.then(result => {
if (result === null) {
return {}
}
const { config } = result
const error = configError(config)
if (error) {
if (/package\.json$/.test(config.filepath)) {
throw new Error(
PACKAGE_ERRORS[error] + '. ' +
'Fix it according to Clean Publish docs.' +
`\n${ PACKAGE_EXAMPLE }\n`
)
} else {
throw new Error(
FILE_ERRORS[error] + '. ' +
'Fix it according to Clean Publish docs.' +
`\n${ FILE_EXAMPLE }\n`
)
}
}
return config
})
}
module.exports = getConfig