| 49 | |
| 50 | // 解析出备份数据 |
| 51 | async parse(): Promise<BackupData> { |
| 52 | const map = new Map<string, Partial<ScriptData> & ScriptBackupData>(); |
| 53 | const subscribe = new Map<string, Partial<SubscribeData> & SubscribeBackupData>(); |
| 54 | let files = await this.fs.list(); |
| 55 | |
| 56 | // 处理订阅 |
| 57 | files = await this.dealFile(files, async (file) => { |
| 58 | const { name } = file; |
| 59 | if (!name.endsWith(".user.sub.js")) { |
| 60 | return false; |
| 61 | } |
| 62 | const key = name.substring(0, name.length - 12); |
| 63 | const subData = { |
| 64 | source: <string>await this.getFileContent(file, false), |
| 65 | lastModificationDate: file.updatetime, |
| 66 | } satisfies Partial<SubscribeData> & SubscribeBackupData; |
| 67 | subscribe.set(key, subData); |
| 68 | return true; |
| 69 | }); |
| 70 | // 处理订阅options |
| 71 | files = await this.dealFile(files, async (file) => { |
| 72 | const { name } = file; |
| 73 | if (!name.endsWith(".user.sub.options.json")) { |
| 74 | return false; |
| 75 | } |
| 76 | const key = name.substring(0, name.length - 22); |
| 77 | const data = <SubscribeOptionsFile>await this.getFileContent(file, true); |
| 78 | subscribe.get(key)!.options = data; |
| 79 | return true; |
| 80 | }); |
| 81 | |
| 82 | // 先处理*.user.js文件 |
| 83 | files = await this.dealFile(files, async (file) => { |
| 84 | const { name } = file; |
| 85 | if (!name.endsWith(".user.js")) { |
| 86 | return false; |
| 87 | } |
| 88 | // 遍历与脚本同名的文件 |
| 89 | const key = name.substring(0, name.length - 8); |
| 90 | const backupData = { |
| 91 | code: <string>await this.getFileContent(file, false), |
| 92 | storage: { data: {}, ts: 0 }, |
| 93 | requires: [], |
| 94 | requiresCss: [], |
| 95 | resources: [], |
| 96 | lastModificationDate: file.updatetime, |
| 97 | } satisfies Partial<ScriptData> & ScriptBackupData; |
| 98 | map.set(key, backupData); |
| 99 | return true; |
| 100 | }); |
| 101 | // 处理options.json文件 |
| 102 | files = await this.dealFile(files, async (file) => { |
| 103 | const { name } = file; |
| 104 | if (!name.endsWith(".options.json")) { |
| 105 | return false; |
| 106 | } |
| 107 | const key = name.substring(0, name.length - 13); |
| 108 | const data = <ScriptOptionsFile>await this.getFileContent(file, true); |