Page 1 of 1
Streamlining scripts - point to common information
Posted: Thu Nov 02, 2017 11:01 pm
by cwswitch
I'm about to dip into scripting - right into the deep end.
I've inherited 30 or so scripts attached to 6 or so flows and there is a lot of repeated information, such that if one thing changed, I'd have to edit all of these occurrences.
Can I instead alter these scripts to point to a variable located in one file, so I only have to update that one file in the future?
I imagine:
master file
Code: Select all
var commonThing = common info
etc
etc
all other files
All of the scripts are made and edited in the SwitchScripter
Re: Streamlining scripts - point to common information
Posted: Fri Nov 03, 2017 1:53 am
by gabrielp
There is a way to set and get global data within scripter but I haven't really used it.
http://www.enfocus.com/manuals/Develope ... class.html
Re: Streamlining scripts - point to common information
Posted: Fri Nov 03, 2017 4:23 am
by cwswitch
Thanks for that. I had come across that same page in the PDF manual but am still yet to form a working example to build from.
Re: Streamlining scripts - point to common information
Posted: Mon Nov 06, 2017 11:54 pm
by cwswitch
I'm wondering if I can use standard JavaScript to import from another file.
Problem I have is that I'm not really confident with the scripts I have here to edit them and try this just yet.
Has anyone gone this way?
Code: Select all
//common.js
var foo = a
var bar = b
Code: Select all
//all other scripts
require('common.js');
//use foo and bar without defining them in each script
//only have to edit common.js to see results in all other scripts
Re: Streamlining scripts - point to common information
Posted: Tue Nov 07, 2017 3:37 pm
by gabrielp
You can't use require in Scripter, since that's a node thing. Remember, Scripter uses an old version of ecmascript so lots of modern stuff is not there.
To do this, you would have to use the environment/file classes to open the .js file, then use
eval https://developer.mozilla.org/en-US/doc ... jects/eval
But there are other ways of solving this problem in Switch. Instead of writing a common config for all 30 scripts, you instead of could carry this config information on the job itself with private data (
https://github.com/open-automation/swit ... data-write). You could pass jobs through a flow at the beginning which would bootstrap them with all of the config data as private data variables, then feed the job into the other flows which would look for that data. Then, you also have the advantage of having those flows/scripts being able to service different config values without changing them since it's all reading from variables.
Re: Streamlining scripts - point to common information
Posted: Tue Nov 07, 2017 9:55 pm
by cwswitch
I love your Private data Write and use it very frequently.
These flows do not talk to one another, the MIS sends to whichever it needs and the flow always returns to the MIS, but I do think you're encouraging me in the right direction - at least towards something I understand!
I think then I'll need to edit the scripts to look for a particular Private Data Key and I'm hoping that I can pull that info into the PD key so that the result is reusable and scalable.
I hope! Thanks for the ideas.
Re: Streamlining scripts - point to common information
Posted: Tue Nov 07, 2017 10:03 pm
by gabrielp
cwswitch wrote:
I think then I'll need to edit the scripts to look for a particular Private Data Key and I'm hoping that I can pull that info into the PD key so that the result is reusable and scalable.
On the right track, but you'll want those scripts to take an input variable which you can edit in the flow. Then in the flow editor you set those private data keys there, so the keys aren't hard coded into the script.
When you get some time, check out the talk on functional flow design with Portals I did for the enfocus safari:
https://github.com/open-automation/switch-portals
Re: Streamlining scripts - point to common information
Posted: Wed Nov 08, 2017 12:19 am
by cwswitch
I got a copy of Portals a little while after finding Private Data Write and have used it at a very entry level so know some of the benefits. I just learned about it a bit late to add it to my past flows that were already built with conventional methods. I read through the slideshow on your link and will certainly use it down the line having seen first hand the spaghetti junction flows you aim to solve with it.
Re you last point though, I'm lost here.. (sorry if this is hard to read, I'm likely overcomplicating a simple issue due to lack of knowledge).
gabrielp wrote:..you'll want those scripts to take an input variable which you can edit in the flow. Then in the flow editor you set those private data keys there, so the keys aren't hard coded into the script.
the existing scripts refer to the same things over and over.
my thoughts were to make this
Code: Select all
var unsure of code or syntax because of above rant "somePrivateDataKey"
and in Switch I'd have a Private Data thing grab some value and place it in somePrivateDataKey for the scripts to then grab as required. So when I'd edited the scripts to refer to Private Data Keys I'd only then be adjusting the contents of these variables once in Switch Designer and never going back to the multiple scripts.
- not right it seems?