forked from jaredly/stylecleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessFile.js
125 lines (108 loc) · 3.16 KB
/
processFile.js
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
const fs = require('fs');
const analyzeFile = require('./analyzeFile');
const removeUnused = require('./removeUnused');
const chalk = require('chalk');
const pos = (loc) => `${loc.start.line}:${loc.start.column}`;
const showWarning = (warning) => {
switch (warning.type) {
case 'reference':
console.log(
`> Non-standard reference to stylesheet variable at ${pos(
warning.loc,
)}`,
);
console.log(warning.code);
break;
case 'computed':
console.log(
`> Computed access to stylesheet variable at ${pos(warning.loc)}`,
);
console.log(warning.code);
break;
default:
throw new Error('Unexpected warning type: ' + warning.type);
}
};
const showCode = (code, loc) => {
if (code.indexOf('\n') !== -1) return console.log(code);
console.log(
code.slice(0, loc.start.column) +
chalk.red(code.slice(loc.start.column, loc.end.column)) +
code.slice(loc.end.column),
);
};
const showMissing = ({ key, loc, code }) => {
console.log(`Missing style '${key}' at ${pos(loc)}`);
showCode(code, loc);
};
const showUnused = ({ key, loc, code }) => {
console.log(`Unused style declaration '${key}' at ${pos(loc)}`);
// showCode(code, loc);
};
const showSheet = ({ warnings, missing, unused }) => {
if (warnings.length) {
console.log(chalk.bold.red('Warnings ⚠️'));
warnings.forEach(showWarning);
}
if (missing.length) {
console.log(chalk.bold.red('Missing styles 🔍'));
missing.forEach(showMissing);
}
if (unused.length) {
console.log(chalk.bold.red('Unused styles 🤔'));
unused.forEach(showUnused);
}
};
const processFile = (file, cmd) => {
const { sheets, lines } = analyzeFile(file);
if (!sheets.length) {
return { removed: 0, skipped: [], hasIssue: [] };
}
if (cmd === 'check') {
if (sheets.some((sheet) => sheet.missing.length || sheet.unused.length)) {
console.log(chalk.bold.blue('File: ' + file));
sheets.forEach(showSheet);
console.log('\n');
}
return {
removed: 0,
skipped: [],
hasIssue: sheets.filter(
(sheet) => sheet.missing.length || sheet.unused.length,
),
};
} else {
const toRemove = [];
let skipped = 0;
sheets.forEach((sheet) => {
if (!sheet.unused.length) return;
if (sheet.warnings.length) {
if (cmd === 'fix') {
skipped += sheet.unused.length;
return;
}
}
toRemove.push(...sheet.unused);
});
if (skipped === 0 && !toRemove.length) {
return { removed: 0, skipped: [], hasIssue: [] };
}
console.log(chalk.bold.blue('File: ' + file));
if (skipped > 0) {
console.log(
`Not removing ${skipped} potentially unused styles - use 'check' to view warnings or use 'fix-force'`,
);
}
if (toRemove.length) {
const fixed = removeUnused(lines, toRemove);
fs.writeFileSync(file, fixed.join('\n'));
console.log(`Removed ${toRemove.length} unused styles`);
}
return {
removed: toRemove.length,
skipped: skipped > 0 ? [{ file, count: skipped }] : [],
hasIssue: [],
};
}
};
module.exports = processFile;