-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublic-clean-up.ts
More file actions
112 lines (95 loc) · 3.18 KB
/
public-clean-up.ts
File metadata and controls
112 lines (95 loc) · 3.18 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
import {existsSync, statSync, rmdirSync} from 'node:fs'
import {readdir} from 'node:fs/promises'
import path from 'path'
/**
* List of files to preserve during cleanup in the root directory
*/
const PRESERVED_FILES = [
'cv-vladimir-lazarev-engineering-director.pdf',
'mellon-for-incubators.pdf',
'mellon-prototype.pdf',
'og_image-min.jpg',
'robots.txt',
'.assetsignore'
]
/**
* List of directories to preserve during cleanup
*/
const PRESERVED_DIRS = ['icons', 'fonts']
/**
* Default public directory if not specified via environment
*/
const DEFAULT_PUBLIC_DIR = 'public'
/**
* Recursively deletes directory contents, preserving specified files and directories
* @param dirPath - Directory path to clean
* @param isRootPublic - Whether this is the root public directory
* @returns Promise resolving when deletion is complete
*/
async function deleteRecursively(dirPath: string, isRootPublic: boolean = false): Promise<void> {
if (!existsSync(dirPath)) return
const items = await readdir(dirPath)
const publicDir = process.env.PUBLIC || DEFAULT_PUBLIC_DIR
for (const item of items) {
const itemPath = path.join(dirPath, item)
const isDirectory = statSync(itemPath).isDirectory()
// Handle directories
if (isDirectory) {
// Skip preserved directories in root public
if (isRootPublic && PRESERVED_DIRS.includes(item)) {
console.log(`Keeping preserved directory: ${item}/`)
continue
}
// Delete non-preserved directories
await deleteRecursively(itemPath, false)
try {
rmdirSync(itemPath)
console.log(`Deleted directory: ${itemPath}`)
} catch (err) {
console.error(`Failed to delete directory ${itemPath}: ${err}`)
}
continue
}
// Preserve specified files in root public directory
if (isRootPublic && PRESERVED_FILES.includes(item)) {
console.log(`Keeping preserved file: ${item}`)
continue
}
// Delete other files
try {
const file = Bun.file(itemPath)
await file.delete()
console.log(`Deleted file: ${itemPath}`)
} catch (err) {
console.error(`Failed to delete file ${itemPath}: ${err}`)
}
}
}
/**
* Cleans up the public directory, preserving specified files and directories
*/
async function cleanupPublicDirectory(): Promise<void> {
// Get public directory from environment or use default
const publicDir = process.env.PUBLIC || DEFAULT_PUBLIC_DIR
// Set default if not already set
if (!process.env.PUBLIC) {
process.env.PUBLIC = DEFAULT_PUBLIC_DIR
console.log(`Using default PUBLIC: ${DEFAULT_PUBLIC_DIR}`)
}
// Validate public directory
if (!existsSync(publicDir)) {
console.error(`Directory does not exist: ${publicDir}`)
process.exit(1)
}
console.log(`Starting cleanup of ${publicDir}`)
console.log(`Preserving files: ${PRESERVED_FILES.join(', ')}`)
console.log(`Preserving directories: ${PRESERVED_DIRS.join(', ')}`)
// Perform cleanup
await deleteRecursively(publicDir, true)
console.log('Public directory cleanup completed')
}
// Execute cleanup
cleanupPublicDirectory().catch(err => {
console.error('Cleanup failed:', err)
process.exit(1)
})