-
Notifications
You must be signed in to change notification settings - Fork 0
/
codemod-add-new-line-to-every-block.js
62 lines (50 loc) · 1.54 KB
/
codemod-add-new-line-to-every-block.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
const glob = require('fast-glob');
const fs = require('fs');
const CAPTURE = /^(CaptureEmail|CaptureFile|CaptureNumber|CapturePhone|CaptureSecure|CaptureText|CaptureTextArea)$/i;
const NOT_GROUP = /^(Image|FakeProps|Text|Proxy|SvgCircle|SvgEllipse|SvgLine|SvgPath|SvgPolygon|SvgPolyline|SvgRect|SvgText|SvgStop)$/i;
const BLOCK = /^([A-Z][a-zA-Z0-9]*)(\s+([A-Z][a-zA-Z0-9]*))?$/;
const is = (thing, line) => thing.test(line);
const get = (regex, line) => line.match(regex);
const getBlock = line => {
// eslint-disable-next-line
const [_, is, _1, block] = get(BLOCK, line);
return {
block: block || is,
is: block ? is : null,
};
};
const isCapture = line => is(CAPTURE, line);
const isGroup = line => !is(NOT_GROUP, line) && !isCapture(line);
function shouldAddNewLine(line) {
const { block } = getBlock(line);
return !isGroup(block);
}
const isBlock = line => is(BLOCK, line);
function transform(rtext) {
const text = rtext.replace(/\r\n/g, '\n');
const lines = text.split('\n').map(line => line.trim());
const next = [];
let addNewLine = false;
lines.forEach(line => {
if (isBlock(line)) {
if (addNewLine) {
addNewLine = false;
next.push('');
}
if (shouldAddNewLine(line)) {
addNewLine = true;
}
}
next.push(line);
});
return next.join('\n');
}
glob(['src/**/*.view'], {
bashNative: ['linux'],
cwd: process.cwd(),
}).then(list => {
list.forEach(file => {
const content = fs.readFileSync(file, 'utf-8');
fs.writeFileSync(file, transform(content));
});
});