Skip to content

Commit ee59571

Browse files
committed
feat: add shared.js template for sharing constants between runtime and UI
1 parent b8f4a32 commit ee59571

10 files changed

Lines changed: 22 additions & 14 deletions

File tree

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ my-nodes/
4646
│ ├── nodes/
4747
│ │ └── example/
4848
│ │ ├── runtime.js # Node.js runtime
49+
│ │ ├── shared.js # Shared logic and constants
4950
│ │ ├── ui.js # Browser editor logic
5051
│ │ └── template.html # Editor panel HTML
5152
│ └── locales/

docs/ru/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ my-nodes/
4646
│ ├── nodes/
4747
│ │ └── example/
4848
│ │ ├── runtime.js # Рантайм (Node.js)
49+
│ │ ├── shared.js # Расшаренная логика и константы
4950
│ │ ├── ui.js # Логика редактора (браузер)
5051
│ │ └── template.html # HTML-шаблон панели
5152
│ └── locales/

packages/create-node-red/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Calls `nrb init [projectDir]`, which generates:
4040

4141
With `--example` flag, it also generates an example node:
4242

43-
- `src/nodes/example/``runtime.js`, `ui.js`, `template.html`
43+
- `src/nodes/example/``runtime.js`, `shared.js`, `ui.js`, `template.html`
4444
- `src/locales/en-US/example.json`
4545
- `docs/en-US/nodes/example.md`
4646

packages/create-node-red/docs/ru/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bunx create-node-red my-nodes
3939

4040
С флагом `--example` также генерируется узел-пример:
4141

42-
- `src/nodes/example/``runtime.js`, `ui.js`, `template.html`
42+
- `src/nodes/example/``runtime.js`, `shared.js`, `ui.js`, `template.html`
4343
- `src/locales/en-US/example.json`
4444
- `docs/en-US/nodes/example.md`
4545

packages/node-red-builder/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ full type safety in the editor.
7070
Creates:
7171

7272
- `src/nodes/<name>/runtime.js`
73+
- `src/nodes/<name>/shared.js` (for functional nodes only)
7374
- `src/nodes/<name>/ui.js`
7475
- `src/nodes/<name>/template.html`
7576
- `src/locales/en-US/<name>.json`

packages/node-red-builder/docs/ru/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ CLI и рантайм-фреймворк для разработки нод Node
6868
Создаёт:
6969

7070
- `src/nodes/<name>/runtime.js`
71+
- `src/nodes/<name>/shared.js` (только для функциональных нод)
7172
- `src/nodes/<name>/ui.js`
7273
- `src/nodes/<name>/template.html`
7374
- `src/locales/en-US/<name>.json`

packages/node-red-builder/src/cli/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ export async function generateNode({ nodeName, prefix, color, type = 'node', con
204204
const localeTemplate = isConfigNode ? 'src/locales/en-US/config-node.json' : 'src/locales/en-US/node.json';
205205
await copyTemplate(localeTemplate, `src/locales/en-US/${nodeName}.json`, replacements);
206206

207-
if (!isConfigNode)
207+
if (!isConfigNode) {
208+
await copyTemplate(`${templateDir}/shared.js`, `src/nodes/${nodeName}/shared.js`, replacements);
208209
await copyTemplate('docs/en-US/nodes/node.md', `docs/en-US/nodes/${nodeName}.md`, replacements);
210+
}
209211
}

packages/node-red-builder/templates/src/nodes/node/runtime.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import { BaseNode, registerNode } from 'node-red-builder';
2+
import { ACTION } from './shared.js';
23
/** @import { NodeMessage, NodeAPI } from 'node-red' */
34
/** @import { NodeProps as BaseNodeProps } from 'node-red-builder' */
5+
/** @import { Action } from './shared.js' */
46
// __CONFIG_IMPORT__
57

6-
/** @typedef {(typeof ACTION)[keyof typeof ACTION]} Action */
7-
export const ACTION = /** @type {const} */ ({
8-
APPLY: 'apply',
9-
RESTART: 'restart',
10-
ADD: 'add',
11-
DEL: 'delete',
12-
UPDATE: 'update'
13-
});
14-
158
/** @typedef {Omit<BaseNodeProps, 'config'> & { action: string, actionType: 'action'|'str'|'msg' }} NodeProps */
169

1710
/** @extends {BaseNode<NodeProps>} */
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @typedef {(typeof ACTION)[keyof typeof ACTION]} Action */
2+
export const ACTION = /** @type {const} */ ({
3+
APPLY: 'apply',
4+
RESTART: 'restart',
5+
ADD: 'add',
6+
DEL: 'delete',
7+
UPDATE: 'update'
8+
});

packages/node-red-builder/templates/src/nodes/node/ui.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { setupTypedInput, createTypedInputOptions } from 'node-red-builder/ui';
2-
import { ACTION } from './runtime.js';
2+
import { ACTION } from './shared.js';
33
/** @import { EditorRED } from 'node-red' */
44
/** @import { EditorDefaults, TypedInputDefinition } from 'node-red-builder/ui' */
5-
/** @import { Action, __NODE_CLASS__ } from './runtime.js' */
5+
/** @import { Action } from './shared.js' */
6+
/** @import { __NODE_CLASS__ } from './runtime.js' */
67

78
let /** @type {EditorRED} */ RED = /** @type {any} */ (window).RED;
89

0 commit comments

Comments
 (0)