-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstripper-cli.js
More file actions
212 lines (183 loc) · 6.72 KB
/
stripper-cli.js
File metadata and controls
212 lines (183 loc) · 6.72 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
#!/usr/bin/env node
/**
* Author: @Enoughsdv 2025
*/
const fs = require('node:fs');
const path = require('node:path');
const strip = require('strip-comments');
const args = process.argv.slice(2);
let srcDir, distDir, verbose = false;
let excludePatterns = [];
let excludeMinified = false;
const MARKER_FILE = '.stripper-cli-marker';
function showHelp() {
console.log(`
stripper-cli - Remove comments from JavaScript and TypeScript files
Usage:
stripper-cli -s <source> -d <destination> [options]
Required Arguments:
-s <path> Source directory to copy and clean
-d <path> Destination directory for cleaned files
Options:
--exclude <pattern> Exclude files/folders matching pattern (can be used multiple times)
--no-minified Exclude all .min.js files from processing
--verbose Show detailed output of processed files
--help Show this help message
Examples:
stripper-cli -s ./src -d ./dist
stripper-cli -s ./src -d ./dist --exclude "lib/" --verbose
stripper-cli -s ./src -d ./dist --exclude "*.test.js" --no-minified
Exclude Patterns:
- Use glob-like patterns: *, ?, folder/
- Examples: "lib/", "*.min.js", "vendor/*"
Note: You can also use 'node stripper-cli' or 'node stripper-cli.js' if not installed globally.
`);
}
// Show help if no arguments provided
if (args.length === 0) {
showHelp();
process.exit(0);
}
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '-s' || arg === '--source') {
srcDir = args[i + 1];
if (!srcDir || srcDir.startsWith('-')) {
console.error('Error: You must provide a valid source directory after -s');
process.exit(1);
}
i++;
} else if (arg === '-d' || arg === '--destination') {
distDir = args[i + 1];
if (!distDir || distDir.startsWith('-')) {
console.error('Error: You must provide a valid destination directory after -d');
process.exit(1);
}
i++;
} else if (arg === '--exclude') {
const pattern = args[i + 1];
if (!pattern || pattern.startsWith('-')) {
console.error('Error: You must provide a pattern after --exclude');
process.exit(1);
}
excludePatterns.push(pattern);
i++;
} else if (arg === '--no-minified') {
excludeMinified = true;
} else if (arg === '--verbose' || arg === '-v') {
verbose = true;
} else if (arg === '--help' || arg === '-h') {
showHelp();
process.exit(0);
} else {
console.error(`Error: Unknown argument "${arg}". Use --help for usage information.`);
process.exit(1);
}
}
if (!srcDir || !distDir) {
console.error('Error: You must provide both arguments -s <source_directory> and -d <destination_directory>');
console.error('Use --help for usage information.');
process.exit(1);
}
// Validate source directory
if (!fs.existsSync(srcDir)) {
console.error(`Error: Source directory "${srcDir}" does not exist.`);
process.exit(1);
}
if (!fs.statSync(srcDir).isDirectory()) {
console.error(`Error: Source path "${srcDir}" is not a directory.`);
process.exit(1);
}
// Convert to absolute paths
srcDir = path.resolve(srcDir);
distDir = path.resolve(distDir);
if (verbose) {
console.log(`Source: ${srcDir}`);
console.log(`Destination: ${distDir}`);
if (excludePatterns.length > 0) {
console.log(`Exclude patterns: ${excludePatterns.join(', ')}`);
}
if (excludeMinified) {
console.log('Excluding .min.js files');
}
console.log('---');
}
function copyDir(src, dest) {
fs.mkdirSync(dest, { recursive: true });
for (const file of fs.readdirSync(src)) {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);
const stat = fs.statSync(srcPath);
if (stat.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}
function matchesExcludePatterns(filePath) {
return excludePatterns.some(pattern => {
if (pattern.endsWith('/') || pattern.endsWith('\\')) {
const normPattern = pattern.replace(/\\/g, '/');
const normPath = filePath.replace(/\\/g, '/');
return normPath.startsWith(normPattern);
}
// Simple glob-like: supports * and ?
const regex = new RegExp('^' + pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.') + '$');
return regex.test(filePath);
});
}
let filesProcessed = 0;
let filesSkipped = 0;
function cleanJsInDir(dir) {
for (const file of fs.readdirSync(dir)) {
if (file === MARKER_FILE) continue;
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
cleanJsInDir(fullPath);
} else if (file.endsWith('.js') || file.endsWith('.ts')) {
const relPath = path.relative(distDir, fullPath).replace(/\\/g, '/');
// Check exclusions
if (excludeMinified && file.endsWith('.min.js')) {
filesSkipped++;
if (verbose) console.log(`Skipped (minified): ${relPath}`);
continue;
}
if (matchesExcludePatterns(relPath)) {
filesSkipped++;
if (verbose) console.log(`Skipped (excluded): ${relPath}`);
continue;
}
try {
const code = fs.readFileSync(fullPath, 'utf8');
const cleaned = strip(code, { preserveNewlines: false });
fs.writeFileSync(fullPath, cleaned, 'utf8');
filesProcessed++;
if (verbose) console.log(`Cleaned: ${relPath}`);
} catch (error) {
console.error(`Error processing ${relPath}: ${error.message}`);
filesSkipped++;
}
}
}
}
if (fs.existsSync(distDir)) {
const markerPath = path.join(distDir, MARKER_FILE);
if (!fs.existsSync(markerPath)) {
console.error(`Error: Destination directory "${distDir}" exists and was not created by this tool. Aborting to prevent data loss.`);
process.exit(1);
}
fs.rmSync(distDir, { recursive: true, force: true });
}
fs.mkdirSync(distDir, { recursive: true });
fs.writeFileSync(path.join(distDir, MARKER_FILE), 'This directory was created by stripper-cli. Safe to replace.', 'utf8');
copyDir(srcDir, distDir);
cleanJsInDir(distDir);
// Final summary
console.log('\n--- Summary ---');
console.log(`Files processed: ${filesProcessed}`);
if (filesSkipped > 0) {
console.log(`Files skipped: ${filesSkipped}`);
}
console.log(`✅ Operation completed successfully!`);