{"id":1880,"date":"2013-03-28T19:54:53","date_gmt":"2013-03-29T00:54:53","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1880"},"modified":"2013-03-28T19:54:54","modified_gmt":"2013-03-29T00:54:54","slug":"using-windows-cscript-to-compile-a-handlebar-js-template","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1880","title":{"rendered":"Using Windows CSCRIPT to compile a Handlebar.js template"},"content":{"rendered":"

I was looking for an alternative to using Node.JS for a JavaScript build process today on a Windows machine. I wanted something that relied as much on natively installed elements of a modern Windows PC as possible so that the build process would be portable.<\/p>\n

So, I broke out my rusty Windows Script Host<\/a> skills.<\/p>\n

First, I created a file called, compile.wsf <\/strong>with the following contents:<\/p>\n

\"image\"<\/a><\/p>\n

When using cscript.exe<\/strong>, you can execute a more complex combination of scripts and include other script files by using a Windows Script File<\/a>. The content of the file is an XML definition of jobs. A job represents a unit of work. If you only have one job in a file, the name won\u2019t matter as the script engine will select it by default. If you do have more than one job you\u2019d like to store in a single WSF file, you can use the \/Job:{id}<\/strong> parameter of cscript.exe<\/strong> to run a single job.<\/p>\n

Using the WSF file, you can include other script files using a script element (much like the script tag in HTML). In the example above, I\u2019ve referenced a local copy of handlebars.js<\/strong> and a custom script called compile.js<\/strong>.<\/p>\n

You can also inline script as shown above. After doing a basic check on the number of arguments provided to the script, compile<\/strong> is called, which is from the compile.js<\/strong> script reference.<\/p>\n

Compile.js<\/strong> is simple:<\/p>\n

\"image\"<\/a><\/p>\n

Using an instance of the FileSystemObject<\/a>, first the input file is verified to exist. Next, both the input and output files are opened. The Handlebars object is available globally by including it in the WSF definition and is used to precompile the contents of the input file\u2019s template definition. Once compiled, it\u2019s written to the output file and both files are closed.<\/p>\n

I threw the three files in a folder called lib<\/strong>, and created a simple batch file called handlebars.bat which called the cscript executable with the Windows Script File shown above as the first parameter and then the values of the other parameters passed along:<\/p>\n

\"image\"<\/a><\/p>\n

While this solution only works on Windows, it doesn\u2019t hurt to keep the Windows Script Host in mind when throwing together general repeatable tasks that you:<\/p>\n