@vdwpsmt/node-red-contrib-flow-splitter-extended 1.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/LICENSE +201 -0
- package/README.md +256 -0
- package/functions-templates-handler.js +301 -0
- package/index.d.ts +31 -0
- package/index.js +342 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# node-red-contrib-flow-splitter-extended
|
|
2
|
+
|
|
3
|
+
Node-RED plugin to split your **_flows.json_** file in individual YAML or JSON files (per tab, subflow and config-node) **with automatic extraction of function and ui-template node code**.
|
|
4
|
+
|
|
5
|
+
## Purpose
|
|
6
|
+
|
|
7
|
+
This plugin is useful if you regularly edit Node-RED applications in combination with editing in VS code or other IDEs.
|
|
8
|
+
|
|
9
|
+
It will make the diffs of your version control much more controlled and readable: It will also allow to edit (complex) function and UI-template nodes from within an external IDE
|
|
10
|
+
|
|
11
|
+
- Commit files individually
|
|
12
|
+
- Nodes are ordered alphabetically with their id
|
|
13
|
+
- **Function and ui-template node code is extracted into separate `.js`, `.vue`, and `.md` files for easier editing in VS Code or other IDEs**
|
|
14
|
+
- **Edit function code externally and reload changes without restarting Node-RED**
|
|
15
|
+
|
|
16
|
+
**Note:** Add **_flows.json_** (or its equivalent given in the `package.json`) to your project `.gitignore` file.
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
### Flow Splitting
|
|
21
|
+
- Splits flows.json into separate files for tabs, subflows, and config-nodes
|
|
22
|
+
- Supports both YAML and JSON formats
|
|
23
|
+
- Maintains tab order through configuration
|
|
24
|
+
|
|
25
|
+
### Function & Template Extraction (NEW)
|
|
26
|
+
- Automatically extracts code from `function` nodes into `.js` files
|
|
27
|
+
- Extracts `ui-template` (Dashboard 2.0) content into `.vue` files
|
|
28
|
+
- Supports function `initialize` and `finalize` code in separate files
|
|
29
|
+
- Extracts node `info` documentation into `.md` files
|
|
30
|
+
- Organizes extracted files in subdirectories alongside their parent tab/subflow
|
|
31
|
+
- Automatically syncs changes back to Node-RED on startup
|
|
32
|
+
|
|
33
|
+
## Functioning
|
|
34
|
+
|
|
35
|
+
This plugin does not modify Node-RED core behavior. Node-RED core will still compile the flows into the JSON file stipulated in the `package.json`.
|
|
36
|
+
|
|
37
|
+
The code is executed at each start of the flows, i.e. a start of Node-RED or a "deploy" action.
|
|
38
|
+
|
|
39
|
+
It will take the running JSON file used by Node-RED specified in the Node-RED `package.json` (**_flows.json_** by default) and create all files in the directory `src` (_by default_) and their sub-directories : `tabs`, `subflows` and `config-nodes` at the root of the Node-RED userDir or the active project folder.
|
|
40
|
+
|
|
41
|
+
### Function/Template Extraction Workflow
|
|
42
|
+
|
|
43
|
+
When you deploy flows containing function or ui-template nodes:
|
|
44
|
+
|
|
45
|
+
1. **Extraction Phase**: After splitting the flow files, the plugin scans each tab and subflow for function and ui-template nodes
|
|
46
|
+
2. **File Creation**: For each node, it creates a subdirectory named after the tab/subflow and extracts:
|
|
47
|
+
- Main code into `<node-name>.js` (function) or `<node-name>.vue` (ui-template)
|
|
48
|
+
- Initialize code into `<node-name>.initialize.js` (if present)
|
|
49
|
+
- Finalize code into `<node-name>.finalize.js` (if present)
|
|
50
|
+
- Info documentation into `<node-name>.info.md` (if present)
|
|
51
|
+
3. **Manifest**: A `.manifest.json` file tracks the mapping between nodes and extracted files
|
|
52
|
+
|
|
53
|
+
When Node-RED starts with no flows.json:
|
|
54
|
+
|
|
55
|
+
1. **Collection Phase**: The plugin reads all extracted function/template files
|
|
56
|
+
2. **Update**: Updates the split flow files with the current code from disk
|
|
57
|
+
3. **Rebuild**: Reconstructs the flows.json from the updated split files
|
|
58
|
+
|
|
59
|
+
This allows you to edit function and template code in your favorite IDE with full syntax highlighting, linting, code assist and version control benefits!
|
|
60
|
+
|
|
61
|
+
### Manual Reload (Live Editing)
|
|
62
|
+
|
|
63
|
+
When Node-RED is running and you edit function/template files externally, use the **manual reload** endpoint to apply changes without restarting:
|
|
64
|
+
|
|
65
|
+
**HTTP Endpoint:**
|
|
66
|
+
```bash
|
|
67
|
+
POST http://localhost:1880/flow-splitter/reload
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Usage Examples:**
|
|
71
|
+
```powershell
|
|
72
|
+
# PowerShell (Windows)
|
|
73
|
+
Invoke-RestMethod -Uri http://localhost:1880/flow-splitter/reload -Method Post
|
|
74
|
+
|
|
75
|
+
# Bash (Linux/Mac)
|
|
76
|
+
curl -X POST http://localhost:1880/flow-splitter/reload
|
|
77
|
+
|
|
78
|
+
# From browser console or bookmarklet
|
|
79
|
+
fetch('http://localhost:1880/flow-splitter/reload', {method: 'POST'})
|
|
80
|
+
.then(r => r.json()).then(console.log);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
This allows you to:
|
|
84
|
+
1. Edit function/template files in VS Code
|
|
85
|
+
2. Save changes
|
|
86
|
+
3. Run the reload command
|
|
87
|
+
4. See changes immediately in Node-RED (without deploy/restart)
|
|
88
|
+
|
|
89
|
+
## File Structure Example
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
project/
|
|
93
|
+
├── flows.json (auto-deleted after split)
|
|
94
|
+
├── .config.flow-splitter.json
|
|
95
|
+
└── src/
|
|
96
|
+
├── tabs/
|
|
97
|
+
│ ├── Dashboard.yaml
|
|
98
|
+
│ └── Dashboard/
|
|
99
|
+
│ ├── .manifest.json
|
|
100
|
+
│ ├── process_data.js
|
|
101
|
+
│ ├── process_data.initialize.js
|
|
102
|
+
│ ├── process_data.finalize.js
|
|
103
|
+
│ ├── process_data.info.md
|
|
104
|
+
│ ├── header_template.vue
|
|
105
|
+
│ └── status_widget.vue
|
|
106
|
+
├── subflows/
|
|
107
|
+
│ ├── DataProcessor.yaml
|
|
108
|
+
│ └── DataProcessor/
|
|
109
|
+
│ ├── .manifest.json
|
|
110
|
+
│ ├── transform.js
|
|
111
|
+
│ └── validate.js
|
|
112
|
+
└── config-nodes/
|
|
113
|
+
└── mqtt-broker.yaml
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Configuration
|
|
117
|
+
|
|
118
|
+
The plugin will generate a configuration file `.config.flow-splitter.json` at the root of the Node-RED userDir or the active project folder.
|
|
119
|
+
|
|
120
|
+
Default configuration file =
|
|
121
|
+
|
|
122
|
+
```json
|
|
123
|
+
{
|
|
124
|
+
"fileFormat": "yaml",
|
|
125
|
+
"destinationFolder": "src",
|
|
126
|
+
"tabsOrder": [],
|
|
127
|
+
"extractFunctionsTemplates": true
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
You can freely edit the config file, the changes are taken into account at the next restart of the flows.
|
|
132
|
+
|
|
133
|
+
- `fileFormat`: parsing language for your split source files (either `yaml` or `json`)
|
|
134
|
+
- `destinationFolder`: path where to create the `tabs`, `subflows` and `config-nodes` sub-directories
|
|
135
|
+
- `tabsOrder`: position of each tab (ordered array of the Ids of each tab node)
|
|
136
|
+
- `extractFunctionsTemplates`: additional extraction of function and ui-template nodes
|
|
137
|
+
## Installation
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
npm install node-red-contrib-flow-splitter-extended
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Or install via the Node-RED palette manager.
|
|
144
|
+
|
|
145
|
+
## Quick Start
|
|
146
|
+
|
|
147
|
+
1. **Install the plugin** (see above)
|
|
148
|
+
2. **Start Node-RED** and deploy your flows
|
|
149
|
+
3. **Plugin automatically:**
|
|
150
|
+
- Splits flows.json into separate files in `src/` directory
|
|
151
|
+
- Extracts function and ui-template code into `.js` and `.vue` files
|
|
152
|
+
- Creates `.config.flow-splitter.json` configuration file
|
|
153
|
+
4. **Edit extracted files** in VS Code or your favorite editor
|
|
154
|
+
5. **Reload changes** using the manual reload command (see above)
|
|
155
|
+
|
|
156
|
+
## Usage Workflows
|
|
157
|
+
|
|
158
|
+
### Option 1: Edit & Restart (Simple)
|
|
159
|
+
1. Edit function/template files in `src/tabs/<TabName>/`
|
|
160
|
+
2. Stop Node-RED
|
|
161
|
+
3. Start Node-RED
|
|
162
|
+
4. Changes are automatically collected and applied
|
|
163
|
+
|
|
164
|
+
### Option 2: Edit & Manual Reload (Fast)
|
|
165
|
+
1. Edit function/template files in `src/tabs/<TabName>/`
|
|
166
|
+
2. Save files
|
|
167
|
+
3. Run reload command: `curl -X POST http://localhost:1880/flow-splitter/reload`
|
|
168
|
+
4. Changes applied immediately (no restart needed)
|
|
169
|
+
|
|
170
|
+
## Working with Extracted Code
|
|
171
|
+
|
|
172
|
+
### Function Nodes
|
|
173
|
+
|
|
174
|
+
Function nodes are extracted to `.js` files:
|
|
175
|
+
- **Main code:** `<node-name>.js`
|
|
176
|
+
- **Initialize code:** `<node-name>.initialize.js` (if present)
|
|
177
|
+
- **Finalize code:** `<node-name>.finalize.js` (if present)
|
|
178
|
+
- **Documentation:** `<node-name>.info.md` (if present)
|
|
179
|
+
|
|
180
|
+
**Example:** A function node named "Process Data" creates:
|
|
181
|
+
```
|
|
182
|
+
src/tabs/MyTab/Process_Data.js
|
|
183
|
+
src/tabs/MyTab/Process_Data.initialize.js
|
|
184
|
+
src/tabs/MyTab/Process_Data.finalize.js
|
|
185
|
+
src/tabs/MyTab/Process_Data.info.md
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### UI Template Nodes
|
|
189
|
+
|
|
190
|
+
Dashboard 2.0 ui-template nodes are extracted to `.vue` files:
|
|
191
|
+
- **Template code:** `<node-name>.vue`
|
|
192
|
+
- **Documentation:** `<node-name>.info.md` (if present)
|
|
193
|
+
|
|
194
|
+
**Example:** A ui-template node named "Header Widget" creates:
|
|
195
|
+
```
|
|
196
|
+
src/tabs/Dashboard/Header_Widget.vue
|
|
197
|
+
src/tabs/Dashboard/Header_Widget.info.md
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Benefits for Development
|
|
201
|
+
|
|
202
|
+
### Version Control
|
|
203
|
+
- Each function/template is in its own file - perfect for git diffs
|
|
204
|
+
- See exactly what changed in each function
|
|
205
|
+
- Review code changes more easily in pull requests
|
|
206
|
+
|
|
207
|
+
### IDE Integration
|
|
208
|
+
- Full syntax highlighting in VS Code or your favorite editor
|
|
209
|
+
- Linting and formatting support
|
|
210
|
+
- IntelliSense and autocomplete
|
|
211
|
+
- Search across all functions easily
|
|
212
|
+
|
|
213
|
+
### Code Organization
|
|
214
|
+
- Functions grouped by their parent tab/subflow
|
|
215
|
+
- Easy to find and navigate function code
|
|
216
|
+
- Consistent file structure across projects
|
|
217
|
+
|
|
218
|
+
## Troubleshooting
|
|
219
|
+
|
|
220
|
+
### Changes Not Appearing After Manual Reload
|
|
221
|
+
|
|
222
|
+
1. **Check Node-RED is running:** Ensure Node-RED is started and accessible at the URL
|
|
223
|
+
2. **Verify endpoint:** Test with `curl -X POST http://localhost:1880/flow-splitter/reload`
|
|
224
|
+
3. **Check file paths:** Ensure you're editing files in the correct `src/` subdirectory
|
|
225
|
+
4. **Review manifest:** Check `.manifest.json` to verify node-to-file mappings
|
|
226
|
+
|
|
227
|
+
### Files Not Extracted After Deploy
|
|
228
|
+
|
|
229
|
+
1. **Check configuration:** Ensure `extractFunctionsTemplates: true` in `.config.flow-splitter.json`
|
|
230
|
+
2. **Verify node types:** Only `function` and `ui-template` nodes are extracted
|
|
231
|
+
3. **Node naming:** Ensure nodes have names (unnamed nodes use their ID)
|
|
232
|
+
|
|
233
|
+
### Split Files Not Rebuilding
|
|
234
|
+
|
|
235
|
+
1. **Check flows.json:** Ensure flows.json doesn't exist (it should be deleted after split)
|
|
236
|
+
2. **Verify file format:** Ensure split files match the configured format (YAML/JSON)
|
|
237
|
+
3. **Restart Node-RED:** A full restart rebuilds flows.json from split files
|
|
238
|
+
|
|
239
|
+
## Compatibility
|
|
240
|
+
|
|
241
|
+
- Works with Node-RED project mode
|
|
242
|
+
- Compatible with function nodes (core)
|
|
243
|
+
- Compatible with Dashboard 2.0 ui-template nodes
|
|
244
|
+
- Supports both CommonJS and modern JavaScript
|
|
245
|
+
|
|
246
|
+
## Credits
|
|
247
|
+
|
|
248
|
+
This extended version combines the flow-splitting functionality from the original [node-red-contrib-flow-splitter](https://github.com/vgo-exo/node-red-contrib-flow-splitter) with function/template extraction inspired by [functions-templates-manager](https://github.com/daniel-payne/functions-templates-manager).
|
|
249
|
+
|
|
250
|
+
## License
|
|
251
|
+
|
|
252
|
+
See LICENSE file for details.
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const fs = require('fs-extra')
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Functions and Templates nodes Handler
|
|
6
|
+
* Extracts function and ui-template node code into separate files
|
|
7
|
+
* and collects them back when rebuilding flows
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Extract functions and templates from flow nodes into separate files
|
|
12
|
+
* @param {Array} flowNodes - Array of nodes from a tab or subflow
|
|
13
|
+
* @param {string} flowName - Name of the tab or subflow
|
|
14
|
+
* @param {string} flowDir - Directory where the flow file is stored
|
|
15
|
+
* @param {object} RED - Node-RED runtime
|
|
16
|
+
*/
|
|
17
|
+
function extractFunctionsAndTemplates(flowNodes, flowName, flowDir, RED) {
|
|
18
|
+
if (!flowNodes || flowNodes.length === 0) return
|
|
19
|
+
|
|
20
|
+
const extractedDir = path.join(flowDir, flowName)
|
|
21
|
+
const manifest = {}
|
|
22
|
+
const fileNames = []
|
|
23
|
+
let count = 0
|
|
24
|
+
|
|
25
|
+
flowNodes.forEach((node) => {
|
|
26
|
+
const id = node.id
|
|
27
|
+
const type = node.type
|
|
28
|
+
|
|
29
|
+
let name
|
|
30
|
+
|
|
31
|
+
if (type === 'function') {
|
|
32
|
+
name = node.name || 'unnamed-function'
|
|
33
|
+
} else if (type === 'ui-template') {
|
|
34
|
+
name = node.name || 'unnamed-template'
|
|
35
|
+
} else {
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const sanitizedName = name.replace(/[\/\\:*?"<>|]/g, '-')
|
|
40
|
+
fileNames.push(sanitizedName)
|
|
41
|
+
const nameCount = fileNames.filter((n) => n === sanitizedName).length
|
|
42
|
+
|
|
43
|
+
let fileName
|
|
44
|
+
if (nameCount > 1) {
|
|
45
|
+
fileName = `${sanitizedName}(${nameCount})`
|
|
46
|
+
} else {
|
|
47
|
+
fileName = sanitizedName
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Detect if it's a Vue template or a function
|
|
51
|
+
const hasTemplate = node.format?.trim().indexOf('<template>') !== -1 ?? false
|
|
52
|
+
const hasScript = node.format?.trim().indexOf('<script>') !== -1 ?? false
|
|
53
|
+
const isVue = (typeof node.format === 'string' && (hasTemplate || hasScript))
|
|
54
|
+
const isFun = (
|
|
55
|
+
(typeof node.func === 'string' && node.func.trim().length > 0) ||
|
|
56
|
+
(typeof node.initialize === 'string' && node.initialize.trim().length > 0) ||
|
|
57
|
+
(typeof node.finalize === 'string' && node.finalize.trim().length > 0)
|
|
58
|
+
) && isVue === false
|
|
59
|
+
|
|
60
|
+
let code = isVue ? node.format : node.func
|
|
61
|
+
let initialize = isFun ? node.initialize : undefined
|
|
62
|
+
let finalize = isFun ? node.finalize : undefined
|
|
63
|
+
let info = node.info ?? undefined
|
|
64
|
+
|
|
65
|
+
// Clean up empty values
|
|
66
|
+
if ((code ?? '').trim().length === 0) code = undefined
|
|
67
|
+
if ((initialize ?? '').trim().length === 0) initialize = undefined
|
|
68
|
+
if ((finalize ?? '').trim().length === 0) finalize = undefined
|
|
69
|
+
if ((info ?? '').trim().length === 0) info = undefined
|
|
70
|
+
|
|
71
|
+
if (isVue || isFun) {
|
|
72
|
+
// Ensure output directory exists
|
|
73
|
+
if (!fs.existsSync(extractedDir)) {
|
|
74
|
+
fs.mkdirSync(extractedDir, { recursive: true })
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
count++
|
|
78
|
+
|
|
79
|
+
const baseName = fileName
|
|
80
|
+
const codeName = `${baseName}.${isVue ? 'vue' : 'js'}`
|
|
81
|
+
const initializeName = `${baseName}.initialize.js`
|
|
82
|
+
const finalizeName = `${baseName}.finalize.js`
|
|
83
|
+
const infoName = `${baseName}.info.md`
|
|
84
|
+
|
|
85
|
+
const codeFile = path.join(extractedDir, codeName)
|
|
86
|
+
const initializeFile = path.join(extractedDir, initializeName)
|
|
87
|
+
const finalizeFile = path.join(extractedDir, finalizeName)
|
|
88
|
+
const infoFile = path.join(extractedDir, infoName)
|
|
89
|
+
|
|
90
|
+
// Write files
|
|
91
|
+
if (code != null) {
|
|
92
|
+
fs.writeFileSync(codeFile, code, 'utf8')
|
|
93
|
+
}
|
|
94
|
+
if (initialize != null) {
|
|
95
|
+
fs.writeFileSync(initializeFile, initialize, 'utf8')
|
|
96
|
+
}
|
|
97
|
+
if (finalize != null) {
|
|
98
|
+
fs.writeFileSync(finalizeFile, finalize, 'utf8')
|
|
99
|
+
}
|
|
100
|
+
if (info != null) {
|
|
101
|
+
fs.writeFileSync(infoFile, info, 'utf8')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Store in manifest
|
|
105
|
+
manifest[id] = {
|
|
106
|
+
nodeId: id,
|
|
107
|
+
name,
|
|
108
|
+
sanitizedName,
|
|
109
|
+
fileName,
|
|
110
|
+
isVue,
|
|
111
|
+
isFun,
|
|
112
|
+
hasCode: code != null,
|
|
113
|
+
hasInitialize: initialize != null,
|
|
114
|
+
hasFinalize: finalize != null,
|
|
115
|
+
hasInfo: info != null
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
// Save manifest if we extracted anything
|
|
121
|
+
if (count > 0) {
|
|
122
|
+
const manifestFile = path.join(extractedDir, '.manifest.json')
|
|
123
|
+
fs.writeFileSync(manifestFile, JSON.stringify(manifest, null, 2), 'utf8')
|
|
124
|
+
|
|
125
|
+
RED.log.info(`[node-red-contrib-flow-splitter] Extracted ${count} functions/templates for "${flowName}"`)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Clean up unused files
|
|
129
|
+
cleanupUnusedFiles(extractedDir, manifest, RED)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Collect functions and templates from separate files back into flow nodes
|
|
134
|
+
* @param {Array} flowNodes - Array of nodes from a tab or subflow
|
|
135
|
+
* @param {string} flowName - Name of the tab or subflow
|
|
136
|
+
* @param {string} flowDir - Directory where the flow file is stored
|
|
137
|
+
* @param {object} RED - Node-RED runtime
|
|
138
|
+
* @returns {Array} - Updated flow nodes
|
|
139
|
+
*/
|
|
140
|
+
function collectFunctionsAndTemplates(flowNodes, flowName, flowDir, RED) {
|
|
141
|
+
if (!flowNodes || flowNodes.length === 0) return flowNodes
|
|
142
|
+
|
|
143
|
+
const extractedDir = path.join(flowDir, flowName)
|
|
144
|
+
const manifestFile = path.join(extractedDir, '.manifest.json')
|
|
145
|
+
|
|
146
|
+
// Check if manifest exists
|
|
147
|
+
if (!fs.existsSync(manifestFile)) {
|
|
148
|
+
return flowNodes
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let manifest
|
|
152
|
+
try {
|
|
153
|
+
manifest = JSON.parse(fs.readFileSync(manifestFile, 'utf8'))
|
|
154
|
+
} catch (error) {
|
|
155
|
+
RED.log.warn(`[node-red-contrib-flow-splitter] Could not read manifest for "${flowName}": ${error.message}`)
|
|
156
|
+
return flowNodes
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let updatedCount = 0
|
|
160
|
+
|
|
161
|
+
// Update nodes with content from files
|
|
162
|
+
Object.keys(manifest).forEach((nodeId) => {
|
|
163
|
+
const item = manifest[nodeId]
|
|
164
|
+
const node = flowNodes.find(n => n.id === nodeId)
|
|
165
|
+
|
|
166
|
+
if (!node) {
|
|
167
|
+
RED.log.warn(`[node-red-contrib-flow-splitter] Node ${nodeId} not found in flow "${flowName}"`)
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const baseName = item.fileName
|
|
172
|
+
const codeName = `${baseName}.${item.isVue ? 'vue' : 'js'}`
|
|
173
|
+
const initializeName = `${baseName}.initialize.js`
|
|
174
|
+
const finalizeName = `${baseName}.finalize.js`
|
|
175
|
+
const infoName = `${baseName}.info.md`
|
|
176
|
+
|
|
177
|
+
const codeFile = path.join(extractedDir, codeName)
|
|
178
|
+
const initializeFile = path.join(extractedDir, initializeName)
|
|
179
|
+
const finalizeFile = path.join(extractedDir, finalizeName)
|
|
180
|
+
const infoFile = path.join(extractedDir, infoName)
|
|
181
|
+
|
|
182
|
+
// Read and update code
|
|
183
|
+
if (item.hasCode && fs.existsSync(codeFile)) {
|
|
184
|
+
const content = fs.readFileSync(codeFile, 'utf8')
|
|
185
|
+
if (item.isVue) {
|
|
186
|
+
if (node.format !== content) {
|
|
187
|
+
node.format = content
|
|
188
|
+
node.func = content
|
|
189
|
+
updatedCount++
|
|
190
|
+
}
|
|
191
|
+
} else if (item.isFun) {
|
|
192
|
+
if (node.func !== content) {
|
|
193
|
+
node.func = content
|
|
194
|
+
updatedCount++
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Read and update initialize
|
|
200
|
+
if (item.hasInitialize && fs.existsSync(initializeFile)) {
|
|
201
|
+
const content = fs.readFileSync(initializeFile, 'utf8')
|
|
202
|
+
if (node.initialize !== content) {
|
|
203
|
+
node.initialize = content
|
|
204
|
+
updatedCount++
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Read and update finalize
|
|
209
|
+
if (item.hasFinalize && fs.existsSync(finalizeFile)) {
|
|
210
|
+
const content = fs.readFileSync(finalizeFile, 'utf8')
|
|
211
|
+
if (node.finalize !== content) {
|
|
212
|
+
node.finalize = content
|
|
213
|
+
updatedCount++
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Read and update info
|
|
218
|
+
if (item.hasInfo && fs.existsSync(infoFile)) {
|
|
219
|
+
const content = fs.readFileSync(infoFile, 'utf8')
|
|
220
|
+
if (node.info !== content) {
|
|
221
|
+
node.info = content
|
|
222
|
+
updatedCount++
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
if (updatedCount > 0) {
|
|
228
|
+
RED.log.info(`[node-red-contrib-flow-splitter] Collected ${updatedCount} functions/templates for "${flowName}"`)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return flowNodes
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Clean up files that are no longer in the manifest
|
|
236
|
+
* @param {string} extractedDir - Directory containing extracted files
|
|
237
|
+
* @param {object} manifest - Manifest object
|
|
238
|
+
* @param {object} RED - Node-RED runtime
|
|
239
|
+
*/
|
|
240
|
+
function cleanupUnusedFiles(extractedDir, manifest, RED) {
|
|
241
|
+
if (!fs.existsSync(extractedDir)) return
|
|
242
|
+
|
|
243
|
+
const validExtensions = ['.vue', '.js', '.md']
|
|
244
|
+
const files = getAllFiles(extractedDir, validExtensions)
|
|
245
|
+
|
|
246
|
+
files.forEach(file => {
|
|
247
|
+
// Skip manifest file
|
|
248
|
+
if (file.endsWith('.manifest.json')) return
|
|
249
|
+
|
|
250
|
+
let found = false
|
|
251
|
+
Object.keys(manifest).forEach((id) => {
|
|
252
|
+
const item = manifest[id]
|
|
253
|
+
if (file.indexOf(item.fileName) > -1) {
|
|
254
|
+
found = true
|
|
255
|
+
}
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
if (!found) {
|
|
259
|
+
const filePath = path.join(extractedDir, file)
|
|
260
|
+
try {
|
|
261
|
+
fs.removeSync(filePath)
|
|
262
|
+
RED.log.info(`[node-red-contrib-flow-splitter] Removed unused file: ${file}`)
|
|
263
|
+
} catch (error) {
|
|
264
|
+
RED.log.warn(`[node-red-contrib-flow-splitter] Could not remove file ${file}: ${error.message}`)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
})
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get all files with specified extensions from a directory
|
|
272
|
+
* @param {string} dir - Directory to search
|
|
273
|
+
* @param {Array<string>} exts - Array of extensions to match
|
|
274
|
+
* @param {Array<string>} fileList - Accumulated file list
|
|
275
|
+
* @param {string} relDir - Relative directory path
|
|
276
|
+
* @returns {Array<string>} - List of file paths
|
|
277
|
+
*/
|
|
278
|
+
function getAllFiles(dir, exts, fileList = [], relDir = '') {
|
|
279
|
+
if (!fs.existsSync(dir)) return fileList
|
|
280
|
+
|
|
281
|
+
const files = fs.readdirSync(dir)
|
|
282
|
+
|
|
283
|
+
files.forEach(file => {
|
|
284
|
+
const filePath = path.join(dir, file)
|
|
285
|
+
const relPath = path.join(relDir, file)
|
|
286
|
+
const stat = fs.statSync(filePath)
|
|
287
|
+
|
|
288
|
+
if (stat.isDirectory()) {
|
|
289
|
+
getAllFiles(filePath, exts, fileList, relPath)
|
|
290
|
+
} else if (exts.some(ext => file.endsWith(ext))) {
|
|
291
|
+
fileList.push(relPath)
|
|
292
|
+
}
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
return fileList
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
module.exports = {
|
|
299
|
+
extractFunctionsAndTemplates,
|
|
300
|
+
collectFunctionsAndTemplates
|
|
301
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {NodeDef} from "@node-red/registry";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This typings files is made to ease developpement of plugins by defining types that are not defined by the NodeRed libraries.
|
|
5
|
+
* When a type is defined by the nodered lib but needs to be extended, it is also done here.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export namespace noderedEvent {
|
|
9
|
+
export interface FlowStartedEvent {
|
|
10
|
+
config: FlowConfig;
|
|
11
|
+
type: "flows" | "node" | "full";
|
|
12
|
+
diff?: FlowDiff;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface FlowDiff {
|
|
16
|
+
added: [string];
|
|
17
|
+
changed: [string];
|
|
18
|
+
removed: [string];
|
|
19
|
+
rewired: [string];
|
|
20
|
+
linked: [string];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface FlowConfig {
|
|
24
|
+
flows: [ExtendedNodeDef],
|
|
25
|
+
rev: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ExtendedNodeDef extends NodeDef {
|
|
29
|
+
[key: string]: any
|
|
30
|
+
}
|
|
31
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const manager = require('flows-file-manager')
|
|
4
|
+
const eol = require('eol')
|
|
5
|
+
const yaml = require('js-yaml')
|
|
6
|
+
const functionsTemplatesHandler = require('./functions-templates-handler')
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Here we define some types to allow the IDE to provide us autocompletion.
|
|
10
|
+
* Some are documented directly by the nodered team but some are not.
|
|
11
|
+
*
|
|
12
|
+
* That's why we also made an `index.d.ts` for undocumented types we need to manipulate.
|
|
13
|
+
* Those are defined by analysing the nodered code and some logging of those objects.
|
|
14
|
+
* In this regard, the types defined by ourselves might be incomplete.
|
|
15
|
+
*
|
|
16
|
+
* @typedef {import('./index').noderedEvent.FlowStartedEvent} FlowStartedEventType
|
|
17
|
+
* @typedef {import('./index').noderedEvent.ExtendedNodeDef} ExtendedNodeDef
|
|
18
|
+
* @typedef {import("node-red").NodeRedApp} REDType
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Exposing the RED runtime globally to avoid passing it in every functions.
|
|
23
|
+
* @type {REDType}
|
|
24
|
+
*/
|
|
25
|
+
let RED
|
|
26
|
+
|
|
27
|
+
const splitCfgFilename = '.config.flow-splitter.json'
|
|
28
|
+
const DEFAULT_CFG = {
|
|
29
|
+
fileFormat: 'yaml',
|
|
30
|
+
destinationFolder: 'src',
|
|
31
|
+
tabsOrder: [],
|
|
32
|
+
monolithFilename: "flows.json",
|
|
33
|
+
extractFunctionsTemplates: true
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Write splitter configuration to disk
|
|
38
|
+
* @param {object} cfg - Splitter configuration
|
|
39
|
+
* @param {string} projectPath - Path to the project
|
|
40
|
+
*/
|
|
41
|
+
function writeSplitterConfig(cfg, projectPath) {
|
|
42
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Writing new config")
|
|
43
|
+
try {
|
|
44
|
+
const splitterCfgToWrite = JSON.parse(JSON.stringify(cfg))
|
|
45
|
+
delete splitterCfgToWrite.monolithFilename
|
|
46
|
+
fs.writeFileSync(path.join(projectPath, splitCfgFilename), eol.auto(JSON.stringify(splitterCfgToWrite, null, 2)))
|
|
47
|
+
} catch (error) {
|
|
48
|
+
RED.log.warn(`[node-red-contrib-flow-splitter-extended] Could not write splitter config '${splitCfgFilename}': ${error}`)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Get the project path (handles both project mode and non-project mode)
|
|
54
|
+
* @returns {string} Project path
|
|
55
|
+
*/
|
|
56
|
+
function getProjectPath() {
|
|
57
|
+
const userDir = RED.settings.userDir
|
|
58
|
+
const projectsConfigFile = path.join(userDir, '.config.projects.json')
|
|
59
|
+
|
|
60
|
+
if (fs.existsSync(projectsConfigFile)) {
|
|
61
|
+
const nrProjectsCfg = JSON.parse(fs.readFileSync(projectsConfigFile))
|
|
62
|
+
return path.join(userDir, 'projects', nrProjectsCfg.activeProject)
|
|
63
|
+
}
|
|
64
|
+
return userDir
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Load splitter configuration from disk
|
|
69
|
+
* @param {string} projectPath - Path to the project
|
|
70
|
+
* @returns {object} Splitter configuration
|
|
71
|
+
*/
|
|
72
|
+
function loadSplitterConfig(projectPath) {
|
|
73
|
+
let cfg = { ...DEFAULT_CFG }
|
|
74
|
+
cfg.monolithFilename = RED.settings.flowFile || 'flows.json'
|
|
75
|
+
|
|
76
|
+
const configPath = path.join(projectPath, splitCfgFilename)
|
|
77
|
+
if (fs.existsSync(configPath)) {
|
|
78
|
+
const loadedCfg = JSON.parse(fs.readFileSync(configPath))
|
|
79
|
+
cfg = { ...cfg, ...loadedCfg }
|
|
80
|
+
cfg.monolithFilename = loadedCfg.monolithFilename || RED.settings.flowFile || 'flows.json'
|
|
81
|
+
}
|
|
82
|
+
return cfg
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get private RED instance to access internal APIs
|
|
87
|
+
* @returns {REDType} Private RED instance
|
|
88
|
+
*/
|
|
89
|
+
function getPrivateRED() {
|
|
90
|
+
for (const child of require.main.children) {
|
|
91
|
+
if (child.filename.endsWith('red.js')) {
|
|
92
|
+
return require(child.filename)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return require('node-red')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Extract functions and templates from split flow files
|
|
100
|
+
* @param {object} cfg - Splitter configuration
|
|
101
|
+
* @param {string} projectPath - Path to the project
|
|
102
|
+
*/
|
|
103
|
+
function extractFunctionsTemplatesFromSplitFiles(cfg, projectPath) {
|
|
104
|
+
if (cfg.extractFunctionsTemplates === false) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const srcDir = path.join(projectPath, cfg.destinationFolder || 'src')
|
|
109
|
+
const tabsDir = path.join(srcDir, 'tabs')
|
|
110
|
+
const subflowsDir = path.join(srcDir, 'subflows')
|
|
111
|
+
|
|
112
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Extracting functions and templates...")
|
|
113
|
+
|
|
114
|
+
processFlowDirectory(tabsDir, cfg.fileFormat, 'tab')
|
|
115
|
+
processFlowDirectory(subflowsDir, cfg.fileFormat, 'subflow')
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Process a directory of flow files to extract functions/templates
|
|
120
|
+
* @param {string} dir - Directory to process
|
|
121
|
+
* @param {string} fileFormat - File format (yaml or json)
|
|
122
|
+
* @param {string} flowType - Type of flow (tab or subflow)
|
|
123
|
+
*/
|
|
124
|
+
function processFlowDirectory(dir, fileFormat, flowType) {
|
|
125
|
+
if (!fs.existsSync(dir)) {
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const extension = fileFormat === 'yaml' ? '.yaml' : '.json'
|
|
130
|
+
const files = fs.readdirSync(dir).filter(f => f.endsWith(extension))
|
|
131
|
+
|
|
132
|
+
files.forEach(file => {
|
|
133
|
+
const filePath = path.join(dir, file)
|
|
134
|
+
const flowName = path.basename(file, extension)
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
let flowData
|
|
138
|
+
const fileContent = fs.readFileSync(filePath, 'utf8')
|
|
139
|
+
|
|
140
|
+
if (fileFormat === 'yaml') {
|
|
141
|
+
flowData = yaml.load(fileContent)
|
|
142
|
+
} else {
|
|
143
|
+
flowData = JSON.parse(fileContent)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const flowNodes = Array.isArray(flowData) ? flowData : [flowData]
|
|
147
|
+
functionsTemplatesHandler.extractFunctionsAndTemplates(flowNodes, flowName, dir, RED)
|
|
148
|
+
|
|
149
|
+
} catch (error) {
|
|
150
|
+
RED.log.warn(`[node-red-contrib-flow-splitter-extended] Error processing ${flowType} ${flowName}: ${error.message}`)
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Collect functions and templates back into split flow files before rebuilding monolith
|
|
157
|
+
* @param {object} cfg - Splitter configuration
|
|
158
|
+
* @param {string} projectPath - Path to the project
|
|
159
|
+
*/
|
|
160
|
+
function collectFunctionsTemplatesIntoSplitFiles(cfg, projectPath) {
|
|
161
|
+
if (cfg.extractFunctionsTemplates === false) {
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const srcDir = path.join(projectPath, cfg.destinationFolder || 'src')
|
|
166
|
+
const tabsDir = path.join(srcDir, 'tabs')
|
|
167
|
+
const subflowsDir = path.join(srcDir, 'subflows')
|
|
168
|
+
|
|
169
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Collecting functions and templates...")
|
|
170
|
+
|
|
171
|
+
collectFromFlowDirectory(tabsDir, cfg.fileFormat, 'tab')
|
|
172
|
+
collectFromFlowDirectory(subflowsDir, cfg.fileFormat, 'subflow')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Process a directory of flow files to collect functions/templates
|
|
177
|
+
* @param {string} dir - Directory to process
|
|
178
|
+
* @param {string} fileFormat - File format (yaml or json)
|
|
179
|
+
* @param {string} flowType - Type of flow (tab or subflow)
|
|
180
|
+
*/
|
|
181
|
+
function collectFromFlowDirectory(dir, fileFormat, flowType) {
|
|
182
|
+
if (!fs.existsSync(dir)) {
|
|
183
|
+
return
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const extension = fileFormat === 'yaml' ? '.yaml' : '.json'
|
|
187
|
+
const files = fs.readdirSync(dir).filter(f => f.endsWith(extension))
|
|
188
|
+
|
|
189
|
+
files.forEach(file => {
|
|
190
|
+
const filePath = path.join(dir, file)
|
|
191
|
+
const flowName = path.basename(file, extension)
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
let flowData
|
|
195
|
+
const fileContent = fs.readFileSync(filePath, 'utf8')
|
|
196
|
+
|
|
197
|
+
if (fileFormat === 'yaml') {
|
|
198
|
+
flowData = yaml.load(fileContent)
|
|
199
|
+
} else {
|
|
200
|
+
flowData = JSON.parse(fileContent)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let flowNodes = Array.isArray(flowData) ? flowData : [flowData]
|
|
204
|
+
flowNodes = functionsTemplatesHandler.collectFunctionsAndTemplates(flowNodes, flowName, dir, RED)
|
|
205
|
+
|
|
206
|
+
if (fileFormat === 'yaml') {
|
|
207
|
+
const yamlContent = yaml.dump(flowNodes, {
|
|
208
|
+
indent: 2,
|
|
209
|
+
lineWidth: -1,
|
|
210
|
+
noRefs: true,
|
|
211
|
+
sortKeys: false
|
|
212
|
+
})
|
|
213
|
+
fs.writeFileSync(filePath, eol.auto(yamlContent), 'utf8')
|
|
214
|
+
} else {
|
|
215
|
+
fs.writeFileSync(filePath, eol.auto(JSON.stringify(flowNodes, null, 2)), 'utf8')
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
} catch (error) {
|
|
219
|
+
RED.log.warn(`[node-red-contrib-flow-splitter-extended] Error collecting ${flowType} ${flowName}: ${error.message}`)
|
|
220
|
+
}
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Manual reload endpoint handler
|
|
226
|
+
* Collects functions/templates from files and reloads flows
|
|
227
|
+
*/
|
|
228
|
+
async function manualReload(req, res) {
|
|
229
|
+
try {
|
|
230
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Manual reload triggered")
|
|
231
|
+
|
|
232
|
+
const projectPath = getProjectPath()
|
|
233
|
+
const cfg = loadSplitterConfig(projectPath)
|
|
234
|
+
|
|
235
|
+
collectFunctionsTemplatesIntoSplitFiles(cfg, projectPath)
|
|
236
|
+
|
|
237
|
+
const flowSet = manager.constructFlowSetFromTreeFiles(cfg, projectPath)
|
|
238
|
+
|
|
239
|
+
if (!flowSet) {
|
|
240
|
+
RED.log.error("[node-red-contrib-flow-splitter-extended] Cannot build FlowSet from source tree files")
|
|
241
|
+
return res.status(500).json({
|
|
242
|
+
success: false,
|
|
243
|
+
error: "Cannot build FlowSet from source tree files"
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
manager.constructMonolithFileFromFlowSet(flowSet, cfg, projectPath, false)
|
|
248
|
+
|
|
249
|
+
const PRIVATE_RED = getPrivateRED()
|
|
250
|
+
await PRIVATE_RED.nodes.loadFlows(true)
|
|
251
|
+
|
|
252
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Manual reload completed successfully")
|
|
253
|
+
|
|
254
|
+
res.json({
|
|
255
|
+
success: true,
|
|
256
|
+
message: "Functions and templates reloaded successfully"
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
} catch (error) {
|
|
260
|
+
RED.log.error(`[node-red-contrib-flow-splitter-extended] Manual reload failed: ${error.message}`)
|
|
261
|
+
res.status(500).json({
|
|
262
|
+
success: false,
|
|
263
|
+
error: error.message
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Main function executed on each flow restart
|
|
270
|
+
* @param {FlowStartedEventType} flowEventData
|
|
271
|
+
* @returns {void}
|
|
272
|
+
*/
|
|
273
|
+
async function onFlowReload(flowEventData) {
|
|
274
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Flow restart event")
|
|
275
|
+
|
|
276
|
+
const projectPath = getProjectPath()
|
|
277
|
+
const cfg = loadSplitterConfig(projectPath)
|
|
278
|
+
|
|
279
|
+
if (flowEventData.config.flows.length === 0) {
|
|
280
|
+
// The flow file does not exist or is empty - rebuild from split files
|
|
281
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Rebuilding monolith file from source files")
|
|
282
|
+
|
|
283
|
+
collectFunctionsTemplatesIntoSplitFiles(cfg, projectPath)
|
|
284
|
+
|
|
285
|
+
const flowSet = manager.constructFlowSetFromTreeFiles(cfg, projectPath)
|
|
286
|
+
|
|
287
|
+
if (!flowSet) {
|
|
288
|
+
RED.log.error("[node-red-contrib-flow-splitter-extended] Cannot build FlowSet from source tree files")
|
|
289
|
+
return
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const updatedCfg = manager.constructMonolithFileFromFlowSet(flowSet, cfg, projectPath, false)
|
|
293
|
+
writeSplitterConfig(updatedCfg, projectPath)
|
|
294
|
+
|
|
295
|
+
const PRIVATE_RED = getPrivateRED()
|
|
296
|
+
|
|
297
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Stopping and loading nodes")
|
|
298
|
+
|
|
299
|
+
PRIVATE_RED.nodes.loadFlows(true).then(function () {
|
|
300
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Flows are rebuilt and available")
|
|
301
|
+
})
|
|
302
|
+
return
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Flows exist - split into source files
|
|
306
|
+
const flowSet = manager.constructFlowSetFromMonolithObject(flowEventData.config.flows)
|
|
307
|
+
|
|
308
|
+
const updatedCfg = manager.constructTreeFilesFromFlowSet(flowSet, cfg, projectPath)
|
|
309
|
+
writeSplitterConfig(updatedCfg, projectPath)
|
|
310
|
+
|
|
311
|
+
extractFunctionsTemplatesFromSplitFiles(updatedCfg, projectPath)
|
|
312
|
+
|
|
313
|
+
try {
|
|
314
|
+
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
|
|
315
|
+
await delay(150) // Wait for Node-RED to create the flowFile before erasing it
|
|
316
|
+
fs.unlinkSync(path.join(projectPath, RED.settings.flowFile))
|
|
317
|
+
} catch (error) {
|
|
318
|
+
RED.log.warn(`[node-red-contrib-flow-splitter-extended] Cannot erase file '${RED.settings.flowFile}': ${error.message}`)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @param {REDType} REDRuntime
|
|
324
|
+
*/
|
|
325
|
+
module.exports = function (REDRuntime) {
|
|
326
|
+
RED = REDRuntime
|
|
327
|
+
|
|
328
|
+
// Register the plugin for Node-RED
|
|
329
|
+
RED.plugins.registerPlugin("node-red-contrib-flow-splitter-extended", {
|
|
330
|
+
type: "exotec-deploy-plugins",
|
|
331
|
+
onadd: function () {
|
|
332
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Initialized plugin successfully")
|
|
333
|
+
}
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
// Register HTTP endpoint for manual reload
|
|
337
|
+
RED.httpAdmin.post("/flow-splitter/reload", manualReload)
|
|
338
|
+
RED.log.info("[node-red-contrib-flow-splitter-extended] Manual reload endpoint registered at POST /flow-splitter/reload")
|
|
339
|
+
|
|
340
|
+
// Code to launch on every restart of the flows = boot or deploy event
|
|
341
|
+
RED.events.on('flows:started', onFlowReload)
|
|
342
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vdwpsmt/node-red-contrib-flow-splitter-extended",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Split your flows.json file in individual YAML or JSON files (per tab, subflow and config-node) with optional function and ui-template node code extraction.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"node-red",
|
|
8
|
+
"flows",
|
|
9
|
+
"tabs",
|
|
10
|
+
"split",
|
|
11
|
+
"splitter",
|
|
12
|
+
"flows-split",
|
|
13
|
+
"tabs-split",
|
|
14
|
+
"flow-splitter",
|
|
15
|
+
"tabs-splitter",
|
|
16
|
+
"version-control",
|
|
17
|
+
"git",
|
|
18
|
+
"diff",
|
|
19
|
+
"svn",
|
|
20
|
+
"ide",
|
|
21
|
+
"editor",
|
|
22
|
+
"code",
|
|
23
|
+
"function",
|
|
24
|
+
"template",
|
|
25
|
+
"extract"
|
|
26
|
+
],
|
|
27
|
+
"author": "vdwpsmt",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/vdwpsmt/node-red-contrib-flow-splitter-extended.git"
|
|
31
|
+
},
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"eol": "^0.10.0",
|
|
35
|
+
"flows-file-manager": "^0.2.0",
|
|
36
|
+
"fs-extra": "^11.3.0",
|
|
37
|
+
"js-yaml": "^4.1.0",
|
|
38
|
+
"node-red": "^1.3.7"
|
|
39
|
+
},
|
|
40
|
+
"node-red": {
|
|
41
|
+
"version": ">=1.3.7",
|
|
42
|
+
"plugins": {
|
|
43
|
+
"node-red-contrib-flow-splitter-extended": "index.js"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=18.5"
|
|
48
|
+
}
|
|
49
|
+
}
|