( app: App | undefined, command: IUserScript, userScriptSettings: UserScriptSettingsDefinition | undefined, )
| 704 | } |
| 705 | |
| 706 | export async function migrateUserScriptSecretSettings( |
| 707 | app: App | undefined, |
| 708 | command: IUserScript, |
| 709 | userScriptSettings: UserScriptSettingsDefinition | undefined, |
| 710 | ): Promise<boolean> { |
| 711 | const secretOptionEntries = getSecretOptionEntries(userScriptSettings); |
| 712 | if (secretOptionEntries.length === 0) return false; |
| 713 | |
| 714 | const secretStorage = getSecretStorage(app); |
| 715 | if (!secretStorage?.getSecret || !secretStorage?.setSecret) { |
| 716 | const hasLegacySecrets = secretOptionEntries.some(([name]) => { |
| 717 | const value = command.settings?.[name]; |
| 718 | return typeof value === "string" && value.length > 0; |
| 719 | }); |
| 720 | |
| 721 | if (hasLegacySecrets) { |
| 722 | log.logWarning( |
| 723 | `SecretStorage unavailable; leaving plaintext user script secret settings for "${command.name}" unchanged.`, |
| 724 | ); |
| 725 | } |
| 726 | |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | let migrated = false; |
| 731 | |
| 732 | for (const [settingName, option] of secretOptionEntries) { |
| 733 | const value = command.settings?.[settingName]; |
| 734 | if (isUserScriptSecretRef(value)) continue; |
| 735 | if (typeof value !== "string" || value.length === 0) { |
| 736 | if (!optionDefinesStableSecretId(option)) continue; |
| 737 | |
| 738 | const secretRef = buildUserScriptSecretId(command, settingName, option); |
| 739 | const existingSecret = await readSecretStorageEntry(app, secretRef); |
| 740 | if (!existingSecret) continue; |
| 741 | |
| 742 | command.settings[settingName] = createUserScriptSecretRef(secretRef); |
| 743 | for (const [existingName, existingValue] of Object.entries( |
| 744 | command.settings, |
| 745 | )) { |
| 746 | if ( |
| 747 | existingName !== settingName && |
| 748 | isUserScriptSecretRef(existingValue) && |
| 749 | existingValue.secretRef === secretRef |
| 750 | ) { |
| 751 | delete command.settings[existingName]; |
| 752 | } |
| 753 | } |
| 754 | migrated = true; |
| 755 | continue; |
| 756 | } |
| 757 | |
| 758 | const secretRef = await storeUserScriptSecret( |
| 759 | app, |
| 760 | command, |
| 761 | settingName, |
| 762 | value, |
| 763 | undefined, |
no test coverage detected