-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrename-component-assets.js
More file actions
65 lines (53 loc) · 2.17 KB
/
rename-component-assets.js
File metadata and controls
65 lines (53 loc) · 2.17 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
const fs = require('fs').promises
const path = require('path')
const glob = require('glob')
async function renameComponentAssets() {
try {
// Find all component asset files matching the pattern
const assetFiles = glob.sync('components/*/assets/*.component.{css,js}')
// Store old->new filename mappings for updating references
const fileMap = new Map()
// Rename the asset files
for (const oldPath of assetFiles) {
const dir = path.dirname(oldPath)
const filename = path.basename(oldPath)
const componentName = path.basename(path.dirname(path.dirname(oldPath)))
const extension = path.extname(filename)
const newFilename = `component.${componentName}${extension}`
const newPath = path.join(dir, newFilename)
console.log(`Renaming: ${oldPath} -> ${newPath}`)
await fs.rename(oldPath, newPath)
// Store the mapping of old name to new name (without extension for js files)
const oldName = filename.replace('.component.', '.')
fileMap.set(oldName, newFilename)
}
// Find all liquid component files
const liquidFiles = glob.sync('components/*/component.*.liquid')
// Update references in liquid files
for (const liquidFile of liquidFiles) {
let content = await fs.readFile(liquidFile, 'utf8')
let hasChanges = false
// Update references in the content
for (const [oldName, newName] of fileMap) {
// Handle both .js and no-extension cases for JavaScript files
const oldJsPattern = oldName.endsWith('.js') ? [oldName, oldName.replace('.js', '')] : [oldName]
for (const pattern of oldJsPattern) {
if (content.includes(pattern)) {
content = content.replace(new RegExp(pattern, 'g'), newName)
hasChanges = true
}
}
}
// Save the file if changes were made
if (hasChanges) {
console.log(`Updating references in: ${liquidFile}`)
await fs.writeFile(liquidFile, content, 'utf8')
}
}
console.log('Component asset renaming completed successfully!')
} catch (error) {
console.error('Error during renaming:', error)
process.exit(1)
}
}
renameComponentAssets()