From 899b839e524d66e33dcc8e7e098b63d91f5cd26e Mon Sep 17 00:00:00 2001 From: Falco Nogatz Date: Wed, 9 May 2018 16:42:55 +0200 Subject: [PATCH] Add options to use cli.pl instead of pre-compiled cli.exe in node.js interface (workaround for issue #87) --- README.md | 4 ++++ index.js | 25 ++++++++++++++++++++++++- package.json | 5 +++-- test/api.js | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 test/api.js diff --git a/README.md b/README.md index bdfd647..814b9ab 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/index.js b/index.js index b49dd61..9ffd74c 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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 diff --git a/package.json b/package.json index 6d8aad2..a16a428 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/api.js b/test/api.js new file mode 100644 index 0000000..165e7f3 --- /dev/null +++ b/test/api.js @@ -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) +})