-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.ts
71 lines (56 loc) · 1.74 KB
/
plugin.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
import { getSource } from './source'
import { fromPairs, keys, map, mergeLeft, pipe } from 'ramda'
import { createFile } from './file'
const defaultOptions = {
path: 'tailwind-types.ts',
exportClassesChoice: true,
exportClassesList: false,
exportClassNamesHelper: false,
exportConstants: false,
}
let alreadyUsed = false
type Options = Partial<typeof defaultOptions>
const getTailwindConfigObject = (configFunction) => pipe(
keys,
map(key => [key, configFunction(key)]),
fromPairs,
)(require('tailwindcss/defaultConfig'))
const validateConfig = (options: Options) => {
let hasError = false
if (options.exportConstants) {
try {
require.resolve('camelcase')
} catch (er) {
hasError = true
console.error('❌ "camelcase" package is required with { exportConstants: true } configuration.')
}
}
if (options.exportClassNamesHelper) {
try {
require.resolve('classnames')
} catch (er) {
hasError = true
console.error('❌ "classnames" package is required with { exportClassNamesHelper: true } configuration.')
}
}
if (hasError) {
console.log()
process.exit(1)
}
}
const generatorPlugin = (userOptions: Options = {}) => {
const options = mergeLeft(userOptions, defaultOptions)
validateConfig(options)
return ({ config: configFunction }) => {
if (alreadyUsed) {
// we don't want to re-load this plugin when using it, or it will provoke an infinite loop.
return
}
alreadyUsed = true
getSource(getTailwindConfigObject(configFunction), options)
.then(createFile(options.path))
.then(() => console.log(`✅ Tailwind types generated in ${options.path}.`))
}
}
generatorPlugin.__isOptionsFunction = true
export = generatorPlugin