-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd-test-ids.js
126 lines (105 loc) · 3.43 KB
/
add-test-ids.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
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
const fs = require("fs");
const path = require("path");
const j = require("jscodeshift");
const { addTestIds } = require("./transforms/jsx-add-data-test-id-attribute");
const { MY_TAGS, HTML_TAGS, MUI_TAGS } = require("./transforms/constants");
/** @typedef {{ source: string; path: string; name: string; }} InputFile*/
const CUSTOM_IO_FOLDER = process.argv
.find((s) => s.includes("io-dir="))
?.split("io-dir=")[1];
const customAttribute = process.argv
.find((s) => s.includes("customAttribute="))
?.split("customAttribute=")[1];
const useAllHtmlTags = process.argv.includes("--all");
const useMuiTags = process.argv.includes("--mui");
const INPUT_FOLDER = path.join(__dirname, CUSTOM_IO_FOLDER || "input");
const OUTPUT_FOLDER = path.join(__dirname, CUSTOM_IO_FOLDER || "output");
const reactFileNaming = /[-_.a-zA-Z]*\.(tsx|jsx)$/;
const testFileNaming = /[-_.a-zA-Z]*\.test\.(tsx|jsx)$/;
const reactExtension = /\.(tsx|jsx)/;
const capitalLetters = /[A-Z]/g;
const htmlSymbolNumber = /&#\d{1,10};/g;
const htmlSymbolEntity = /&[a-z]{1,10};/g;
/* HACK !
* jscodeshift removes html entities such as   etc.,
* this solution escapes those symbols, then converts them back
*/
const registeredSymbols = [];
const toSafeChar = (symbol) => {
const escaped = escape(symbol);
registeredSymbols.push(escaped)
return escaped;
};
const escapeSymbol = (str) =>
str.replace(htmlSymbolEntity, toSafeChar).replace(htmlSymbolNumber, toSafeChar);
const unescapeSymbol = (str) =>
registeredSymbols.reduce((result, symbol) => {
return result.replace(symbol, unescape(symbol));
}, str);
const writeFile = (filePath, source) => {
const folderPath = filePath.replace(reactFileNaming, "");
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
fs.writeFileSync(filePath, source, { encoding: "utf-8" });
};
/**
* @param {string} inputFilePath
* @return {void}
* */
const transform = (inputFilePath) => {
const capitalToMiddleScore = (a, idx) => {
return `${idx ? "-" : ""}${a.toLowerCase()}`;
};
const kebabCaseName = inputFilePath
.match(reactFileNaming)[0]
.replace(reactExtension, "")
.replace(capitalLetters, capitalToMiddleScore);
/** @type {InputFile} */
const file = {
source: escapeSymbol(
fs.readFileSync(inputFilePath, { encoding: "utf-8" })
),
path: inputFilePath,
name: kebabCaseName,
};
let tagList = useAllHtmlTags ? HTML_TAGS : MY_TAGS;
if (useMuiTags) {
tagList = tagList.concat(MUI_TAGS);
}
/** @type {Buffer} */
const outputSource = addTestIds(
file,
j.withParser("tsx"),
tagList,
customAttribute
);
const outputFilePath = inputFilePath.replace(INPUT_FOLDER, OUTPUT_FOLDER);
writeFile(outputFilePath, unescapeSymbol(outputSource));
};
const isFolder = (dir) => fs.lstatSync(dir).isDirectory();
const isReactFile = (dir) => {
return (
fs.lstatSync(dir).isFile() &&
reactFileNaming.test(dir) &&
!testFileNaming.test(dir)
);
};
/**
* @param {string[]} dirs
* @param {string} parentDir
* @return {string[]} */
const toAbsolute = (dirs, parentDir = INPUT_FOLDER) => {
return dirs.map((dir) => path.join(parentDir, dir));
};
const stack = toAbsolute(fs.readdirSync(INPUT_FOLDER));
while (stack.length) {
const curDir = stack.pop();
if (isFolder(curDir)) {
const folders = toAbsolute(fs.readdirSync(curDir), curDir);
stack.push(...folders);
}
if (isReactFile(curDir)) {
transform(curDir);
}
}