diff --git a/.gitignore b/.gitignore index 136408a..eae1150 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -xml/ \ No newline at end of file +node_modules/ +xml/ diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..9741884 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,79 @@ +/*jshint node: true */ + +'use strict'; + +var gulp = require('gulp'); + +var argv = require('yargs').argv; +var fs = require('fs'); +var request = require('request'); +var through = require('through2'); +var typescript = require('gulp-tsc'); + +var onError = function(err) { + if (err) { + console.log(err); + } +}; + +var options = { + mlHost: argv['ml-host'] || 'ml8-ml1', + mlPort: argv['ml-port'] || '8000', + mlUser: argv['ml-user'] || 'admin', + mlPass: argv['ml-pass'] || 'admin' +}; + + +gulp.task('generate', function(){ + var query; + + fs.readFile('qconsole/generate-definitions.xqy', null, function(err, data) { + query = data.toString(); + }); + + return gulp.src(['xml/**/*.xml']) + .pipe(through.obj(function (file, enc, cb) { + + console.log(file.path); + + var outFile = file.path.replace('/xml/', '/ts/').replace('.xml', '.d.ts'); + + var xml = file.contents.toString(); + var escapedXml = xml.replace(/\\/gm, '\\\\').replace(/"/gm, '\\"').replace(/(\n\r|\r\n|\n|\r)/gm, '\\n').replace(/\t/gm, '\\t'); + + request({ + method: 'POST', + url: 'http://' + options.mlHost + ':' + options.mlPort + '/v1/eval', + form: { + xquery: query, + vars: '{ xml: "'+ escapedXml +'"}' + }, + auth: { + user: options.mlUser, + pass: options.mlPass, + sendImmediately: false + } + }, function(err, httpResponse, body) { + // get rid of multipart response wrapping + body = body.replace(/^([^\r]*\r\n){5}/, '').replace(/\r\n[^\r]*\r\n$/, ''); + + if (err || httpResponse.statusCode !== 200) { + console.log('FAILED!'); + console.log(body); + //console.log(escapedXml); + } else { + fs.writeFile(outFile, body, onError); + } + }); + + cb(null, file); + })); +}); + +gulp.task('validate', function(){ + gulp.src(['ts/**/*.ts']) + .pipe(typescript()); +}); + +// Default Task +gulp.task('default', ['generate', 'validate']); diff --git a/package.json b/package.json index 266aac8..fae80c6 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,13 @@ "typescript", "marklogic" ], - "dependencies": { - }, + "dependencies": {}, "devDependencies": { + "gulp": "^3.8.11", + "gulp-tsc": "^0.9.5", + "yargs": "^3.9.1", + "request": "^2.55.0", + "through2": "^0.6.5" }, "engine": "node >= 0.4.1", "scripts": { diff --git a/qconsole/generate-definitions.xqy b/qconsole/generate-definitions.xqy index fb47bf5..67ed8a5 100644 --- a/qconsole/generate-definitions.xqy +++ b/qconsole/generate-definitions.xqy @@ -1,10 +1,15 @@ xquery version "1.0-ml"; +(: let $docs-dir := "/Users/gjosten/Projects/github-grtjn/marklogic-typescript-definitions/xml/" let $docs-files := xdmp:filesystem-directory($docs-dir)/*[ends-with(*:filename, ".xml")]/*:filename for $file in $docs-files let $xml := xdmp:document-get(concat($docs-dir, $file)) +:) + +declare variable $xml external; +let $xml := xdmp:unquote($xml) let $definition := xdmp:xslt-eval( @@ -25,7 +30,7 @@ let $definition := xdmp:xslt-eval( ( - + ? : @@ -38,6 +43,8 @@ let $definition := xdmp:xslt-eval( + + // Type definitions for // Definitions: @@ -49,44 +56,70 @@ let $definition := xdmp:xslt-eval( {{ - interface - - {{ + + + interface + + {{ - + - }} }} + }} + + + }} - + + + + + Node void - any - number + String + Array + Object + () => any + Date + Number + Node + - + + Object - - - - - + + defaultVal + functionArg + + + + + + - - - + + + - + + + , $xml) - -return xdmp:save(concat(replace($docs-dir, 'xml/$', 'ts/'), replace($file, '\.xml$', ''), '.d.ts'), $definition) \ No newline at end of file + +(: +return xdmp:save(concat(replace($docs-dir, 'xml/$', 'ts/'), replace($file, '\.xml$', ''), '.d.ts'), $definition) +:) +return $definition \ No newline at end of file