File size: 2,277 Bytes
430354e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
import { createStore } from "/js/AlpineStore.js";
import * as api from "/js/api.js";
import {
  store as notificationStore,
  defaultPriority,
} from "/components/notifications/notification-store.js";
import { formatDateTime } from "/js/time-utils.js";

const model = {
  pluginName: "",
  pluginDisplayName: "",
  output: "",
  running: false,
  exitCode: null,
  lastExecution: null,

  async open(plugin) {
    if (!plugin?.name) return;
    this.pluginName = plugin.name;
    this.pluginDisplayName = plugin.display_name || plugin.name;
    this.output = "";
    this.exitCode = null;
    this.lastExecution = null;
    window.openModal?.("components/plugins/list/plugin-execute-modal.html");
    await this.fetchLastExecution();
  },

  async fetchLastExecution() {
    try {
      const response = await api.callJsonApi("plugins", {
        action: "get_execute_record",
        plugin_name: this.pluginName,
      });
      this.lastExecution = response.data || null;
    } catch (e) {
      this.lastExecution = null;
    }
  },

  async run() {
    if (!this.pluginName) return;
    this.output = "";
    this.exitCode = null;
    this.running = true;
    try {
      const response = await api.callJsonApi("plugins", {
        action: "run_execute_script",
        plugin_name: this.pluginName,
      });
      this.output = response.output || "";
      this.exitCode = response.exit_code ?? null;
      if (response.executed_at) {
        this.lastExecution = {
          executed_at: response.executed_at,
          exit_code: response.exit_code ?? null,
        };
      }
    } catch (e) {
      this.output = e.message || String(e);
      this.exitCode = -1;
      notificationStore.frontendError(
        e.message || String(e),
        "Failed to run execute script",
        3,
        "pluginExecute",
        defaultPriority,
        true,
      );
    } finally {
      this.running = false;
    }
  },

  cleanup() {
    this.pluginName = "";
    this.pluginDisplayName = "";
    this.output = "";
    this.running = false;
    this.exitCode = null;
    this.lastExecution = null;
  },

  formatTimestamp(value) {
    if (!value) return "";
    return formatDateTime(value, "full");
  },
};

export const store = createStore("pluginExecuteStore", model);