-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrefactorPaths.js
More file actions
139 lines (119 loc) · 4.65 KB
/
Copy pathrefactorPaths.js
File metadata and controls
139 lines (119 loc) · 4.65 KB
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
const fs = require('fs');
const path = require('path');
const mappings = {
// public
"'pages/home'": "'pages/public/home'",
"'pages/aboutUs'": "'pages/public/aboutUs'",
"'pages/contact'": "'pages/public/contact'",
"'pages/services'": "'pages/public/services'",
"'pages/service'": "'pages/public/service'",
"'pages/providers'": "'pages/public/providers'",
"'pages/registerService'": "'pages/public/registerService'",
// auth
"'pages/login'": "'pages/auth/login'",
"'pages/register'": "'pages/auth/register'",
"'pages/forgot-password'": "'pages/auth/forgot-password'",
"'pages/reset-password'": "'pages/auth/reset-password'",
"'pages/verify-otp'": "'pages/auth/verify-otp'",
// booking
"'pages/booking'": "'pages/booking/index'",
"'pages/booking-confirm'": "'pages/booking/confirm'",
"'pages/booking-details'": "'pages/booking/details'",
"'pages/my-bookings'": "'pages/booking/my-bookings'",
// payment
"'pages/payment-success'": "'pages/payment/success'",
"'pages/success'": "'pages/payment/status'",
// support
"'pages/chatbot'": "'pages/support/chatbot'",
"'pages/complaints'": "'pages/support/complaints'",
"'pages/feedback'": "'pages/support/feedback'",
// dashboards
"'pages/customerDashboard'": "'pages/customer/dashboard'",
"'pages/providerDashboard'": "'pages/provider/dashboard'"
};
// Also support double quotes
const doubleMappings = {};
for (const [key, value] of Object.entries(mappings)) {
doubleMappings[key.replace(/'/g, '"')] = value.replace(/'/g, '"');
}
// Also support backticks
const backtickMappings = {};
for (const [key, value] of Object.entries(mappings)) {
backtickMappings[key.replace(/'/g, '`')] = value.replace(/'/g, '`');
}
const allMappings = { ...mappings, ...doubleMappings, ...backtickMappings };
function walkSync(dir, callback, extension) {
if (!fs.existsSync(dir)) return;
const files = fs.readdirSync(dir);
for (const file of files) {
const filepath = path.join(dir, file);
const stat = fs.statSync(filepath);
if (stat.isDirectory()) {
walkSync(filepath, callback, extension);
} else if (filepath.endsWith(extension)) {
callback(filepath);
}
}
}
// 1. Refactor JS files in controllers/ and routes/
const jsDirs = [
path.join(__dirname, 'controllers'),
path.join(__dirname, 'routes'),
path.join(__dirname, 'app.js'),
path.join(__dirname, 'index.js')
];
jsDirs.forEach(dir => {
if (!fs.existsSync(dir)) return;
if (fs.statSync(dir).isFile()) {
processJsFile(dir);
return;
}
walkSync(dir, processJsFile, '.js');
});
function processJsFile(filepath) {
let content = fs.readFileSync(filepath, 'utf8');
let changed = false;
for (const [oldPath, newPath] of Object.entries(allMappings)) {
if (content.includes(oldPath)) {
content = content.split(oldPath).join(newPath);
changed = true;
}
}
if (changed) {
fs.writeFileSync(filepath, content, 'utf8');
console.log(`Updated res.render paths in ${filepath}`);
}
}
// 2. Refactor EJS includes in views/pages
// All files in views/pages/* are now one level deeper, so we must replace:
// include('../components/ => include('../../components/
// include('../includes/ => include('../../includes/
// include('../layouts/ => include('../../layouts/
// include('./ => include('../
const pagesDir = path.join(__dirname, 'views', 'pages');
walkSync(pagesDir, processEjsFile, '.ejs');
function processEjsFile(filepath) {
let content = fs.readFileSync(filepath, 'utf8');
let changed = false;
// This handles basic string replacements for common paths
const includeReplacements = [
{ from: "include('../components/", to: "include('../../components/" },
{ from: 'include("../components/', to: 'include("../../components/' },
{ from: "include('../includes/", to: "include('../../includes/" },
{ from: 'include("../includes/', to: 'include("../../includes/' },
{ from: "include('../layouts/", to: "include('../../layouts/" },
{ from: 'include("../layouts/', to: 'include("../../layouts/' },
// Sometimes they do <%- include('partials/header') %> - we don't have partials dir but just in case
];
for (const r of includeReplacements) {
if (content.includes(r.from)) {
content = content.split(r.from).join(r.to);
changed = true;
}
}
if (changed) {
fs.writeFileSync(filepath, content, 'utf8');
console.log(`Updated EJS includes in ${filepath}`);
}
}
console.log("Refactoring complete.");