-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
gen.mjs
117 lines (105 loc) · 2.3 KB
/
gen.mjs
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
import { existsSync, promises as fs } from 'fs'
import { join } from 'path'
import { dir } from './meta.mjs'
const sizes = [
'sm',
'lg',
'xl',
]
const colors = map(
[
'red',
'orange',
'blue',
],
[
'200',
'400',
],
)
const variants = [
'hover',
'first',
'active',
'dark',
'dark:hover:focus:first:active',
...sizes,
...map(sizes, ['dark:hover:focus:first:active'], ':'),
]
const names = [
...map(
['p', 'pt', 'm', 'mx', 'top'],
[
...Array.from({ length: 10 }, (_, i) => i.toString()),
'[1px]',
'[3vh]',
'[3.555em]',
],
),
...map(
['text', 'bg', 'border'],
[
...colors,
...map(colors, ['10', '20'], '/'),
'[#525343]',
'[#124453]',
'[#942]',
],
),
...map(
['text', 'rounded'],
[
...sizes,
],
),
...map([
'grid-cols',
], [
'1',
'2',
'[1fr,3em]',
'[20px,min-content,1fr]',
]),
]
function map(a, b, join = '-') {
const classes = []
for (const n of a) {
for (const t of b)
classes.push(n + join + t)
}
return classes
}
export const classes = [
...names,
...map(variants, names, ':'),
]
export async function writeMock() {
const content = () => `document.getElementById('app').innerHTML = \`${chunk(shuffle(classes)).map(c => `<div class="${c.join(' ')}" />`).join('\n')}\``
if (!existsSync(join(dir, 'source')))
await fs.mkdir(join(dir, 'source'))
await fs.writeFile(join(dir, 'source/gen1.js'), content(), 'utf-8')
await fs.writeFile(join(dir, 'source/gen2.js'), content(), 'utf-8')
await fs.writeFile(join(dir, 'source/gen3.js'), content(), 'utf-8')
await fs.writeFile(join(dir, 'source/gen.js'), 'import "./gen1";import "./gen2";import "./gen3";', 'utf-8')
return classes
}
export function shuffle(array) {
array = Array.from(array)
let curr = array.length
let idx
// While there remain elements to shuffle...
while (curr !== 0) {
// Pick a remaining element...
idx = Math.floor(Math.random() * curr)
curr--;
// And swap it with the current element.
[array[curr], array[idx]] = [array[idx], array[curr]]
}
return array
}
export function chunk(array, size = 15) {
const chunks = []
for (let i = 0; i < array.length; i += size)
chunks.push(array.slice(i, i + size))
return chunks
}