-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmake-nextjs-happy.js
51 lines (44 loc) · 1.19 KB
/
make-nextjs-happy.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
import fs from 'fs'
import path from 'path'
import prettier from 'prettier'
import * as HUI from '../packages/@headlessui-react/src/index.ts'
let customRemaps = {
tab: 'tabs',
radio: 'radio-group',
data: 'data-interactive',
focus: 'focus-trap',
}
let components = Object.keys(HUI)
async function run() {
for (let component of components) {
let name = component.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
let module = name.split('-')[0]
module = customRemaps[module] ?? module
let filePath = path.resolve(
__dirname,
'..',
'packages',
'@headlessui-react',
'src',
'components',
name,
`${name}.tsx`
)
// Main module path already exists
if (!fs.existsSync(filePath)) {
fs.mkdirSync(path.dirname(filePath), { recursive: true })
fs.writeFileSync(filePath, await template(component, module))
}
}
}
async function template(name, module) {
return await prettier.format(
[
'// Next.js barrel file improvements (GENERATED FILE)',
`export type * from '../${module}/${module}';`,
`export { ${name} } from '../${module}/${module}';`,
].join('\n'),
{ parser: 'typescript' }
)
}
run()