-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
97 lines (72 loc) · 2.46 KB
/
app.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
const { readdir, rename, readFileSync } = require("fs");
class App {
setInput() {
const dirDivider = "withoutType";
const nameWithoutExt = "ComponentA";
const pathWithoutExt = `./components/${dirDivider}/${nameWithoutExt}`;
const tsxOldPath = `${pathWithoutExt}.tsx`;
const tsxNesPath = `${pathWithoutExt}.txt`;
return { tsxOldPath, tsxNesPath, pathWithoutExt };
}
startApp() {
const input = this.setInput();
this.startMainOperation(input);
}
startMainOperation({ tsxOldPath, tsxNesPath, pathWithoutExt }) {
this.getRepoHighLevelStructure();
rename(tsxOldPath, tsxNesPath, async () => {
console.log("STEP 1. Extension was changed");
const readTxt = this.readTxt(pathWithoutExt);
const splittedStrings = this.getSplittedStrings(readTxt);
const stringsWithoutEmptyLines =
this.getStringsWithoutEmptyLines(splittedStrings);
this.checkingForType(stringsWithoutEmptyLines);
this.restoreExtension(tsxNesPath, tsxOldPath);
});
}
getRepoHighLevelStructure() {
readdir(".", { withFileTypes: true }, (err, files) => {
if (err) {
console.log(err);
} else {
const fileNames = files.map(({ name }) => name);
const dirName = fileNames.filter((item) => !item.includes("."));
console.log("STEP 0.1. Get full root structure:", fileNames);
console.log("STEP 0.2. Get full root structure (dirs only):", dirName);
}
});
}
readTxt(pathWithoutExt) {
const readTxt = readFileSync(`${pathWithoutExt}.txt`, "utf-8");
console.log("STEP 2. Read txt:", readTxt);
return readTxt;
}
getSplittedStrings(readTxt) {
const splittedStrings = readTxt.split("\r\n");
console.log("STEP 3. Splitted strings:", splittedStrings);
return splittedStrings;
}
getStringsWithoutEmptyLines(splittedStrings) {
const stringsWithoutEmptyLines = splittedStrings.filter(
(item) => item !== ""
);
console.log(
"STEP 4. Strings without empty lines:",
stringsWithoutEmptyLines
);
return stringsWithoutEmptyLines;
}
checkingForType(stringsWithoutEmptyLines) {
const hasType = stringsWithoutEmptyLines.some((item) =>
item.includes(".Main")
);
console.log("STEP 5. Has type?", hasType);
}
restoreExtension(tsxNesPath, tsxOldPath) {
rename(tsxNesPath, tsxOldPath, () => {
console.log("STEP 6. Extension was restored");
});
}
}
const app = new App();
app.startApp();