(command: IUserScript)
| 326 | // Slightly modified from Templater's user script engine: |
| 327 | // https://github.com/SilentVoid13/Templater/blob/master/src/UserTemplates/UserTemplateParser.ts |
| 328 | protected async executeUserScript(command: IUserScript) { |
| 329 | // Member-aware key: preloaded values are DRILLED exports, so a command |
| 330 | // drilling a different `::` member of the same file must never consume |
| 331 | // another command's entry (see getUserScriptPreloadKey). |
| 332 | const cacheKey = getUserScriptPreloadKey(command); |
| 333 | let userScript: unknown; |
| 334 | if (cacheKey !== undefined) { |
| 335 | const cached = this.preloadedUserScripts.get(cacheKey); |
| 336 | if (cached !== undefined) { |
| 337 | userScript = cached; |
| 338 | this.preloadedUserScripts.delete(cacheKey); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (userScript === undefined) { |
| 343 | userScript = await getUserScript(command, this.app); |
| 344 | } |
| 345 | |
| 346 | if (!userScript) { |
| 347 | log.logError(`failed to load user script ${command.path}.`); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | if (!command.settings) { |
| 352 | command.settings = {}; |
| 353 | } |
| 354 | |
| 355 | const userScriptSettings = getUserScriptSettings(userScript); |
| 356 | if (userScriptSettings) { |
| 357 | // Initialize default values for settings before executing the script |
| 358 | initializeUserScriptSettings(command.settings, userScriptSettings); |
| 359 | } |
| 360 | this.userScriptCommand = command; |
| 361 | this.userScriptSettingsDefinition = userScriptSettings; |
| 362 | |
| 363 | try { |
| 364 | await this.userScriptDelegator(userScript); |
| 365 | } catch (err) { |
| 366 | if (err instanceof MacroAbortError) { |
| 367 | throw err; |
| 368 | } |
| 369 | // Report and re-throw script errors so users can debug them |
| 370 | reportError(err, `Failed to run user script ${command.name}`); |
| 371 | throw err; |
| 372 | } finally { |
| 373 | this.userScriptCommand = null; |
| 374 | this.userScriptSettingsDefinition = undefined; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | private async getResolvedUserScriptSettings(command: IUserScript) { |
| 379 | if ( |
no test coverage detected