qleaner 1.0.29 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +2 -2
- package/controllers/image.js +12 -16
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -47,7 +47,7 @@ async function loadChalk() {
|
|
|
47
47
|
)
|
|
48
48
|
.option("-t, --table", "Print the results in a table")
|
|
49
49
|
.option("-d, --dry-run", "Show what would be deleted without actually deleting (skips prompt)")
|
|
50
|
-
.option("-C, --clear-cache", "Clear the cache")
|
|
50
|
+
.option("-C, --clear-cache", "Clear the cache recommended after making code changes")
|
|
51
51
|
.action(async (path, options) => {
|
|
52
52
|
if(options.clearCache) {
|
|
53
53
|
clearCache(process.cwd());
|
|
@@ -75,7 +75,7 @@ async function loadChalk() {
|
|
|
75
75
|
)
|
|
76
76
|
.option("-t, --table", "Print the results in a table")
|
|
77
77
|
.option("-d, --dry-run", "Show what would be deleted without actually deleting (skips prompt)")
|
|
78
|
-
.option("-C, --clear-cache", "Clear the cache")
|
|
78
|
+
.option("-C, --clear-cache", "Clear the cache recommended after making code changes")
|
|
79
79
|
.action(async (directory, rootPath, options) => {
|
|
80
80
|
await getUnusedImages(chalk, directory, rootPath, options);
|
|
81
81
|
});
|
package/controllers/image.js
CHANGED
|
@@ -64,11 +64,7 @@ function extractFromJSXStyle(node, file, collected) {
|
|
|
64
64
|
if (!expr || expr.type !== "ObjectExpression") return;
|
|
65
65
|
|
|
66
66
|
expr.properties.forEach((prop) => {
|
|
67
|
-
if (
|
|
68
|
-
prop.type !== "ObjectProperty" ||
|
|
69
|
-
!prop.key ||
|
|
70
|
-
!prop.value
|
|
71
|
-
) return;
|
|
67
|
+
if (prop.type !== "ObjectProperty" || !prop.key || !prop.value) return;
|
|
72
68
|
|
|
73
69
|
const keyName = prop.key.name || prop.key.value;
|
|
74
70
|
|
|
@@ -111,7 +107,9 @@ async function getUnusedImages(chalk, imageDirectory, codeDirectory, options) {
|
|
|
111
107
|
const unusedImages = [];
|
|
112
108
|
|
|
113
109
|
// ---- Collect image files in asset directory ----
|
|
114
|
-
const imageFiles = await fg([
|
|
110
|
+
const imageFiles = await fg([
|
|
111
|
+
`${imageDirectory}/**/*.{png,jpg,jpeg,svg,gif,webp}`,
|
|
112
|
+
]);
|
|
115
113
|
|
|
116
114
|
// ---- Scan Code Files ----
|
|
117
115
|
const codeFiles = await fg([`${codeDirectory}/**/*.{js,jsx,ts,tsx}`]);
|
|
@@ -119,7 +117,7 @@ async function getUnusedImages(chalk, imageDirectory, codeDirectory, options) {
|
|
|
119
117
|
for (const file of codeFiles) {
|
|
120
118
|
index++;
|
|
121
119
|
console.clear();
|
|
122
|
-
console.log(
|
|
120
|
+
console.log("Scanning code files...", index, "of", codeFiles.length);
|
|
123
121
|
const code = fs.readFileSync(file, "utf8");
|
|
124
122
|
|
|
125
123
|
const ast = parser.parse(code, {
|
|
@@ -219,9 +217,7 @@ async function getUnusedImages(chalk, imageDirectory, codeDirectory, options) {
|
|
|
219
217
|
|
|
220
218
|
// detect url("...") inside strings (e.g., Tailwind)
|
|
221
219
|
const matches = [...val.matchAll(URL_EXTRACT_REGEX)];
|
|
222
|
-
matches.forEach((m) =>
|
|
223
|
-
used.add(JSON.stringify({ path: m[2], file }))
|
|
224
|
-
);
|
|
220
|
+
matches.forEach((m) => used.add(JSON.stringify({ path: m[2], file })));
|
|
225
221
|
},
|
|
226
222
|
|
|
227
223
|
/**
|
|
@@ -256,7 +252,7 @@ async function getUnusedImages(chalk, imageDirectory, codeDirectory, options) {
|
|
|
256
252
|
for (const entry of used) {
|
|
257
253
|
index++;
|
|
258
254
|
console.clear();
|
|
259
|
-
console.log(
|
|
255
|
+
console.log("Normalizing images...", index, "of", used.size);
|
|
260
256
|
const { path: p } = JSON.parse(entry);
|
|
261
257
|
const normalized = normalize(p, imageDirectory);
|
|
262
258
|
if (normalized) normalizedUsed.add(normalized);
|
|
@@ -267,7 +263,7 @@ async function getUnusedImages(chalk, imageDirectory, codeDirectory, options) {
|
|
|
267
263
|
for (const img of imageFiles) {
|
|
268
264
|
index++;
|
|
269
265
|
console.clear();
|
|
270
|
-
console.log(
|
|
266
|
+
console.log("Determining unused images...", index, "of", imageFiles.length);
|
|
271
267
|
const full = path.resolve(img);
|
|
272
268
|
if (!normalizedUsed.has(full)) {
|
|
273
269
|
unusedImages.push(full);
|
|
@@ -276,7 +272,9 @@ async function getUnusedImages(chalk, imageDirectory, codeDirectory, options) {
|
|
|
276
272
|
// ---- Output table or list ----
|
|
277
273
|
if (options.table) {
|
|
278
274
|
const table = new Table({
|
|
279
|
-
head: options.dryRun
|
|
275
|
+
head: options.dryRun
|
|
276
|
+
? ["Unused Images (Would Delete)"]
|
|
277
|
+
: ["Unused Images"],
|
|
280
278
|
colWidths: [100],
|
|
281
279
|
});
|
|
282
280
|
unusedImages.forEach((img) => table.push([img]));
|
|
@@ -288,9 +286,7 @@ async function getUnusedImages(chalk, imageDirectory, codeDirectory, options) {
|
|
|
288
286
|
// ---- deletion logic ----
|
|
289
287
|
if (options.dryRun) {
|
|
290
288
|
console.log(
|
|
291
|
-
chalk.cyan(
|
|
292
|
-
`\n[DRY RUN] Would delete ${unusedImages.length} file(s)`
|
|
293
|
-
)
|
|
289
|
+
chalk.cyan(`\n[DRY RUN] Would delete ${unusedImages.length} file(s)`)
|
|
294
290
|
);
|
|
295
291
|
} else if (unusedImages.length > 0) {
|
|
296
292
|
askDeleteFiles(unusedImages);
|