-
Notifications
You must be signed in to change notification settings - Fork 18
/
get-config.js
183 lines (170 loc) · 5.22 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Spaghetti were carefully borrowed from:
* https://github.com/ai/size-limit/blob/master/cli.js
*/
import { lilconfig } from 'lilconfig'
import { relative } from 'node:path'
import { isObject } from './utils.js'
const PACKAGE_ERRORS = {
empty: 'The `"clean-publish"` section of package.json must `not be empty`',
fieldsNotStrings:
'The `fields` in the `"clean-publish"` section ' +
'of package.json must be `an array of strings`',
filesNotStringsOrRegExps:
'The `files` in the `"clean-publish"` section ' +
'of package.json must be ' +
'`an array of strings or RegExps`',
notObject:
'The `"clean-publish"` section of package.json ' + 'must be `an object`',
packageManagerNotString:
'The `packageManager` in the `"clean-publish"` section ' +
'of package.json must be `an string`',
packageManagerOptionsNotStrings:
'The `packageManagerOptions` in the `"clean-publish"` section ' +
'of package.json must be `an array of strings`',
tempDirNotString:
'The `tempDir` in the `"clean-publish"` section ' +
'of package.json must be `an string`'
}
const FILE_ERRORS = {
empty: 'Clean Publish config must `not be empty`',
fieldsNotStrings:
'The `fields` in Clean Publish config ' + 'must be `an array of strings`',
filesNotStringsOrRegExps:
'The `files` in the Clean Publish config ' +
'must be `an array of strings or RegExps`',
notObject: 'Clean Publish config must contain `an object`',
packageManagerNotString:
'The `packageManager` in Clean Publish config ' +
'must be `an string`',
packageManagerOptionsNotStrings:
'The `packageManagerOptions` in Clean Publish config ' +
'must be `an array of strings`',
tempDirNotString:
'The `tempDir` in Clean Publish config ' + 'must be `an string`'
}
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 isString(value) {
return typeof value === 'string' && value
}
function isStrings(value) {
if (!Array.isArray(value)) return false
return value.every(isString)
}
function isStringsOrRegExps(value) {
if (!Array.isArray(value)) return false
return value.every(i => isString(i) || i instanceof RegExp)
}
function isStringOrUndefined(value) {
return typeof value === 'undefined' || isString(value)
}
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 (!isObject(config)) {
return 'notObject'
}
if (Object.keys(config).length === 0) {
return 'empty'
}
if (!isStringsOrRegExpsOrUndefined(config.files)) {
return 'filesNotStringsOrRegExps'
}
if (!isStringsOrUndefined(config.fields)) {
return 'fieldsNotStrings'
}
if (!isStringOrUndefined(config.tempDir)) {
return 'tempDirNotString'
}
if (!isStringOrUndefined(config.packageManager)) {
return 'packageManagerNotString'
}
if (!isStringsOrUndefined(config.packageManagerOptions)) {
return 'packageManagerOptionsNotStrings'
}
return false
}
export function getConfig() {
const explorer = lilconfig('clean-publish', {
searchPlaces: ['package.json', '.clean-publish', '.clean-publish.js', '.clean-publish.json']
})
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 = 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
})
}