Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This may help you developing for Node RED. Instead of having to write your Javas
## Status
What? | Status | What? | Status
------- | ------ | ------- | ------
Code Climate GPA | [![Code Climate](https://codeclimate.com/github/emiloberg/node-red-contrib-file-function/badges/gpa.svg)](https://codeclimate.com/github/emiloberg/node-red-contrib-file-function) | Licence | [![Licence](https://img.shields.io/npm/l/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/blob/master/LICENSE)
Code Climate GPA | [![Code Climate](https://codeclimate.com/github/emiloberg/node-red-contrib-file-function/badges/gpa.svg)](https://codeclimate.com/github/emiloberg/node-red-contrib-file-function) | License | [![License](https://img.shields.io/npm/l/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/blob/master/LICENSE)
Codacy | [![Codacy Badge](https://www.codacy.com/project/badge/f51ca088d01f4af6b83ed2e2529b51dd)](https://www.codacy.com/public/emiloberg/node-red-contrib-file-function) | Tag | [![Tag](https://img.shields.io/github/tag/emiloberg/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/tags)
Issues | [![Issues](https://img.shields.io/github/issues/emiloberg/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/issues) | GitHub Forks | [![Forks](https://img.shields.io/github/forks/emiloberg/node-red-contrib-file-function.svg)](https://github.com/emiloberg/node-red-contrib-file-function/network)
GitHub Version | [![GitHub version](https://badge.fury.io/gh/emiloberg%2Fnode-red-contrib-file-function.svg)](http://badge.fury.io/gh/emiloberg%2Fnode-red-contrib-file-function) | GitHub Followers | [![Followers](https://img.shields.io/github/followers/emiloberg.svg)](https://github.com/emiloberg/followers)
Expand All @@ -26,7 +26,7 @@ Either set the filename in the configuration dialog of the node, or override it
## Cache
By checking the _"Reread file from disk every time node is invoked?"_ checkbox the file will be read every time the node is called, so there's no need to redeploy or restart Node-RED. If the checkbox is unchecked, the file will be read on deploy/start.

If the checkbox is set to only read the file once (when flow is deployed/Node-RED is started) but another filename is sent in msg.filename, it will read the file from disk and cache it anyways. Only the last called file will be cached. If you're alternating between two different files you're better of creating two different nodes if you're looking for perfomance.
If the checkbox is set to only read the file once (when flow is deployed/Node-RED is started) but another filename is sent in msg.filename, it will read the file from disk and cache it anyways. Only the last called file will be cached. If you're alternating between two different files you're better of creating two different nodes if you're looking for performance.

Unless you're working with functions called __very__ often or with __very__ large functions you can probably just leave it to reload the file every time it's invoked.

Expand Down Expand Up @@ -70,4 +70,4 @@ Import this flow (or add it manually by creating a simple [inject] > [file funct
[{"id":"62efe026.9d102","type":"inject","name":"Inject","topic":"this is topic from the function","payload":"this data is feeded to the function","payloadType":"string","repeat":"","crontab":"","once":false,"x":303,"y":119,"z":"dd1ad5c3.22e528","wires":[["fd11ceda.02ee3"]]},{"id":"fd11ceda.02ee3","type":"file function","name":"","filename":"sample-file-function.js","outputs":"1","x":508,"y":119,"z":"dd1ad5c3.22e528","wires":[["7e85f5db.817a0c"]]},{"id":"7e85f5db.817a0c","type":"debug","name":"","active":true,"console":"false","complete":"true","x":723,"y":118,"z":"dd1ad5c3.22e528","wires":[]}]
```

Deploy the changes and click the inject node - check the output in the debug sidebar!
Deploy the changes and click the inject node - check the output in the debug sidebar!
146 changes: 79 additions & 67 deletions file-function/file-function.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,89 @@
limitations under the License.
-->

<script type="text/x-red" data-template-name="file function">
<div class="form-row node-input-filename">
<label for="node-input-filename"><i class="fa fa-file"></i> Filename</label>
<input type="text" id="node-input-filename" placeholder="Filename">
</div>
<div class="form-row">
<label for="node-input-reloadfile" style="vertical-align: top"><i class="fa fa-refresh"></i> Reload</label>
<input type="checkbox" id="node-input-reloadfile" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-reloadfile" style="width: 70%;">Reread file from disk every time node is invoked?</label>
</div>
<div class="form-row">
<label for="node-input-outputs"><i class="fa fa-random"></i> Outputs</label>
<input id="node-input-outputs" style="width: 60px; height: 1.7em;" value="1">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div id="node-input-tip" class="form-tips">
The file path will be relative from the path set in <b>settings.userDir</b>,
or if not set; relative to your Node-RED root directory.
</div>
<script type="text/html" data-template-name="file-function">
<div class="form-row node-input-filename">
<label for="node-input-filename"><i class="fa fa-file"></i> Filename</label>
<input type="text" id="node-input-filename" placeholder="Filename" />
</div>
<div class="form-row">
<label for="node-input-reloadFile" style="vertical-align: top">
<i class="fa fa-refresh"></i> Reload
</label>
<input
type="checkbox"
id="node-input-reloadFile"
style="display: inline-block; width: auto; vertical-align: top;"
/>
<label for="node-input-reloadFile" style="width: 70%;">
Reread file from disk every time node is invoked?
</label>
</div>
<div class="form-row">
<label for="node-input-outputs"><i class="fa fa-random"></i> Outputs</label>
<input id="node-input-outputs" style="width: 60px; height: 1.7em;" value="1" />
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
<div id="node-input-tip" class="form-tips">
The file path will be relative from the path set in <b>settings.userDir</b>, or if not set;
relative to your Node-RED root directory.
</div>
</script>

<script type="text/x-red" data-help-name="file function">

<p>Just like the core node "function", only that this node loads the script to be
executed from an actual file on your drive.</p>

<p>This may help you developing for Node RED. Instead of having to write your Javascript
code in that small textfield in your browser you can use your favorite editor/IDE.</p>

<p>The filename can be overridden by the <code>msg.filename</code> property of the incoming message.</p>

<p>The file path will be relative from the path set in <code>settings.userDir</code>,
or if not set; relative to your Node-RED root directory.</p>

<p>Writing functions in this node works works just like functions in the the original
function node (except that you write it in an actual file and no in an input field)</p>

<p>See the <a target="_blank" href="https://github.com/emiloberg/node-red-contrib-file-function">online documentation for more help</a>.</p>

<p>See the <a target="_new" href="http://nodered.org/docs/writing-functions.html">Node-RED documentation on functions</a> for more help on functions.</p>



<script type="text/html" data-help-name="file-function">
<p>
Just like the core node "function", only that this node loads the script to be executed from an
actual file on your drive.
</p>
<p>
This may help you developing for Node RED. Instead of having to write your Javascript code in
that small textfield in your browser you can use your favorite editor/IDE.
</p>
<p>
The filename can be overridden by the <code>msg.filename</code> property of the incoming
message.
</p>
<p>
The file path will be relative from the path set in <code>settings.userDir</code>, or if not
set; relative to your Node-RED root directory.
</p>
<p>
Writing functions in this node works works just like functions in the the original function node
(except that you write it in an actual file and no in an input field)
</p>
</script>

<script type="text/javascript">
RED.nodes.registerType('file function',{
color:"#fdd0a2",
category: 'function',
defaults: {
name: {value:""},
filename: {value:""},
reloadfile: {value:true},
outputs: {value:1}
},
inputs:1,
outputs:1,
icon: "file-function-icon.png",
label: function() {
return this.name || this.filename || 'file function';
},
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function() {
$( "#node-input-outputs" ).spinner({
min:1
});
RED.nodes.registerType('file-function', {
color: '#fdd0a2',
category: 'function',
defaults: {
name: { value: '' },
filename: {
value: '',
validate(filename) {
return filename.length
}
});
},
reloadFile: { value: true },
outputs: { value: 1 }
},
inputs: 1,
outputs: 1,
icon: 'font-awesome/fa-file-code-o',
label: function () {
return this.name || this.filename || 'File Function'
},
labelStyle: function () {
return this.name ? 'node_label_italic' : ''
},
oneditprepare: function () {
$('#node-input-outputs').spinner({
min: 1
})
}
})
</script>
Loading