require-js and app framework for Windows Scripting Host JavaScript
wsh-appjs
provides a small application framework for working with JavaScript and Windows Scripting Host.
If, like me, you like writing JavaScript and don't want to learn PowerShell or install Python, then JavaScript
is a very capable command prompt scripting language.
However, WSH lacks support for writing modular JavaScript, which is where wsh-appjs
comes in. wsh-appjs
is
a very small framework that provides a require()
function that allows the loading of JavaScript modules by other
JavaScript modules.
Using wsh-appjs
means that code can now be shared and reused. See the lib subfolder for some examples.
git clone https://github.com/redskyit/wsh-appjs
An app is just another wsh-appjs
module that also exports a main()
method. Once loaded the main()
method is called.
var LIB = require('lib/std');
return {
main: function(argv) {
// your app goes here
DBG("Hello World");
return 0;
}
}
cscript app.js ./myapp
All wsh-appjs
modules follow the same pattern, are run in their own scope and may or may not return an
object, function or other non-zero, null, undefined value. A common module pattern is:
// my module
var LIB = require('lib/std');
var interface = { global: global, VERSIONINFO: "mymodule v1" };
// define rest of interface here
return interface;
But that pattern isn't a the only pattern, a module can return a function (constructor) for instance or even some static data.
This library provides some standard methods and also some global convenience methods.
var LIB = require('lib/std');
DBG
is a global function that will output the passed string to standard output. It is a convenient shortcut to WScript.echo()
.
Adds an indexOf
method to array objects.
Works like Server.CreateObject, it is really just an alias for new ActiveXObject(name)
.
This library provides a sendmail function that uses wither CDO (the default) or Persists.MailSender
to send an email.
var MAIL = require('lib/sendmail');
Sends a mail based on the supplied mailmsg
object. The mailmsg
object can have the following properties.
Prop | Description |
---|---|
To | The to email address |
From | The from email address |
Cc | The CC list |
ReplyTo | A reply-to address |
Subject | The email Subject |
Body | The email body |
isHTML | true if the body is an html email message. |
id | A message ID |
MAILHOST | The mail server to send the message to |
MAILPORT | The port number to use (CDO only) |
This library contains a bunch of convenience wrappers for Scripting.FileSystemObject
Scripting.FileSystemObject, FileExists, FolderExists, GetFile, ReadAll, OpenTextFile, Write, ADODB.Stream, WriteText, MoveFile, CreateFolder
This library contains a simple SQL interface based on ADODB.Connection
and ADODB.Recordset
.
This returns a database object to the opened database. The object provides the following methods:
method | Description |
---|---|
query(sql) |
Run the SQL query and return an ADODB.Recordset instance with the results. |
exec(sql) |
Run the SQL statement and return an ADODB.Recordset object if successful. |
insert(table, cols) |
Use a recordset addNew() to insert a record. cols should be a { name: value, ... } hash map. |
close() |
Close the datebase connection |
reopen() |
Reopen the datebase connection |
Quotes a string for inclusion in a SQL string.
Converts a database BLOB to text based on charset.
Writes a database BLOB field to the named file using ADODB.Stream
.
ADODB.Connection, ADODB.Recordset, Open, Execute, AddNew
- At the moment, a
require()
will only find modules relative to the folder thatapp.js
is located in. See Issue #1. Ideally modules should be loaded relative to the requiring module.