A simple flow to manage flows.json version (non-GIT)

Okay, I know there is the project feature, git-enabled, etc... But I have some issues with it that I will raise in a separate thread.

In the meantime, here is a little flow that simply computes a hash on flows.json and stores it in a flows.version file, bumping up the build number on restart if it has changed since last time.

Easy way 101 beginner to version control...

[{"id":"3da39f6c.10532","type":"file in","z":"9fa6a4e4.0490b8","name":"","filename":"/data/flows.json","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":840,"y":120,"wires":[["9f9337f6.065878"]]},{"id":"9f9337f6.065878","type":"function","z":"9fa6a4e4.0490b8","name":"get hash","func":"const hash = function(s) {\n    var hash = 0;\n    if (s.length === 0) {\n        return hash;\n    }\n    for (var i = 0; i < s.length; i++) {\n        var char = s.charCodeAt(i);\n        hash = ((hash<<5)-hash)+char;\n        hash = hash & hash; // Convert to 32bit integer\n    }\n    return hash;\n}\n\nmsg.payload = hash(msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":1020,"y":120,"wires":[["7c87b710.8896e8"]]},{"id":"7eecb9ba.399378","type":"file in","z":"9fa6a4e4.0490b8","name":"","filename":"/data/flows.version","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":330,"y":120,"wires":[["ed54b330.4018c"]]},{"id":"650f6709.7c2458","type":"inject","z":"9fa6a4e4.0490b8","name":"","repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":120,"wires":[["7eecb9ba.399378"]]},{"id":"7d4b039.66f20fc","type":"file","z":"9fa6a4e4.0490b8","name":"","filename":"/data/flows.version","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":1430,"y":120,"wires":[[]]},{"id":"9cb59dfd.4041a","type":"catch","z":"9fa6a4e4.0490b8","name":"","scope":["7eecb9ba.399378","ed54b330.4018c"],"uncaught":false,"x":670,"y":160,"wires":[["3da39f6c.10532","d055379d.c7c748"]]},{"id":"6830dfe7.3b4e8","type":"change","z":"9fa6a4e4.0490b8","name":"","rules":[{"t":"set","p":"build","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":120,"wires":[["3da39f6c.10532"]]},{"id":"7c87b710.8896e8","type":"function","z":"9fa6a4e4.0490b8","name":"Update build number","func":"let build = global.get(\"build\");\nlet h = msg.payload; // hash of flows.json\n\nlet v = 1.0;\n\nif (typeof build !== 'undefined') {\n    if (build.hash !== h) {\n        // hash has changed, bump up version\n        v = parseFloat(build.version) + 0.1;\n    } // else we don't change the version\n}\n\nbuild = { \n    \"version\": v.toFixed(5), \n    \"hash\": h, \n    \"hostname\": global.get(\"hostname\"),\n    \"date\": new Date().toISOString()\n};\n\nglobal.set(\"build\", build);\n\nmsg.payload = build;\n\nreturn msg;","outputs":1,"noerr":0,"x":1220,"y":120,"wires":[["7d4b039.66f20fc","9c5883b.dc0f88"]]},{"id":"9c5883b.dc0f88","type":"debug","z":"9fa6a4e4.0490b8","name":"Flow Version","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":" \"Current Flow Version: \" & payload.version","targetType":"jsonata","x":1410,"y":80,"wires":[]},{"id":"ed54b330.4018c","type":"json","z":"9fa6a4e4.0490b8","name":"","property":"payload","action":"","pretty":false,"x":490,"y":120,"wires":[["6830dfe7.3b4e8"]]},{"id":"d055379d.c7c748","type":"debug","z":"9fa6a4e4.0490b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":780,"y":240,"wires":[]}]
1 Like