-
Notifications
You must be signed in to change notification settings - Fork 7
/
update-import.js
264 lines (221 loc) · 8.93 KB
/
update-import.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// const fs = require("fs");
// const path = require("path");
// const directoryPath = path.join(__dirname, "src/features/vistaar/src/Views");
// fs.readdir(directoryPath, (err, files) => {
// if (err) {
// return console.error("Error reading directory:", err);
// }
// files.forEach((file) => {
// if (file.endsWith(".jsx") || file.endsWith(".js")) {
// const filePath = path.join(directoryPath, file);
// fs.readFile(filePath, "utf8", (err, data) => {
// if (err) {
// return console.error(`Error reading file ${file}:`, err);
// }
// let updatedData = data;
// let matchCount = 0; // Count the number of matches found in the file
// let importsAdded = {}; // To keep track of added imports
// // Regular expression to find all image src with require
// const regex =
// /<img\s+([^>]*)src={require\(["'](.+?)["']\)}([^>]*)\/?>/g;
// let match;
// while ((match = regex.exec(data)) !== null) {
// const [fullMatch, beforeSrc, imagePath, afterSrc] = match;
// const imageName = path.basename(imagePath);
// // Check if import statement already exists
// if (!importsAdded[imageName]) {
// const importStatement = `import ${imageName.replace(
// /\..+$/,
// ""
// )} from '${imagePath}';\n`;
// // Add import statement at the top
// updatedData = importStatement + updatedData;
// importsAdded[imageName] = true; // Mark import as added
// }
// // Replace img tag with the correct src, retaining all other attributes
// updatedData = updatedData.replace(
// fullMatch,
// `<img ${beforeSrc} src={${imageName.replace(
// /\..+$/,
// ""
// )}} ${afterSrc}>`
// );
// matchCount++;
// }
// if (matchCount > 0) {
// // Only write the file if there were matches found
// fs.writeFile(filePath, updatedData, "utf8", (err) => {
// if (err) {
// return console.error(`Error writing file ${file}:`, err);
// }
// console.log(`Updated ${file}`);
// });
// } else {
// console.log(`No updates needed for ${file}`);
// }
// });
// }
// });
// });
// for image
const fs = require("fs");
const path = require("path");
const directoryPath = path.join(__dirname, "src/features/default/src");
// Function to recursively read all files in a directory and its subdirectories
const readFilesRecursively = (dir, fileCallback) => {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
readFilesRecursively(filePath, fileCallback);
} else {
fileCallback(filePath);
}
});
};
readFilesRecursively(directoryPath, (filePath) => {
if (filePath.endsWith(".jsx") || filePath.endsWith(".js")) {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
return console.error(`Error reading file ${filePath}:`, err);
}
let updatedData = data;
let matchCount = 0; // Count the number of matches found in the file
let importsAdded = {}; // To keep track of added imports
// Regular expression to find all image src with require
const regex = /<img\s+([^>]*)src={require\(["'](.+?)["']\)}([^>]*)\/?>/g;
let match;
while ((match = regex.exec(data)) !== null) {
const [fullMatch, beforeSrc, imagePath, afterSrc] = match;
const imageName = path.basename(imagePath);
// Check if import statement already exists
if (!importsAdded[imageName]) {
const importStatement = `import ${imageName.replace(
/\..+$/,
""
)} from '${imagePath}';\n`;
// Add import statement at the top
updatedData = importStatement + updatedData;
importsAdded[imageName] = true; // Mark import as added
}
// Replace img tag with the correct src, retaining only style and alt attributes
updatedData = updatedData.replace(
fullMatch,
`<img ${beforeSrc} src={${imageName.replace(
/\..+$/,
""
)}} ${afterSrc}>`
);
matchCount++;
}
if (matchCount > 0) {
// Only write the file if there were matches found
fs.writeFile(filePath, updatedData, "utf8", (err) => {
if (err) {
return console.error(`Error writing file ${filePath}:`, err);
}
console.log(`Updated ${filePath}`);
});
} else {
console.log(`No updates needed for ${filePath}`);
}
});
}
});
// for context path update
// const fs = require("fs");
// const path = require("path");
// const directoryPath = path.join(__dirname, "src/features/vistaar/src");
// // Function to recursively read all files in a directory and its subdirectories
// const readFilesRecursively = (dir, fileCallback) => {
// fs.readdirSync(dir).forEach((file) => {
// const filePath = path.join(dir, file);
// if (fs.statSync(filePath).isDirectory()) {
// readFilesRecursively(filePath, fileCallback);
// } else {
// fileCallback(filePath);
// }
// });
// };
// readFilesRecursively(directoryPath, (filePath) => {
// if (filePath.endsWith(".jsx") || filePath.endsWith(".js")) {
// fs.readFile(filePath, "utf8", (err, data) => {
// if (err) {
// return console.error(`Error reading file ${filePath}:`, err);
// }
// let updatedData = data;
// let matchCount = 0; // Count the number of matches found in the file
// // Define patterns to replace and their replacement
// const replacementMap = {
// "../Contexts/FarmStackContext":
// "common/components/context/VistaarContext/FarmStackProvider",
// "../../Contexts/FarmStackContext":
// "common/components/context/VistaarContext/FarmStackProvider",
// "../../../Contexts/FarmStackContext":
// "common/components/context/VistaarContext/FarmStackProvider",
// "../../../../Contexts/FarmStackContext":
// "common/components/context/VistaarContext/FarmStackProvider",
// };
// // Perform replacements
// Object.keys(replacementMap).forEach((pattern) => {
// const regex = new RegExp(pattern.replace(/\.\.\//g, "\\.{2}/"), "g"); // Escape dots for regex, then create regex
// if (regex.test(updatedData)) {
// updatedData = updatedData.replace(regex, replacementMap[pattern]);
// matchCount++;
// }
// });
// // Continue with your original file modification logic here if necessary...
// if (matchCount > 0) {
// // Only write the file if there were matches found
// fs.writeFile(filePath, updatedData, "utf8", (err) => {
// if (err) {
// return console.error(`Error writing file ${filePath}:`, err);
// }
// console.log(`Updated ${filePath}`);
// });
// } else {
// console.log(`No updates needed for ${filePath}`);
// }
// });
// }
// });
// const fs = require("fs");
// const path = require("path");
// const directoryPath = path.join(__dirname, "src/features/default/src");
// // Function to recursively read all files in a directory and its subdirectories
// const readFilesRecursively = (dir, fileCallback) => {
// fs.readdirSync(dir).forEach((file) => {
// const filePath = path.join(dir, file);
// if (fs.statSync(filePath).isDirectory()) {
// readFilesRecursively(filePath, fileCallback);
// } else {
// fileCallback(filePath);
// }
// });
// };
// readFilesRecursively(directoryPath, (filePath) => {
// if (filePath.endsWith(".jsx") || filePath.endsWith(".js")) {
// fs.readFile(filePath, "utf8", (err, data) => {
// if (err) {
// return console.error(`Error reading file ${filePath}:`, err);
// }
// // Define the pattern for import statements to replace and their precise replacement
// const regex =
// /import\s+\{\s*FarmStackContext\s*\}\s+from\s+["']((?:\.\.\/)+)(?:[\w\/]+)?Contexts\/FarmStackContext["'];/g;
// // Replacement path
// const replacement = `import { FarmStackContext } from "common/components/context/DefaultContext/FarmStackProvider";`;
// // Perform the replacement
// const updatedData = data.replace(regex, replacement);
// if (updatedData !== data) {
// // Only write the file if changes were made
// fs.writeFile(filePath, updatedData, "utf8", (err) => {
// if (err) {
// return console.error(`Error writing file ${filePath}:`, err);
// }
// console.log(`Updated ${filePath}`);
// });
// } else {
// console.log(`No updates needed for ${filePath}`);
// }
// });
// }
// });