-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete.js
77 lines (66 loc) · 2.26 KB
/
delete.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
// ==Headers==
// @Name: delete
// @Description: 删除漫画
// @Version: 1.0.196
// @Author: dodying
// @Modified: 2023-01-01 10:38:25
// @Namespace: https://github.com/dodying/Nodejs
// @SupportURL: https://github.com/dodying/Nodejs/issues
// @Require: readline-sync,jszip
// ==/Headers==
// usage: []file
// 设置
const _ = require('../config');
const extensions = ['.cbz', '.jpg', '.zip', '.png'];
// 导入原生模块
const fs = require('fs');
const path = require('path');
const deletedPath = path.join(_.libraryFolder, _.subFolderDelete);
if (!fs.existsSync(deletedPath)) fs.mkdirSync(deletedPath);
// 导入第三方模块
const readlineSync = require('readline-sync');
const JSZip = require('jszip');
// Function
// Main
const main = async () => {
const list = process.argv.slice(2).filter((i) => extensions.includes(path.parse(i).ext));
for (const i of list) {
const fullpath = path.resolve(process.cwd(), i);
const { name } = path.parse(fullpath);
if (!fullpath.match('ComicLibrary') && !readlineSync.keyInYNStrict('Continue to delete?')) continue;
if (!fs.existsSync(fullpath)) continue;
console.log((`\nFile:\t${fullpath}`));
const confirm = readlineSync.keyInYNStrict('Delete it?');
if (confirm) {
if (['.zip', '.cbz'].includes(path.extname(fullpath))) {
const pathnew = path.resolve(deletedPath, `${name}.cbz`);
try {
const targetData = fs.readFileSync(fullpath);
const jszip = new JSZip();
const zip = await jszip.loadAsync(targetData);
const fileList = Object.keys(zip.files).filter((i) => !i.match(/(info.txt|\/)$/));
for (const i of fileList) zip.remove(i);
const content = await zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: {
level: 9,
},
});
fs.writeFileSync(pathnew, content);
} catch (error) {
console.log(error);
fs.writeFileSync(pathnew, '');
}
}
fs.unlinkSync(fullpath);
}
}
};
main().then(async () => {
//
}, async (err) => {
console.error(err);
readlineSync.keyInPause();
process.exit();
});