https://github.com/vweevers/jscript
JScript runner as a duplex stream
https://github.com/vweevers/jscript
Last synced: 8 months ago
JSON representation
JScript runner as a duplex stream
- Host: GitHub
- URL: https://github.com/vweevers/jscript
- Owner: vweevers
- License: mit
- Created: 2016-03-20T22:53:29.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-12-10T22:03:38.000Z (over 9 years ago)
- Last Synced: 2025-03-28T21:39:06.454Z (about 1 year ago)
- Language: JavaScript
- Size: 19.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# jscript
**[JScript](https://en.wikipedia.org/wiki/JScript) runner: a duplex stream wrapper around [cscript](https://technet.microsoft.com/en-us/library/bb490887.aspx) with optional JSON serialization for easy marshalling of data between Node.js and [Windows Script Host](https://en.wikipedia.org/wiki/Windows_Script_Host).**
[](https://www.npmjs.org/package/jscript) [](https://ci.appveyor.com/project/vweevers/jscript) [](https://david-dm.org/vweevers/jscript)
## example
Let's create a `version.js`: a JScript that reads the `FileVersion` of executables.
```js
var JSON = require('json3')
var fs = new ActiveXObject('Scripting.FileSystemObject')
while(!WScript.StdIn.AtEndOfStream) {
var file = JSON.parse(WScript.StdIn.ReadLine())
if (fs.FileExists(file.path)) {
file.version = fs.GetFileVersion(file.path)
}
WScript.StdOut.Write(JSON.stringify(file) + '\n')
}
```
Install this script's dependencies and create a bundle with [jscriptify](https://www.npmjs.com/package/jscriptify):
```bash
npm install json3
npm install -g jscriptify
jscriptify version.js > bundle.js
```
Last but not least, an `example.js`:
```js
var jscript = require('jscript')
var path = require('path')
var duplex = jscript('bundle.js', { json: true })
duplex.on('data', function(file) {
console.log(file.version)
})
process.argv.slice(2).forEach(function (file) {
duplex.write({ path: path.resolve(file) })
})
duplex.end()
```
And run it with Node.js:
```bash
> node example one.exe two.exe
16.4.8
6.2.3
```
*Note: avoid `StdOut.WriteLine()` because `json-stream` doesn't like CRLF.*
## `jscript(file, [opts])`
- **file**: relative or absolute path to a JScript file.
Options:
- **args** (array): script arguments
- **json** (boolean): if true, wrap stream with JSON serialization
- **debug** (boolean): if true, pipe stderr to process.stderr;
- **native**: if false, don't escape WoW64 redirection (passed to [windows-bin](https://www.npmjs.com/package/windows-bin)).
## install
With [npm](https://npmjs.org) do:
```
npm install jscript
```
## changelog
### 2.0.0
- Drop support of Node 0.10 and 0.12, add 6 and 7
- Emit error on non-zero exit code;
- Remove `wrap` option. Use `{ json: true }` instead.
## license
[MIT](http://opensource.org/licenses/MIT) © Vincent Weevers