-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.cjs
More file actions
307 lines (261 loc) · 9.43 KB
/
Copy pathclean.cjs
File metadata and controls
307 lines (261 loc) · 9.43 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const { execSync } = require("child_process");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function question(query) {
return new Promise((resolve) => rl.question(query, resolve));
}
async function cleanProject() {
console.log("\n🧹 Project Cleanup\n");
console.log("This will prepare the template for your own project.\n");
// 1. Compute project name
const projectName = path.basename(process.cwd());
const npmName = projectName
.toLowerCase()
.replace(/[^a-z0-9-~]/g, "-")
.replace(/^-+|-+$/g, "")
.replace(/-+/g, "-");
// 2. Load package.json
const packageJsonPath = path.join(process.cwd(), "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
packageJson.name = npmName;
packageJson.version = "1.0.0";
packageJson.description = "";
packageJson.keywords = [];
packageJson.author = "";
packageJson.repository = "";
// 3. Remove Git history and reinitialize automatically
console.log("Removing Git history...\n");
const gitPath = path.join(process.cwd(), ".git");
if (fs.existsSync(gitPath)) {
execSync(`rm -rf "${gitPath}"`, { stdio: "ignore" });
console.log("Deleted: .git/");
// Reinitialize git
try {
execSync("git init", { stdio: "ignore" });
console.log("Initialized: new git repository\n");
} catch (err) {
console.log("Warning: Could not initialize git repository\n");
}
}
// 4. Check if tests exist before asking
const hasTests =
fs.existsSync(path.join(process.cwd(), "jest.config.ts")) ||
fs.existsSync(path.join(process.cwd(), "tests")) ||
fs.existsSync(path.join(process.cwd(), "src", "tests")) ||
fs.existsSync(path.join(process.cwd(), "src", "__tests__")) ||
fs.existsSync(path.join(process.cwd(), "__tests__")) ||
(packageJson.devDependencies &&
(packageJson.devDependencies["jest"] ||
packageJson.devDependencies["@types/jest"] ||
packageJson.devDependencies["ts-jest"]));
let removeTests = false;
if (hasTests) {
const keepTests = await question("Keep tests? (y/n): ");
console.log("");
removeTests =
keepTests.toLowerCase().trim() !== "y" &&
keepTests.toLowerCase().trim() !== "yes";
} else {
console.log("No tests found, skipping test removal...\n");
}
if (removeTests) {
const testPackages = ["@jest/globals", "@types/jest", "jest", "ts-jest"];
testPackages.forEach((pkg) => {
if (packageJson.devDependencies?.[pkg]) {
delete packageJson.devDependencies[pkg];
}
});
if (packageJson.scripts) {
delete packageJson.scripts.test;
delete packageJson.scripts["test:watch"];
}
const jestConfigPath = path.join(process.cwd(), "jest.config.ts");
if (fs.existsSync(jestConfigPath)) {
fs.unlinkSync(jestConfigPath);
console.log("Deleted: jest.config.ts");
}
const testsPaths = [
path.join(process.cwd(), "tests"),
path.join(process.cwd(), "src", "tests"),
path.join(process.cwd(), "src", "__tests__"),
path.join(process.cwd(), "__tests__"),
];
for (const testsPath of testsPaths) {
if (fs.existsSync(testsPath)) {
execSync(`rm -rf "${testsPath}"`, { stdio: "ignore" });
console.log(`Deleted: ${path.relative(process.cwd(), testsPath)}/`);
}
}
const tsconfigPath = path.join(process.cwd(), "tsconfig.json");
if (fs.existsSync(tsconfigPath)) {
let tsconfigContent = fs.readFileSync(tsconfigPath, "utf8");
tsconfigContent = tsconfigContent.replace(/"jest",?/g, "");
fs.writeFileSync(tsconfigPath, tsconfigContent, "utf8");
}
}
// 5. Remove template-specific files
console.log("\nRemoving template-specific files...\n");
// Remove LICENSE
const licensePath = path.join(process.cwd(), "LICENSE");
if (fs.existsSync(licensePath)) {
fs.unlinkSync(licensePath);
console.log("Deleted: LICENSE");
}
// Remove FUNDING.yml
const fundingPath = path.join(process.cwd(), ".github", "FUNDING.yml");
if (fs.existsSync(fundingPath)) {
fs.unlinkSync(fundingPath);
console.log("Deleted: .github/FUNDING.yml");
// Remove .github folder if empty
const githubDir = path.join(process.cwd(), ".github");
if (fs.existsSync(githubDir) && fs.readdirSync(githubDir).length === 0) {
fs.rmdirSync(githubDir);
console.log("Deleted: .github/");
}
}
// Remove pnpm-lock.yaml
const lockPath = path.join(process.cwd(), "pnpm-lock.yaml");
if (fs.existsSync(lockPath)) {
fs.unlinkSync(lockPath);
console.log("Deleted: pnpm-lock.yaml");
}
// Remove assets folder
const assetsPath = path.join(process.cwd(), "assets");
if (fs.existsSync(assetsPath)) {
execSync(`rm -rf "${assetsPath}"`, { stdio: "ignore" });
console.log("Deleted: assets/");
}
// Remove example code (src/lib/math.ts)
const mathPath = path.join(process.cwd(), "src", "lib", "math.ts");
if (fs.existsSync(mathPath)) {
fs.unlinkSync(mathPath);
console.log("Deleted: src/lib/math.ts");
}
// Remove test files (math.test.ts in all possible locations)
const mathTestPaths = [
path.join(process.cwd(), "src", "tests", "math.test.ts"),
path.join(process.cwd(), "src", "__tests__", "math.test.ts"),
path.join(process.cwd(), "tests", "math.test.ts"),
path.join(process.cwd(), "__tests__", "math.test.ts"),
];
for (const testPath of mathTestPaths) {
if (fs.existsSync(testPath)) {
fs.unlinkSync(testPath);
console.log(`Deleted: ${path.relative(process.cwd(), testPath)}`);
}
}
// 6. Clean index.ts properly
const indexPath = path.join(process.cwd(), "src", "index.ts");
if (fs.existsSync(indexPath)) {
let indexContent = fs.readFileSync(indexPath, "utf8");
// Remove math imports
indexContent = indexContent.replace(
/import\s*\{[^}]*\}\s*from\s*["']@\/lib\/math\.js["'];?\s*/g,
""
);
// Remove math function calls
indexContent = indexContent.replace(
/console\.log\(add\([^)]*\)\);?\s*/g,
""
);
indexContent = indexContent.replace(
/console\.log\(sub\([^)]*\)\);?\s*/g,
""
);
indexContent = indexContent.replace(
/console\.log\(subtract\([^)]*\)\);?\s*/g,
""
);
// Remove any orphaned closing parentheses or semicolons
indexContent = indexContent.replace(/^\s*\);?\s*$/gm, "");
// Clean up multiple empty lines
indexContent = indexContent.replace(/\n{3,}/g, "\n\n");
// Trim and ensure single newline at end
indexContent = indexContent.trim();
// If file is empty or only has whitespace, add basic content
if (!indexContent || indexContent.length === 0) {
indexContent = 'console.log("Hello from Node TypeScript Starter!");';
}
fs.writeFileSync(indexPath, indexContent + "\n", "utf8");
console.log("Cleaned: src/index.ts");
}
// 7. Empty README.md completely (handle both README.MD and README.md)
const readmePath =
path.join(process.cwd(), "README.MD") ||
path.join(process.cwd(), "README.md");
if (fs.existsSync(readmePath)) {
// Delete the file first, then create new one
fs.unlinkSync(readmePath);
fs.writeFileSync(readmePath, `# ${projectName}\n`, "utf8");
console.log("Cleared: README.md");
}
// 8. Rename .env.example -> .env
const envExamplePath = path.join(process.cwd(), ".env.example");
const envPath = path.join(process.cwd(), ".env");
if (fs.existsSync(envExamplePath)) {
fs.renameSync(envExamplePath, envPath);
console.log("Renamed: .env.example → .env");
}
// 9. Update package.json scripts to use .env instead of .env.example
if (packageJson.scripts) {
Object.keys(packageJson.scripts).forEach((key) => {
packageJson.scripts[key] = packageJson.scripts[key].replace(
/\.env\.example/g,
".env"
);
});
}
// 10. Save updated package.json
fs.writeFileSync(
packageJsonPath,
JSON.stringify(packageJson, null, 2) + "\n",
"utf8"
);
console.log("Updated: package.json");
// 11. Delete this cleanup script
const cleanScriptPath = path.join(process.cwd(), "clean.ts");
if (fs.existsSync(cleanScriptPath)) {
fs.unlinkSync(cleanScriptPath);
console.log("Deleted: clean.ts (this script)");
// Remove clean script from package.json
if (packageJson.scripts?.clean) {
delete packageJson.scripts.clean;
fs.writeFileSync(
packageJsonPath,
JSON.stringify(packageJson, null, 2) + "\n",
"utf8"
);
}
}
// 12. Ask to install packages
const installPackages = await question("\nInstall packages? (y/n): ");
console.log("");
if (
installPackages.toLowerCase().trim() === "y" ||
installPackages.toLowerCase().trim() === "yes"
) {
console.log("Installing packages...\n");
execSync("pnpm install", { stdio: "inherit" });
}
console.log("\n✨ Cleanup complete! Your project is ready.\n");
console.log("What was removed:");
console.log(" ✓ Git history (reinitialized fresh repository)");
console.log(" ✓ LICENSE");
console.log(" ✓ FUNDING.yml");
console.log(" ✓ Example code (math.ts and math.test.ts)");
console.log(" ✓ Assets folder");
console.log(" ✓ Template README (cleared to just project title)");
console.log(" ✓ This cleanup script\n");
rl.close();
}
cleanProject().catch((err) => {
console.error("Error:", err.message);
rl.close();
process.exit(1);
});