-
Notifications
You must be signed in to change notification settings - Fork 0
/
codemod-expand-transforms.js
71 lines (62 loc) · 1.75 KB
/
codemod-expand-transforms.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
const glob = require('fast-glob')
const fs = require('fs')
const UNSUPPORTED_TRANSFORMS = [
'perspective',
'scaleX',
'scaleY',
'scaleZ',
'translateZ',
'transformOriginZ',
]
const isUnsupported = name => UNSUPPORTED_TRANSFORMS.includes(name)
const composeTransformNewLines = line => {
const name = line.split('(')[0]
if (isUnsupported(name)) return line
const valuesMatch = line.match(/\(([^)]+)\)/)
if (!valuesMatch) return line
const values = valuesMatch[1].split(/,\s|,/)
if (name === 'rotateZ') {
return `rotate ${values[0]}`
} else if (values.length === 1) {
return `${name} ${values[0]}`
} else {
const axes = ['X', 'Y']
return axes.map((axis, j) => `${name}${axis} ${values[j]}`).join('\n')
}
}
const composeTransformOriginNewLines = line => {
if (line.startsWith('<')) {
const [x, y] = line.replace('< ').split(' ')
return `transformOriginX < ${x}\ntransformOriginY < ${y}`
} else {
const [x, y] = line.split(' ')
return `transformOriginX ${x}\ntransformOriginY ${y}`
}
}
function transform(rtext) {
const text = rtext.replace(/\r\n/g, '\n')
const lines = text.split('\n').map(line => line.trim())
return lines
.map(
line =>
line.startsWith('transform ')
? composeTransformNewLines(line.split('transform ')[1])
: line.startsWith('transformOrigin ')
? composeTransformOriginNewLines(line.split('transformOrigin ')[1])
: line
)
.join('\n')
}
glob(['src/**/*.view'], {
bashNative: ['linux'],
cwd: process.cwd(),
}).then(list => {
list.forEach(file => {
try {
const content = fs.readFileSync(file, 'utf-8')
fs.writeFileSync(file, transform(content))
} catch (error) {
console.error(error)
}
})
})