Streamlining scripts - point to common information

Post Reply
cwswitch
Member
Posts: 78
Joined: Fri Feb 24, 2017 12:25 am

Streamlining scripts - point to common information

Post 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

Code: Select all

use commonThing from master file
All of the scripts are made and edited in the SwitchScripter
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Streamlining scripts - point to common information

Post 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
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
cwswitch
Member
Posts: 78
Joined: Fri Feb 24, 2017 12:25 am

Re: Streamlining scripts - point to common information

Post 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.
cwswitch
Member
Posts: 78
Joined: Fri Feb 24, 2017 12:25 am

Re: Streamlining scripts - point to common information

Post 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
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Streamlining scripts - point to common information

Post 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.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
cwswitch
Member
Posts: 78
Joined: Fri Feb 24, 2017 12:25 am

Re: Streamlining scripts - point to common information

Post 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.
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Streamlining scripts - point to common information

Post 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
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
cwswitch
Member
Posts: 78
Joined: Fri Feb 24, 2017 12:25 am

Re: Streamlining scripts - point to common information

Post 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.

Code: Select all

var something = "value";
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?
Post Reply