这是一个非常必要的工具脚本。因为一旦开发过程中 App 崩溃或者调试中断,文件夹就会保持在“拒绝写入”的状态,连你自己都删不掉它。
这个脚本作为一个独立的 Node.js 运维脚本,不依赖 Electron,直接在终端运行即可救急。
🛠️ 脚本:reset-lock.js
在你的项目根目录下创建一个名为 reset-lock.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
|
const { exec } = require('child_process'); const path = require('path'); const fs = require('fs'); const readline = require('readline');
const isWindows = process.platform === 'win32';
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
function unlockDirectory(targetPath) { const absolutePath = path.resolve(targetPath.trim()); if (!fs.existsSync(absolutePath)) { console.error(`\n❌ 错误:找不到路径 "${absolutePath}"`); process.exit(1); }
console.log(`\n🛡️ 正在尝试解锁: ${absolutePath}`); console.log(`💻 操作系统: ${isWindows ? 'Windows' : 'macOS/Linux'}`);
let cmd = '';
if (isWindows) { cmd = `icacls "${absolutePath}" /remove:d Everyone /T /C /Q`; } else { cmd = `chmod -R u+w "${absolutePath}"`; }
console.log(`⚙️ 执行命令: ${cmd}`);
exec(cmd, (error, stdout, stderr) => { if (error) { console.error(`\n❌ 解锁失败!`); console.error(`原因: ${error.message}`); console.error(`提示: 如果文件夹在系统保护目录,请尝试以【管理员身份】运行此脚本。`); } else { console.log(`\n✅ 解锁成功!`); console.log(`📁 你现在应该可以正常删除或修改 "${absolutePath}" 了。`); } process.exit(0); }); }
const args = process.argv.slice(2);
if (args.length > 0) { unlockDirectory(args[0]); } else { rl.question('请输入被锁定的文件夹路径: ', (answer) => { if (!answer) { console.log('未输入路径,退出。'); process.exit(0); } unlockDirectory(answer); rl.close(); }); }
|
📖 使用方法
方式 1:交互式运行(推荐)
- 打开终端(在 VS Code 或 CMD 中)。
- 运行命令:
- 脚本会提示你输入路径,把被锁的文件夹拖进终端(或者复制路径粘贴进去),回车即可。
方式 2:命令行参数运行
直接把路径跟在后面:
1 2 3 4 5
| node reset-lock.js "C:\Users\Admin\Desktop\TestFolder"
node reset-lock.js ./TestFolder
|
💡 为什么需要这个脚本?
你在前面的代码中使用了 icacls ... /deny Everyone。这是一个非常强力的系统级指令。
如果你的 Electron App 在开发时:
- 热更新(HMR)导致主进程重启,但没来得及触发
will-quit。
- 你手动点了 VS Code 的“停止调试”按钮(这属于强制杀进程,不会触发
will-quit)。
- 代码报错导致主进程 Crash。
在这些情况下,unlockProject() 永远不会被执行。那个文件夹就会像“钉子户”一样留在你的磁盘上,删也删不掉,改也改不了。这时只有运行这个脚本才能“解救”它。
Prev: Windows (icacls) 这个方案 有文件夹node_modle,.git这种限制吗
Next: 独占式文件锁定1