Skip to content

Commit

Permalink
Add options to use cli.pl instead of pre-compiled cli.exe in node.js …
Browse files Browse the repository at this point in the history
…interface (workaround for issue #87)
  • Loading branch information
fnogatz committed May 9, 2018
1 parent 49ebf13 commit 899b839
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ xsd2json(filename, function(err, schemaObject) {
});
```

In addition to the [command line options provided by the Prolog module](https://github.com/fnogatz/xsd2json/tree/master/lib-pl#synopsis), there are the following options available in the node.js module:
- `noExe: true | false (default)`: Use the native Prolog interface instead of the pre-compiled `cli.exe`. This might be useful for MacOS users (see [issue #87](https://github.com/fnogatz/xsd2json/issues/87) for more details).
- `swi: 'swipl' (default)`: Executable to call SWI-Prolog.

## Usage with Prolog

xsd2json provides a predicate `xsd2json(+XSD,-JSON)`, which holds for a given XML Schema (either file path, URL or `stream`). For instructions on how to use xsd2json programmatically in Prolog, have a look at the Prolog module's [Readme](https://github.com/fnogatz/xsd2json/tree/master/lib-pl).
Expand Down
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ var concat = require('concat-stream')
var path = require('path')

var CLI = path.resolve(__dirname, 'lib-pl', 'cli.exe')
var CLIPL = path.resolve(__dirname, 'lib-pl', 'cli.pl')
var SWI = 'swipl'

var reservedKeys = [
'noExe',
'swi'
]

function xsd2json (filename, options, callback) {
if (arguments.length === 1) {
Expand All @@ -21,11 +28,27 @@ function xsd2json (filename, options, callback) {

var spawnArgs = []
for (var key in options) {
if (reservedKeys.indexOf(key) >= 0) {
continue
}

spawnArgs.push('--' + key + '=' + options[key])
}
spawnArgs.push(filename)

var outputStream = childProcess.spawn(CLI, spawnArgs)
var outputStream
if (options.noExe) {
spawnArgs = [
'-g',
'main',
CLIPL,
'--'
].concat(spawnArgs)

outputStream = childProcess.spawn(options.swi || SWI, spawnArgs)
} else {
outputStream = childProcess.spawn(CLI, spawnArgs)
}

if (typeof callback !== 'function') {
// no callback given --> return stream
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"prepublish": "git checkout -- lib-pl/cli.exe",
"create-qlf": "swipl -O --goal=main -o lib-pl/cli.exe -c lib-pl/cli.pl",
"standard": "standard",
"test": "npm run standard && npm run test-cli && npm run test-converter",
"test": "npm run standard && npm run test-cli && npm run test-converter && npm run test-api",
"test-cli": "./lib-pl/cli.exe --version && ./lib-pl/cli.exe test/xsd/all_element.xsd",
"test-converter": "node test/index.js interpreted",
"test-examples": "node test/index.js validate-json"
"test-examples": "node test/index.js validate-json",
"test-api": "node test/api.js"
},
"keywords": [
"XML Schema",
Expand Down
39 changes: 39 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var assert = require('assert')
var path = require('path')

var async = require('async')

var xsd2json = require('../index')

async.parallel([
function (cb) {
xsd2json(path.resolve(__dirname, 'xsd', 'schema.xsd'), {}, function (err, res) {
if (err) {
process.exit(1)
}

assert.deepStrictEqual(res, { type: 'string' })

cb(null, res)
})
},
function (cb) {
xsd2json(path.resolve(__dirname, 'xsd', 'schema.xsd'), { noExe: true }, function (err, res) {
if (err) {
console.log(err)
process.exit(1)
}

assert.deepStrictEqual(res, { type: 'string' })

cb(null, res)
})
}
], function (err, results) {
if (err) {
process.exit(1)
}

console.log(results)
process.exit(0)
})

0 comments on commit 899b839

Please sign in to comment.