(script: ScriptBackupData)
| 24 | } |
| 25 | |
| 26 | writeScript(script: ScriptBackupData): Promise<void>[] { |
| 27 | const { name } = script.options!.meta; |
| 28 | // 将脚本名中的特殊字符替换为下划线 |
| 29 | const filename = name.replace(/[\\/\\:*?"<>|]/g, "_"); |
| 30 | |
| 31 | // 写脚本文件 |
| 32 | const writeCode = script.code; |
| 33 | |
| 34 | // 写入脚本options.json |
| 35 | const writeOptions = JSON.stringify(script.options); |
| 36 | |
| 37 | // 写入脚本storage.json |
| 38 | // 不想兼容tm的导出规则了,直接写入storage.json |
| 39 | const storage = { ...script.storage }; |
| 40 | const data = storage.data; |
| 41 | for (const key of Object.keys(data)) { |
| 42 | data[key] = toStorageValueStr(data[key]); |
| 43 | } |
| 44 | const wrtieStorage = JSON.stringify(storage); |
| 45 | |
| 46 | const fileOpts = { modifiedDate: script.lastModificationDate } as FileCreateOptions; |
| 47 | const fileOptsStorage = { modifiedDate: storage.ts || script.lastModificationDate } as FileCreateOptions; |
| 48 | return [ |
| 49 | // 写脚本文件 |
| 50 | this.fs.create(`${filename}.user.js`, fileOpts).then((fileWriter) => fileWriter.write(writeCode)), |
| 51 | // 写入脚本options.json |
| 52 | this.fs.create(`${filename}.options.json`, fileOpts).then((fileWriter) => fileWriter.write(writeOptions)), |
| 53 | // 写入脚本storage.json |
| 54 | this.fs.create(`${filename}.storage.json`, fileOptsStorage).then((fileWriter) => fileWriter.write(wrtieStorage)), |
| 55 | // 写入脚本资源文件 |
| 56 | ...this.writeResource(filename, script.resources, "resources", fileOpts), |
| 57 | ...this.writeResource(filename, script.requires, "requires", fileOpts), |
| 58 | ...this.writeResource(filename, script.requiresCss, "requires.css", fileOpts), |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | writeResource( |
| 63 | filename: string, |
no test coverage detected