-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
198 lines (166 loc) · 4.86 KB
/
Copy pathcli.js
File metadata and controls
198 lines (166 loc) · 4.86 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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const {
unlockFile,
deleteFile,
getProcessesUsingFile,
isRunningAsAdmin
} = require('./lib/native-windows-api');
// Função para desbloquear ou deletar arquivo
async function unlockOrDeleteFile(filePath, options = {}) {
try {
if (options.deleteFile) {
// Deletar arquivo
const result = await deleteFile(filePath, {
useRecycleBin: options.useRecycleBin
});
return result;
} else {
// Apenas desbloquear
const result = await unlockFile(filePath, {
killProcesses: options.forceKill
});
return result;
}
} catch (error) {
return { success: false, message: error.message };
}
}
// Função para mostrar ajuda
function showHelp() {
console.log(`
File Unlocker - CLI
Uso: npx file-unlocker <arquivo> [opções]
Argumentos:
arquivo Caminho do arquivo para desbloquear/deletar
Opções:
-d, --delete Deletar o arquivo após desbloquear
-r, --recycle-bin Mover para lixeira em vez de deletar permanentemente
-f, --force-kill Forçar desbloqueio matando processos se necessário
-h, --help Mostrar esta ajuda
-v, --version Mostrar versão
Exemplos:
npx file-unlocker "arquivo.txt" # Apenas desbloquear
npx file-unlocker "arquivo.txt" --delete # Desbloquear e deletar
npx file-unlocker "arquivo.txt" --recycle-bin # Mover para lixeira
npx file-unlocker "arquivo.txt" --force-kill # Forçar desbloqueio
Verificar processos que estão usando um arquivo:
npx file-unlocker "arquivo.txt" --check-only
`);
}
// Função para mostrar versão
function showVersion() {
const packageJson = require('./package.json');
console.log(`File Unlocker v${packageJson.version}`);
}
// Função principal
async function main() {
const args = process.argv.slice(2);
// Verificar se há argumentos
if (args.length === 0) {
console.error('Erro: Nenhum arquivo especificado');
showHelp();
process.exit(1);
}
// Parsear argumentos
const options = {
deleteFile: false,
useRecycleBin: false,
forceKill: false,
checkOnly: false
};
let filePath = null;
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case '-h':
case '--help':
showHelp();
process.exit(0);
break;
case '-v':
case '--version':
showVersion();
process.exit(0);
break;
case '-d':
case '--delete':
options.deleteFile = true;
break;
case '-r':
case '--recycle-bin':
options.useRecycleBin = true;
break;
case '-f':
case '--force-kill':
options.forceKill = true;
break;
case '--check-only':
options.checkOnly = true;
break;
default:
if (!filePath && !arg.startsWith('-')) {
filePath = arg;
} else {
console.error(`Erro: Argumento desconhecido: ${arg}`);
showHelp();
process.exit(1);
}
break;
}
}
// Verificar se o arquivo foi especificado
if (!filePath) {
console.error('Erro: Nenhum arquivo especificado');
showHelp();
process.exit(1);
}
// Resolver caminho absoluto
filePath = path.resolve(filePath);
// Verificar se o arquivo existe
if (!fs.existsSync(filePath)) {
console.error(`Error: File not found: ${filePath}`);
process.exit(1);
}
try {
// Verificar privilégios de administrador
if (process.platform === 'win32' && !isRunningAsAdmin()) {
console.warn('Aviso: Execute como administrador para melhor compatibilidade no Windows');
}
if (options.checkOnly) {
// Apenas verificar processos
const processes = await getProcessesUsingFile(filePath);
if (processes.length === 0) {
console.log('✅ Arquivo não está bloqueado');
} else {
console.log(`❌ Arquivo bloqueado por ${processes.length} processo(s):`);
processes.forEach(p => {
console.log(` - ${p.name} (PID: ${p.pid})`);
});
}
} else {
// Executar desbloqueio/deleção
console.log(`Processando: ${filePath}`);
const result = await unlockOrDeleteFile(filePath, options);
if (result.success) {
console.log(`✅ ${result.message}`);
process.exit(0);
} else {
console.error(`❌ ${result.message}`);
process.exit(1);
}
}
} catch (error) {
console.error('Erro:', error.message);
process.exit(1);
}
}
// Executar se for o arquivo principal
if (require.main === module) {
main().catch(error => {
console.error('Erro fatal:', error);
process.exit(1);
});
}
module.exports = { main };