-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
executable file
·146 lines (120 loc) · 3.71 KB
/
index.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
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
#!/usr/bin/env node
import fs from 'fs/promises'
import fg from 'fast-glob'
import path from 'path'
import { createUtils, } from '@windicss/plugin-utils'
import { prompt } from 'enquirer'
import { regexHTMLClass } from './regexes'
import meta from './meta'
const cwd = process.cwd()
function normalizeClass(input: string) {
if (input.includes(':'))
return input.substring(input.lastIndexOf(':') + 1)
return input
}
function convertAttributesToString(attributes, prefix): string {
let output = ' '
for (const attr in attributes) {
if (attributes[attr].length > 0)
output += `${prefix && attr !== 'class' ? prefix + '-' : ''}${attr}="${attributes[attr].join(' ')}" `
}
return output.trimEnd()
}
function findGroupForClass(input: string) {
input = normalizeClass(input)
for (const entry in meta.utility) {
for (const test of meta.utility[entry]) {
if (input.match(test))
return entry
}
}
return null
}
function replaceAtIndexes(code: string, replacements) {
let offset = 0
for (const node of replacements) {
code = code.slice(0, node.start + offset) + node.replacement + code.slice(node.end + offset)
offset += node.replacement.length - (node.end - node.start)
}
return code
}
async function init() {
console.log('🍃 WindiCSS Attributify Converter 🍃')
console.warn('⚠️ Please backup your code before running this tool ⚠️')
const { style, prefix, dir } = await prompt<{ style: string, prefix: string, dir: string }>([{
type: 'select',
name: 'style',
message: 'Choose Attributify Style',
choices: [
{ name: 'utilities', message: 'utilities', value: 'utilities' },
{ name: 'variants', message: 'variants', value: 'variants' },
],
}, {
type: 'input',
name: 'prefix',
message: 'Attribute Prefix',
initial: '',
}, {
type: 'input',
name: 'dir',
message: 'Directory (relative)',
initial: './src'
}])
const files = await fg(path.join(`${dir ? dir : '.'}`, `/**/*.{vue,html,svelte}`), {
onlyFiles: true,
ignore: ['node_modules'],
cwd,
})
const utils = createUtils()
await utils.init()
if (style === 'variants') {
console.log('Sorry this option is not supported yet.')
} else {
await Promise.all(files.map(async (file) => {
const code = await fs.readFile(file, 'utf-8')
const ast = await Promise.all(Array.from(code.matchAll(regexHTMLClass))
.map(async (match) => ({
value: match[0],
start: match.index,
end: match.index + match[0].length,
classes: (await utils.applyExtractors(match[0], file)).classes
})))
const nodes = ast.map((node) => ({
...node,
replaced: node.classes.filter((clazz) => findGroupForClass(clazz)),
attributes: node.classes.reduce((obj, clazz) => {
const group = findGroupForClass(clazz)
if (group) {
let name = clazz
if (name === group)
name = 'default'
else
name = clazz.replace(new RegExp(group + '(-)?', 'gm'), '')
if (group in obj) {
obj[group] = [...obj[group], name]
} else {
obj[group] = [name]
}
}
return obj
}, {})
}))
.map((node) => {
return {
...node,
replacement: convertAttributesToString({
...node.attributes,
class: node.classes.filter((x) => node.replaced.indexOf(x) === -1)
}, prefix)
}
})
await fs.writeFile(file, replaceAtIndexes(code, nodes))
console.log(`Update ${file}`)
}))
}
console.log('Done!')
}
init()
.catch((e) => {
console.error(e)
})