Function Node exported json

Hi all!

I'm new here and new to Node-RED. I don't know if this is the common way to do this, but in my team we export all the flows to a flows.json file and use this to deploy the apps.

While exporting it, I noticed the content of a function ends up in only one line. Example:

[
    {
        "id": "3fc0691543bbac9f",
        "type": "function",
        "z": "1a93fafb2ad45337",
        "name": "function 1",
        "func": "msg.test = 'hey, listen!'\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 200,
        "y": 100,
        "wires": [
            []
        ]
    }
]

We use git to version this. And if there is a change in only one line of the code, it is hard to check it while deciding by add or not to the commits, even for codes with just few lines. I was thinking about opening a PR for change the function export, making "func" to be exported to an array of lines. Like this:

[
    {
        "id": "3fc0691543bbac9f",
        "type": "function",
        "z": "1a93fafb2ad45337",
        "name": "function 1",
        "func": [
            "msg.test = 'hey, listen!'",
            "return msg;"
        ],
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 200,
        "y": 100,
        "wires": [
            []
        ]
    }
]

Given it is a big change, seems fair to give an option at the settings of each project to decide if the functions are exported into array or one line. But it would make it easier to check the code while committing.

I would have to take a time to study where to make all these changes. I came here because I read the terms in the github and it says we must discuss things here before run to the code and change stuff. Already looked for similar topic, could not find it. If it was already raised, sorry for that.

This would be a major breaking change of the flow file format, so not something we can consider lightly.

We've discussed a few variations of this type of thing in the past, but not to any definitive conclusion.

Another common request is to be able to natively split the flow file up into a file per tab and to support saving function/template code in their own files.

It's something I'd like to evaluate for Node-RED 4.0 this summer.

If you run this script it will unpack the functions into multiple lines into a file called flows.json.formatted which can then be committed your git repo alongside the flows file. To see the diffs you can run the diff on the formatted file. No doubt one could use git triggers to run the script automatically, but I have never got round to working out how to do that.

#!/usr/bin/env sh

# generates formatted versions of node red flow files matching flow*.json
# in files flow*.json.formatted

# NOTE must have flowFilePretty: true in settings.js

for f in flow*.json; do
  echo $f
  sed -r 's/\\n/\n/g' "$f" > "$f.formatted"
done
# remove any cred files converted
rm *_cred.json.formatted

Yeah, it would. That's why I suggested the configuration. And, yes, seems something to add to a new version. But is this a bad idea? Or is this something I can open a PR and someday get it to production code and make other developer lives easier?

I actually did not considered using scripts to change it locally. Maybe it is a good thing. But I would use something maybe with a higher level language so I could interpret json and export only the function things, and maybe some format that did not break the json format or maybe only export the functions to js files so I can check.

In the matter of automating with git, there is a tool called husky that you can use so you don't need to make everyone execute the pre-commit configuration manually.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.