@luna-park/plugin 1.7.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,139 +1,229 @@
1
- # @luna-park/plugin
2
-
3
- This package provides the core types and helper functions for creating plugins for **Luna Park**, a visual scripting platform.
4
-
5
- It exports the necessary interfaces to define plugin configurations, editor components, logic nodes, and lifecycle hooks.
6
-
7
- ## Installation
8
-
9
- ```bash
10
- npm install @luna-park/plugin
11
- ```
12
-
13
- ## Usage
14
-
15
- The primary way to define a plugin is using the `makePlugin` helper function. This ensures type safety for your plugin configuration.
16
-
17
- ```typescript
18
- import { makePlugin, ELogicType } from "@luna-park/plugin";
19
- import awesomeIcon from "./awesome-icon.svg"; // Icon URL
20
-
21
- export default makePlugin({
22
- id: "my-awesome-plugin",
23
- name: "My Awesome Plugin",
24
- icon: awesomeIcon, // Icon URL
25
- description: "Adds amazing capabilities to Luna Park.",
26
-
27
- // Plugin configuration schema (optional)
28
- config: LogicType.object({
29
- apiKey: LogicType.string(),
30
- theme: LogicType.string({enum: ["light", "dark"], default: "dark"}),
31
- }),
32
-
33
- // Lifecycle hooks (optional)
34
- lifecycle: {
35
- mount: async (env) => {
36
- console.log("Plugin mounted", env.config.apiKey);
37
- },
38
- update: async ({config}) => {
39
- console.log("Config updated", config);
40
- },
41
- unmount: (env) => {
42
- console.log("Plugin unmounted");
43
- },
44
- },
45
-
46
- // Extend the editor
47
- editor: {
48
- // Register custom Vue components for the editor (optional)
49
- components: [
50
- {
51
- name: "Element/Avatar",
52
- component: MyAvatarComponent,
53
- properties: {
54
- src: LogicType.string({ name: "Avatar URL" }),
55
- name: LogicType.string({ name: "Name" }),
56
- },
57
- slots: {
58
- default: LogicType.void()
59
- }
60
- }
61
- // ... component definitions
62
- ],
63
-
64
- // Register custom logic nodes (optional)
65
- nodes: [
66
- makeLogicNode({
67
- name: "log/stuff",
68
- inputs: {
69
- in_exec: LogicType.exec(),
70
- in_stuff: LogicType.string({ name: "Stuff" })
71
- },
72
- outputs: {
73
- out_exec: LogicType.exec()
74
- },
75
- methods: {
76
- async in_exec() {
77
- console.log(this.in_stuff);
78
- await this.out_exec();
79
- }
80
- },
81
- display: {
82
- name: "Log stuff"
83
- }
84
- })
85
- // ... node definitions
86
- ],
87
-
88
- // Register wrapper for the editor (optional)
89
- wrapper : MyWrapperComponent,
90
- },
91
-
92
- // Code injection options (optional)
93
- inject: {
94
- css: "body { background-color: #000; }",
95
- js: "console.log('Hello world!')"
96
- }
97
- });
98
- ```
99
-
100
- ## API Reference
101
-
102
- ### `makePlugin(plugin)`
103
-
104
- A helper identity function that takes a `TPlugin` object and returns it. It is used to infer types for the configuration and internal state.
105
-
106
- ### `TPlugin<TConfig, TInternal>`
107
-
108
- The main interface for defining a plugin.
109
-
110
- | Property | Type | Description |
111
- | :--- | :--- | :--- |
112
- | `id` | `string` | **Required**. Unique identifier for the plugin. |
113
- | `name` | `string` | **Required**. Display name of the plugin. |
114
- | `icon` | `string` | **Required**. Icon for the plugin. |
115
- | `description` | `string` | Brief description of what the plugin does. |
116
- | `config` | `TSchema` | Schema definition for the plugin's configuration settings. |
117
- | `lifecycle` | `object` | Lifecycle hooks (`mount`, `unmount`, `update`). |
118
- | `editor` | `object` | Extensions for the editor (components, nodes, tokens, wrapper). |
119
- | `build` | `object` | Build-time configurations (Vite plugins, imports). |
120
- | `inject` | `object` | CSS/JS injection options. |
121
-
122
- ### `TEnv<TConfig, TInternal>`
123
-
124
- The environment object passed to lifecycle hooks and dynamic options.
125
-
126
- | Property | Type | Description |
127
- |:------------|:----------------------| :--- |
128
- | `app` | `TApplication` | Access to the core application instance. |
129
- | `config` | `Static<TConfig>` | The resolved configuration values based on the schema. |
130
- | `mode` | `"build" \| "editor"` | The current running mode. |
131
- | `internals` | `TInternals` | Internal state (if defined). |
132
-
133
- ### `TOption<TEnv, T>`
134
-
135
- Many properties in `TPlugin` can be either a static value `T` or a function that returns `T` (or `Promise<T>`). This allows for dynamic configuration based on the environment.
136
-
137
- ```typescript
138
- type TOption<TEnv, T> = T | ((env: TEnv) => T | Promise<T>);
139
- ```
1
+ # @luna-park/plugin
2
+
3
+ This package provides the core types and helper functions for creating plugins for **Luna Park**, a visual scripting platform.
4
+
5
+ It exports the necessary interfaces to define plugin configurations, editor components, logic nodes, and lifecycle hooks.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @luna-park/plugin
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ The primary way to define a plugin is using the `makePlugin` helper function. This ensures type safety for your plugin configuration.
16
+
17
+ ```typescript
18
+ import { makePlugin, ELogicType } from "@luna-park/plugin";
19
+ import awesomeIcon from "./awesome-icon.svg"; // Icon URL
20
+
21
+ export default makePlugin({
22
+ id: "my-awesome-plugin",
23
+ name: "My Awesome Plugin",
24
+ icon: awesomeIcon, // Icon URL
25
+ description: "Adds amazing capabilities to Luna Park.",
26
+
27
+ // Plugin configuration schema (optional)
28
+ config: LogicType.object({
29
+ apiKey: LogicType.string(),
30
+ theme: LogicType.string({enum: ["light", "dark"], default: "dark"}),
31
+ }),
32
+
33
+ // Lifecycle hooks (optional)
34
+ lifecycle: {
35
+ mount: async (env) => {
36
+ console.log("Plugin mounted", env.config.apiKey);
37
+ },
38
+ update: async ({config}) => {
39
+ console.log("Config updated", config);
40
+ },
41
+ unmount: (env) => {
42
+ console.log("Plugin unmounted");
43
+ },
44
+ },
45
+
46
+ // Extend the editor
47
+ editor: {
48
+ // Register custom Vue components for the editor (optional)
49
+ components: [
50
+ {
51
+ name: "Element/Avatar",
52
+ component: MyAvatarComponent,
53
+ properties: {
54
+ src: LogicType.string({ name: "Avatar URL" }),
55
+ name: LogicType.string({ name: "Name" }),
56
+ },
57
+ slots: {
58
+ default: LogicType.void()
59
+ }
60
+ }
61
+ // ... component definitions
62
+ ],
63
+
64
+ // Register custom logic nodes (optional)
65
+ nodes: [
66
+ makeLogicNode({
67
+ name: "log/stuff",
68
+ inputs: {
69
+ in_exec: LogicType.exec(),
70
+ in_stuff: LogicType.string({ name: "Stuff" })
71
+ },
72
+ outputs: {
73
+ out_exec: LogicType.exec()
74
+ },
75
+ methods: {
76
+ async in_exec() {
77
+ console.log(this.in_stuff);
78
+ await this.out_exec();
79
+ }
80
+ },
81
+ display: {
82
+ name: "Log stuff"
83
+ }
84
+ })
85
+ // ... node definitions
86
+ ],
87
+
88
+ // Register wrapper for the editor (optional)
89
+ wrapper : MyWrapperComponent,
90
+ },
91
+
92
+ // Code injection options (optional)
93
+ inject: {
94
+ css: "body { background-color: #000; }",
95
+ js: "console.log('Hello world!')"
96
+ }
97
+ });
98
+ ```
99
+
100
+ ## API Reference
101
+
102
+ ### `makePlugin(plugin)`
103
+
104
+ A helper identity function that takes a `TPlugin` object and returns it. It is used to infer types for the configuration and internal state.
105
+
106
+ ### `TPlugin<TConfig, TInternal>`
107
+
108
+ The main interface for defining a plugin.
109
+
110
+ | Property | Type | Description |
111
+ | :--- | :--- | :--- |
112
+ | `id` | `string` | **Required**. Unique identifier for the plugin. |
113
+ | `name` | `string` | **Required**. Display name of the plugin. |
114
+ | `icon` | `string` | **Required**. Icon for the plugin. |
115
+ | `description` | `string` | Brief description of what the plugin does. |
116
+ | `api` | `TOption<TEnv, unknown>` | Functions exposed to other plugins (see below). |
117
+ | `config` | `TSchema` | Schema definition for the plugin's configuration settings. |
118
+ | `dependencies` | `Array<{ id, name, target }>` | Other plugins this plugin requires (see below). |
119
+ | `lifecycle` | `object` | Lifecycle hooks (`mount`, `unmount`, `update`). |
120
+ | `editor` | `object` | Extensions for the editor (components, nodes, tokens, wrapper). |
121
+ | `build` | `object` | Build-time configurations (Vite plugins, imports). |
122
+ | `inject` | `object` | CSS/JS injection options. |
123
+
124
+ ### Dependencies
125
+
126
+ A plugin can depend on other plugins. Missing dependencies are installed alongside it, and dependencies are always loaded, mounted and injected before the plugins that require them.
127
+
128
+ ```typescript
129
+ export default makePlugin({
130
+ id: "my-awesome-plugin",
131
+ // ...
132
+ dependencies: [
133
+ { id: "nuxt-ui", name: "Nuxt UI", target: "@luna-park/plugin-nuxt-ui" }
134
+ ]
135
+ });
136
+ ```
137
+
138
+ | Property | Description |
139
+ | :--- | :--- |
140
+ | `id` | The `id` of the required plugin. |
141
+ | `name` | Display name, shown before the dependency is installed. |
142
+ | `target` | npm package name or URL used to install it. |
143
+
144
+ `sortPlugins(plugins)` returns the given plugins in dependency order.
145
+
146
+ ### Using another plugin's functions
147
+
148
+ A plugin exposes functions through `api`, built from its own environment (its config and internals). Other plugins get it with `env.getPlugin(id)`. Import only the API type from the dependency so its code is not bundled twice.
149
+
150
+ ```typescript
151
+ // @luna-park/plugin-a
152
+ export type TPluginAApi = { greet: (name: string) => string; };
153
+
154
+ export default makePlugin({
155
+ id: "plugin-a",
156
+ // ...
157
+ api: (env): TPluginAApi => ({
158
+ greet: (name) => `${ env.config.greeting } ${ name }`
159
+ })
160
+ });
161
+
162
+ // @luna-park/plugin-b
163
+ import type { TPluginAApi } from "@luna-park/plugin-a";
164
+
165
+ export default makePlugin({
166
+ id: "plugin-b",
167
+ // ...
168
+ dependencies: [{ id: "plugin-a", name: "Plugin A", target: "@luna-park/plugin-a" }],
169
+ lifecycle: {
170
+ mount: async (env) => {
171
+ const pluginA = await env.getPlugin<TPluginAApi>("plugin-a");
172
+ env.log(pluginA?.greet("world") ?? "Plugin A is not installed");
173
+ }
174
+ }
175
+ });
176
+ ```
177
+
178
+ `getPlugin` resolves to `undefined` when the plugin is not installed or exposes no `api`. It works in both `editor` and `build` modes, and dependencies are always loaded first.
179
+
180
+ ### `TEnv<TConfig, TInternal>`
181
+
182
+ The environment object passed to lifecycle hooks and dynamic options.
183
+
184
+ | Property | Type | Description |
185
+ |:------------|:----------------------| :--- |
186
+ | `app` | `TApplication` | Access to the core application instance. |
187
+ | `config` | `Static<TConfig>` | The resolved configuration values based on the schema. |
188
+ | `backend` | `object` | Editor backend helpers (see below). |
189
+ | `getPlugin` | `(id) => Promise<TApi>` | Get the `api` of another installed plugin. |
190
+ | `host` | `THost \| undefined` | Desktop app helpers (see below). Editor only. |
191
+ | `mode` | `"build" \| "editor"` | The current running mode. |
192
+ | `internals` | `TInternals` | Internal state (if defined). |
193
+
194
+ #### Transactions
195
+
196
+ Backend logic can run inside a database transaction (the `DB Transaction` node, or the editor's test runs, which are always rolled back). When a plugin changes something outside the database, it follows the transaction with:
197
+
198
+ | Function | Description |
199
+ | :--- | :--- |
200
+ | `backend.onCommit(callback)` | Runs `callback` once the current transaction is committed, or right away outside a transaction. |
201
+ | `backend.onRollback(callback)` | Runs `callback` if the current transaction is rolled back. Ignored outside a transaction. |
202
+
203
+ ```typescript
204
+ await saveContent(id, content);
205
+ env.backend.onRollback(() => deleteContent(id));
206
+ ```
207
+
208
+ The generated backend exposes the same functions as `onCommit` and `onRollback` from `@/database/index.js`.
209
+
210
+ #### Desktop host
211
+
212
+ In the desktop app, `host.available` is `true` and the plugin can use:
213
+
214
+ | Function | Description |
215
+ | :--- | :--- |
216
+ | `host.readFile(path)` | Reads a file of the project folder (`undefined` if it doesn't exist). |
217
+ | `host.writeFile(path, data)` | Writes a file of the project folder, creating its folders. |
218
+ | `host.removeFile(path)` | Removes a file of the project folder. |
219
+ | `host.fetch(input, init)` | `fetch` without CORS restrictions. |
220
+
221
+ Paths are relative to the project folder and can't leave it. In the web editor, `host` is `undefined` or `host.available` is `false`.
222
+
223
+ ### `TOption<TEnv, T>`
224
+
225
+ Many properties in `TPlugin` can be either a static value `T` or a function that returns `T` (or `Promise<T>`). This allows for dynamic configuration based on the environment.
226
+
227
+ ```typescript
228
+ type TOption<TEnv, T> = T | ((env: TEnv) => T | Promise<T>);
229
+ ```