-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcssOrganizer.js
More file actions
281 lines (242 loc) · 8.47 KB
/
cssOrganizer.js
File metadata and controls
281 lines (242 loc) · 8.47 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
import css from "./parseAndStringify.js"
import { readFileSync } from "fs"
import fsp from "fs/promises"
import path, { join } from "path"
// Sorts a rules group based on properties
function customSort(declarations) {
return declarations.sort(
(a, b) => allProps.indexOf(a.property) - allProps.indexOf(b.property)
)
}
function sortStyleBlock(styleContent) {
const parsed = css.parse(styleContent)
parsed.stylesheet.rules.forEach((rule) => {
if (rule?.declarations) rule.declarations = customSort(rule.declarations)
})
return css.stringify(parsed)
}
async function replaceStyleInVueSFC(filePath, vueFileName = "xyz.vue") {
try {
// Read the current content of the Vue file
let htmlContent = await fsp.readFile(filePath, "utf-8")
// Search for the <style> tag
const styleStart = htmlContent.search("<style.*>")
if (styleStart === -1) return
const styleStartString = htmlContent.match("<style.*>")[0]
const styleStartStringLength = styleStartString.length
const styleEnd = htmlContent.indexOf("</style>")
// Check if <style> was found
if (styleStart !== -1 && styleEnd !== -1) {
// Extract the content of the <style> tag
const styleContent = htmlContent.substring(
styleStart + styleStartStringLength,
styleEnd
)
// Sort the style content
const sortedStyleContent = "\n" + sortStyleBlock(styleContent) + "\n"
// Replace the current content of the <style> tag with the new content
htmlContent =
htmlContent.substring(0, styleStart + styleStartStringLength) +
sortedStyleContent +
htmlContent.substring(styleEnd)
// Write the updated content back to the file
await fsp.writeFile(filePath, htmlContent, "utf-8")
if (config.showLogFiles)
console.log(
"--> File \x1b[33m" + vueFileName + "\x1b[0m has been edited"
)
} else {
// <style> not found
console.error(
"\x1b[31mThe content of " + vueFileName + " was not found.\x1b[0m"
)
}
} catch (error) {
console.error("Error updating the file " + vueFileName, error.message)
}
}
async function processVueFilesRecursively(folderPath) {
try {
// Read all filenames in the folder
const fileNames = await fsp.readdir(folderPath)
// Filter only files with the extension ".vue"
const vueFiles = fileNames.filter(
(fileName) => path.extname(fileName) === ".vue"
)
// Process each Vue file in the current folder
for (const vueFile of vueFiles) {
const filePath = path.join(folderPath, vueFile)
await replaceStyleInVueSFC(filePath, vueFile)
}
// Also search through all subfolders
for (const fileName of fileNames) {
const filePath = path.join(folderPath, fileName)
const stats = await fsp.stat(filePath)
if (stats.isDirectory()) {
// Recursively process subfolders
await processVueFilesRecursively(filePath)
}
}
if (config.showLogFolders) {
console.log(
`====> Folder \x1b[33m${folderPath}\x1b[0m has been processed.`
)
console.log(
`- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - `
)
}
} catch (error) {
console.error("\x1b[31mError processing Vue files:\x1b[0m", error.message)
}
}
async function processCssFilesRecursively(folderPath) {
try {
// Read all filenames in the folder
const fileNames = await fsp.readdir(folderPath)
// Filter only files with the extension ".css"
const cssFiles = fileNames.filter(
(fileName) => path.extname(fileName) === ".css"
)
// Process each CSS file in the current folder
for (const cssFile of cssFiles) {
const filePath = path.join(folderPath, cssFile)
// Read the CSS file
const styleContent = await fsp.readFile(filePath, "utf-8")
// Sort the style blocks
const sortedStyleContent = sortStyleBlock(styleContent)
// Write the updated content back to the file
await fsp.writeFile(filePath, sortedStyleContent, "utf-8")
if (config.showLogFiles)
console.log("--> File \x1b[33m" + cssFile + "\x1b[0m has been edited")
}
// Also search through all subfolders
for (const fileName of fileNames) {
const filePath = path.join(folderPath, fileName)
const stats = await fsp.stat(filePath)
if (stats.isDirectory()) {
// Recursively process subfolders
await processCssFilesRecursively(filePath)
}
}
if (config.showLogFolders) {
console.log(
`====> Folder \x1b[33m${folderPath}\x1b[0m has been processed.`
)
console.log(
`- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - `
)
}
} catch (error) {
console.error("\x1b[31mError processing CSS files:\x1b[0m", error.message)
}
}
function organizeVuePart(config, projectRoot) {
console.log("---------------------------------------------------------------")
console.log("CSSOrganizer - START - Vue Files")
// Construct the absolute path
const vueAbsolutePath = join(projectRoot, config.vueFolderPath)
processVueFilesRecursively(vueAbsolutePath)
.then(() =>
console.log("\x1b[32mVue Process completed successfully\x1b[0m")
)
.catch((error) =>
console.error("\x1b[31mError processing Vue:\x1b[0m", error)
)
.finally(() => {
console.log("CSSOrganizer - END - Vue Files")
console.log(
"---------------------------------------------------------------"
)
if (config.sortCssFiles) {
organizeCssPart(config, projectRoot)
}
})
}
function organizeCssPart(config, projectRoot) {
console.log("---------------------------------------------------------------")
console.log("CSSOrganizer - START - CSS Files")
// Construct the absolute path
const cssAbsolutePath = join(projectRoot, config.cssFolderPath)
processCssFilesRecursively(cssAbsolutePath)
.then(() =>
console.log("\x1b[32mCSS Process completed successfully\x1b[0m")
)
.catch((error) =>
console.error("\x1b[31mError processing CSS:\x1b[0m", error)
)
.finally(() => {
console.log("CSSOrganizer - END - CSS Files")
console.log(
"---------------------------------------------------------------"
)
})
}
function runCssOrganizer(config, projectRoot) {
console.log("\x1b[33mPath to project root\x1b[0m", projectRoot)
if (config.sortVueFiles) {
organizeVuePart(config, projectRoot)
} else if (config.sortCssFiles) {
organizeCssPart(config, projectRoot)
}
}
// Navigate to the root directory of the project
// Support --root <path> or -r <path> as CLI argument
const args = process.argv.slice(2)
const rootArgIndex = args.findIndex((a) => a === "--root" || a === "-r")
let projectRoot
if (rootArgIndex !== -1 && args[rootArgIndex + 1]) {
projectRoot = path.resolve(args[rootArgIndex + 1])
} else {
projectRoot = process.cwd().split("node_modules")[0]
if (projectRoot.endsWith("/")) {
projectRoot = projectRoot.slice(0, -1)
}
}
// Get grouping settings
let grouping = null
try {
grouping = JSON.parse(readFileSync(projectRoot + "/grouping.cssorg.json"))
} catch {
try {
grouping = JSON.parse(readFileSync("./grouping.json"))
} catch {
console.log(
"\x1b[47m\x1b[31m# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
)
console.error(
"# Could not load grouping file. #"
)
console.error(
"# Please add a valid grouping.cssorg.json #"
)
console.log("# Add the file to your root folder: ./grouping.cssorg.json #")
console.log(
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\x1b[0m"
)
process.exit(0)
}
}
// Get config
let config = null
try {
config = JSON.parse(readFileSync(projectRoot + "/config.cssorg.json"))
} catch {
try {
config = JSON.parse(readFileSync("./config.json"))
} catch {
console.log(
"\x1b[47m\x1b[31m# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
)
console.error("# Could not load config file. #")
console.error("# Please add a valid config.cssorg.json #")
console.log("# Add the file to your root folder: ./config.cssorg.json #")
console.log(
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\x1b[0m"
)
process.exit(0)
}
}
// Merge all CSS properties
const allProps = grouping.groups.map((group) => group.properties).flat()
// Init and start
runCssOrganizer(config, projectRoot)