-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
46 lines (39 loc) · 1.12 KB
/
cli.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
#!/usr/bin/env node
const fs = require('fs');
const yargs = require('yargs');
const removeUnused = require('./remove-unused');
const argv = yargs
.usage('Usage: $0 <files...> [options]')
.option('keepImports', {
alias: 'i',
type: 'array',
description: 'List of imports to keep',
})
.option('keepVars', {
alias: 'v',
type: 'array',
description: 'List of variables to keep',
})
.help('h')
.alias('h', 'help')
.demandCommand(1).argv;
const files = argv._;
const keepImports = argv.keepImports || [];
const keepVars = argv.keepVars || [];
function processFile(filePath, options) {
fs.readFile(filePath, 'utf-8', (err, fileContent) => {
if (err) {
console.error(`Error reading file ${filePath}:`, err);
return;
}
const newFileContent = removeUnused(fileContent, options);
fs.writeFile(filePath, newFileContent, (err) => {
if (err) {
console.error(`Error writing file ${filePath}:`, err);
return;
}
console.log(`Successfully processed ${filePath}`);
});
});
}
files.forEach((filePath) => processFile(filePath, { keepImports, keepVars }));