Introduction of hooks


Hooks are a new addition in SSJS Manager v0.6.1. They can help you automate your work, by running commands on save.

Hooks? Like captain Hook?

No, not like that. Hooks are a way to run actions on specific events in programming. In SSJS Manager, you can run any command line (terminal) command on save. This can be useful for formatting your code, linting, or even running tests. My favorite use case is to automatically compile my JS code to SSJS using the generator-ssjs and uploading it to SFMC.

How to?

This is not going to get much technical. Have you set your command yet? If not, install the Command Line Interface. Use npm, yarn, brew or whatever you like. Feel free to setup package.json scripts if you need. For this example, I will use generator-ssjs and npm script build.

Open the .vscode/ssjs-setup.json file in your project. Add the following configuration:

{
	// ... other config
	"hooks": {
		// triggering action (currently only on-save)
		"on-save": {
			// file extension (dot is mandatory)
			".js": {
				// is hook enabled
				"enabled": true,
				// command to run
				"command": "npm run build",
				// what to do with the resulting file? "upload-self", "upload-output", "none"
				"success-handling": "upload-output",
				// where to find file from workspace root (name is without extension)
				"output-file": "./dist/{{name}}.ssjs"
			}
		}
	}
	// ... other config
}

Now, configure the file extensions and commands you want to run. You can also specify the success handling (if you want to use the original file or the output file). The output file is a template string, where you can use {{name}} to get the name of the file without the extension (NOTE: don’t forget to change the file extension if needed!). And you can of course disable the hook by setting enabled to false!

That’s it! Now, every time you save a JS file, the command will run. The output of the command will also show in the Debug Console of VS Code.

Finally, the output file works with other commands (like SSJS: Run or SSJS: Upload Script to Dev commands).

New command

Also, this version introduces new command SSJS: Get Standalone Run Script. This new feature gets you a piece of AMPscript that you can put in your Cloud Page to directly include your SSJS / AMPscript code.

How about that?

Hooks are a powerful tool to automate your work. I hope you will find them useful. They are speeding up my workflow a lot when I use them with generator-ssjs.

Let me know what you think!