UNPKG

xquery-cli

Version:

A command-line tool for XQuery

3 lines (2 loc) 17.7 kB
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("fontoxpath-module-loader"),r=e(require("fs/promises")),n=require("slimdom-sax-parser"),o=e(require("child_process")),i=require("events"),a=e(require("glob")),s=e(require("npmlog")),u=e(require("path")),c=require("util"),l=e(require("os"));function f(e,t,r,n,o,i,a){try{var s=e[i](a),u=s.value}catch(e){return void r(e)}s.done?t(u):Promise.resolve(u).then(n,o)}function p(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var i=e.apply(t,r);function a(e){f(i,n,o,a,s,"next",e)}function s(e){f(i,n,o,a,s,"throw",e)}a(void 0)}))}}function h(){return(h=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}var d,v=(function(e){var t=function(e){var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function s(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{s({},"")}catch(e){s=function(e,t,r){return e[t]=r}}function u(e,t,r,n){var o=Object.create((t&&t.prototype instanceof f?t:f).prototype),i=new E(n||[]);return o._invoke=function(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var s=x(a,r);if(s){if(s===l)continue;return s}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=c(e,t,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(e,r,i),o}function c(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=u;var l={};function f(){}function p(){}function h(){}var d={};s(d,o,(function(){return this}));var v=Object.getPrototypeOf,m=v&&v(v(L([])));m&&m!==t&&r.call(m,o)&&(d=m);var y=h.prototype=f.prototype=Object.create(d);function g(e){["next","throw","return"].forEach((function(t){s(e,t,(function(e){return this._invoke(t,e)}))}))}function b(e,t){var n;this._invoke=function(o,i){function a(){return new t((function(n,a){!function n(o,i,a,s){var u=c(e[o],e,i);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==typeof f&&r.call(f,"__await")?t.resolve(f.__await).then((function(e){n("next",e,a,s)}),(function(e){n("throw",e,a,s)})):t.resolve(f).then((function(e){l.value=e,a(l)}),(function(e){return n("throw",e,a,s)}))}s(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function x(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,x(e,t),"throw"===t.method))return l;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=c(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,l;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,l):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,l)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function k(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function E(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,i=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:O}}function O(){return{value:void 0,done:!0}}return p.prototype=h,s(y,"constructor",h),s(h,"constructor",p),p.displayName=s(h,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===p||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,h):(e.__proto__=h,s(e,a,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},g(b.prototype),s(b.prototype,i,(function(){return this})),e.AsyncIterator=b,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new b(u(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},g(y),s(y,a,"Generator"),s(y,o,(function(){return this})),s(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=L,E.prototype={constructor:E,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var s=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(s&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),l},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),k(r),l}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;k(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:L(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},e}(e.exports);try{regeneratorRuntime=t}catch(e){"object"==typeof globalThis?globalThis.regeneratorRuntime=t:Function("r","regeneratorRuntime = r")(t)}}(d={exports:{}}),d.exports),m=function(e,t,r){return function(){var n=p(v.mark((function n(a){var s,u,c,l;return v.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return s=a.length>t.batchSize?a.slice(0,t.batchSize):a,u=a.length>t.batchSize?a.slice(t.batchSize):[],c=0,(l=o.fork(e)).on("message",(function(e){if(e)return r(e,c++);l.send({type:"kill"})})),l.send(h({type:"run"},t,{files:s})),n.next=8,new Promise((function(e,t){l.on("close",(function(r){return r?t(r):e()}))}));case 8:if(!u.length){n.next=11;break}return n.next=11,i(u);case 11:case"end":return n.stop()}}),n)})));function i(e){return n.apply(this,arguments)}return i}()(t.files)};function y(e){return e instanceof n.slimdom.Node?n.slimdom.serializeToWellFormedString(e):Array.isArray(e)?e.map((function(e){return y(e)})):e instanceof Date?e.toISOString():"object"==typeof e?Object.keys(e).reduce((function(t,r){var n;return Object.assign(t,((n={})[r]=y(e[r]),n))}),{}):e}function g(e,t,r,n){return b.apply(this,arguments)}function b(){return(b=p(v.mark((function e(r,o,i,a){var s,u,c;return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.fontoxpath.evaluateUpdatingExpression(r.main.contents,o||new n.slimdom.Document,null,i,a);case 2:return c=(s=e.sent).xdmValue,t.fontoxpath.executePendingUpdateList(u=s.pendingUpdateList),e.abrupt("return",{isUpdating:!!u.length,returnValue:(Array.isArray(c)?c:[c]).map(y)});case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function x(e,t,r,n){return w.apply(this,arguments)}function w(){return(w=p(v.mark((function e(t,o,i,a){var s,u;return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,r.readFile(o,"utf8");case 2:return s=n.sync(e.sent),e.next=6,g(t.modules,s,i,a);case 6:return(u=e.sent).isUpdating&&!t.isDryRun&&r.writeFile(o,n.slimdom.serializeToWellFormedString(s),"utf8"),e.abrupt("return",u);case 9:case"end":return e.stop()}}),e)})))).apply(this,arguments)}s.addLevel("rawOutput",999999,{}," ");var k=function(e,t,r){t.on("result",(function(e){var t=e.$value;if(!e.$error&&t){var n=s.stream;s.stream=r,s.rawOutput(null,E(t)),s.stream=n}})),t.on("file",(function(e){var t=e.$value;if(!e.$error&&t){s.clearProgress();var n=s.stream;s.stream=r,t.forEach((function(e){s.rawOutput(null,E(e))})),s.stream=n,s.showProgress()}}))};function E(e){return e?"string"==typeof e||"number"==typeof e?e:Array.isArray(e)?e.map((function(e){return E(e)})).join(l.EOL):"object"==typeof e?Object.keys(e).map((function(t){return e[t]})).join("\t"):e:e}var L=function(e,t,r){var n,o,i=Date.now(),a={};t.on("files",(function(e){a.files=e.length,s.info(null,"Located %s files in %s milliseconds",a.files,Date.now()-i)})),t.on("modules",(function(e){s.info(null,"Using %s main and %s library XQuery modules",e.main?1:0,e.libraries.length)})),t.on("expression",(function(e){return s.verbose(null,"Using expression:\n%s",e)})),t.on("start",(function(){s.enableProgress(),s.info(null,"Starting evaluation"),n=s.newItem("0 of "+a.files,a.files),o=Date.now()}));var u=0,c=0;function l(e,t){++c,s.error(e),(t.stack||t.message||"").split("\n").forEach((function(e,t){return t?s.error(null,e):s.error(e)}))}t.on("error",(function(e){l("Fatal error in program",e)})),t.on("result",(function(e){var t=e.$error;if(t)return l("Error in expression evaluation",t)})),t.on("file",(function(t){var r=t.$fileName,o=t.$error,i=t.$isUpdate;if(s.verbose(null,"Evaluated %s",r),n.name=++u+" of "+a.files,n.completeWork(1),i&&s.verbose(null,e.isDryRun?'Dry run: Not saving update to "'+r+'"':'Saved update to "'+r+'"'),o)return l("Error in file evaluation",o)})),t.on("end",(function(e){a.totalTime=Date.now()-o;var t=(a.totalTime/u).toFixed(2),r=(u/a.totalTime*1e3).toFixed(2);s.disableProgress(),o&&a.files?(s.info(null,"Evaluated %s out of %s files in %s milliseconds",u,a.files,a.totalTime),s.verbose(null,"%s milliseconds per document",t),s.verbose(null,"%s documents per second",r)):o&&!a.files?s.info(null,"Evaluated expression in %s milliseconds",a.totalTime):s.info(null,"Quitting before a query was evaluated"),s.info(null,"Encountered %s errors",c),e>0&&s.info(null,"Exiting process with a non-zero code.")}))},O=c.promisify(a);function j(e){return P.apply(this,arguments)}function P(){return(P=p(v.mark((function e(t){var r,n,o,i,a,u;return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:n={files:[],modules:null,hasGlobbed:!1,hasLocations:!1,hasEventLogging:!0,hasResultLogging:!0,isDryRun:!1,batchSize:25};case 1:if(!t.length){e.next=38;break}a=t.shift(),e.t0=a,e.next=void 0===e.t0||"--"===e.t0?6:"-m"===e.t0||"--module"===e.t0||"--main"===e.t0?7:"-d"===e.t0||"--dry"===e.t0?9:"-l"===e.t0||"--log-level"===e.t0?11:"-x"===e.t0||"--expression"===e.t0||"--xpath"===e.t0||"--xquery"===e.t0||"--xquf"===e.t0?13:"-b"===e.t0||"--batch"===e.t0||"--batch-size"===e.t0?15:"-g"===e.t0||"--glob"===e.t0?17:"-O"===e.t0||"--no-stderr"===e.t0?30:"-o"===e.t0||"--no-stdout"===e.t0?32:34;break;case 6:return e.abrupt("break",36);case 7:return o=t.shift(),e.abrupt("continue",1);case 9:return n.isDryRun=!0,e.abrupt("continue",1);case 11:return s.level=t.shift(),e.abrupt("continue",1);case 13:return i=t.shift(),e.abrupt("continue",1);case 15:return n.batchSize=parseInt(t.shift()||String(n.batchSize),10),e.abrupt("continue",1);case 17:if(u=t.shift()){e.next=20;break}throw new Error('Invalid globbing pattern "'+u+'"');case 20:return n.hasGlobbed=!0,e.t1=(r=n.files).splice,e.t2=r,e.t3=[0,0],e.next=26,O(u,{cwd:process.cwd(),absolute:!1});case 26:return e.t4=e.sent,e.t5=e.t3.concat.call(e.t3,e.t4),e.t1.apply.call(e.t1,e.t2,e.t5),e.abrupt("continue",1);case 30:return n.hasEventLogging=!1,e.abrupt("continue",1);case 32:return n.hasResultLogging=!1,e.abrupt("continue",1);case 34:n.hasLocations=!0,n.files.push(a);case 36:e.next=1;break;case 38:if(e.t6=S,e.t7=i,e.t7){e.next=44;break}return e.next=43,N();case 43:e.t7=e.sent;case 44:return e.t8=e.t7,e.t9=o,e.next=48,(0,e.t6)(e.t8,e.t9);case 48:if(n.modules=e.sent,n.modules.main&&n.modules.main.contents){e.next=51;break}throw new Error("Your XPath expression should not be empty.");case 51:return e.abrupt("return",n);case 52:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function S(e,t){return $.apply(this,arguments)}function $(){return($=p(v.mark((function e(n,o){return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",t.getModules((function(e,t){var r=e?u.dirname(e):process.cwd();return u.resolve(r,t)}),(function(e){return e?r.readFile(e,"utf8"):n}),o));case 1:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function N(){return _.apply(this,arguments)}function _(){return(_=p(v.mark((function e(){var t;return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!process.stdin.isTTY){e.next=2;break}return e.abrupt("return");case 2:return t="",process.stdin.on("readable",(function(){var e=process.stdin.read();null!==e&&(t+=e)})),e.abrupt("return",new Promise((function(e){return process.stdin.on("end",(function(){return e(t)}))})));case 5:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function T(e,t,r){return q.apply(this,arguments)}function q(){return(q=p(v.mark((function e(t,r,n){var o;return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,j(r);case 2:if((o=e.sent).hasResultLogging&&k(0,t,process.stdout),o.hasEventLogging&&L(o,t,process),t.emit("files",o.files),t.emit("modules",o.modules),t.emit("start"),o.hasGlobbed||o.hasLocations){e.next=23;break}return t.emit("expression",o.modules.main.contents),e.prev=10,e.t0=t,e.next=14,g(o.modules,null,{cwd:process.cwd()},{debug:!0});case 14:e.t1=e.sent.returnValue,e.t2={$value:e.t1},e.t0.emit.call(e.t0,"result",e.t2),e.next=22;break;case 19:e.prev=19,e.t3=e.catch(10),t.emit("result",{$error:e.t3});case 22:return e.abrupt("return");case 23:return e.next=25,m(n,o,(function(e,r){e.$error&&(process.exitCode=1),e.$fileNameBase=u.relative(process.cwd(),e.$fileName).replace(/\\/g,"/"),t.emit("file",e,r)}));case 25:case"end":return e.stop()}}),e,null,[[10,19]])})))).apply(this,arguments)}function F(){return(F=p(v.mark((function e(t,r){var n;return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=new i.EventEmitter,e.prev=1,e.next=4,T(n,t,r);case 4:e.next=12;break;case 6:return e.prev=6,e.t0=e.catch(1),s.disableProgress(),process.exitCode=1,n.emit("error",e.t0),e.abrupt("return");case 12:n.emit("end",process.exitCode);case 13:case"end":return e.stop()}}),e,null,[[1,6]])})))).apply(this,arguments)}exports.evaluateUpdatingExpressionOnNode=g,exports.startChildProcess=function(){var e;process.on("message",(e=function(){var e=p(v.mark((function e(t,r){var n;return v.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(process.send){e.next=2;break}throw new Error("Not running from a child process");case 2:return e.prev=2,e.next=5,x(t,r,{"document-uri":r},{debug:!0});case 5:n=e.sent,process.send({$fileName:r,$error:null,$value:n.returnValue,$isUpdate:n.isUpdating}),e.next=14;break;case 11:e.prev=11,e.t0=e.catch(2),process.send({$fileName:r,$error:{message:e.t0.message||null,stack:e.t0.stack||null},$value:null,$isUpdate:!1});case 14:case"end":return e.stop()}}),e,null,[[2,11]])})));return function(t,r){return e.apply(this,arguments)}}(),function(){var r=p(v.mark((function r(n){var o;return v.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(r.prev=0,process.send){r.next=3;break}throw new Error("Not running from a child process");case 3:if("run"!==n.type){r.next=10;break}return o=n.files,n.modules.libraries.forEach(t.loadModule),r.next=8,Promise.all(o.map((function(t,r){return e(n,t,r)})));case 8:return process.send(null),r.abrupt("return");case 10:"kill"===n.type&&process.exit(),r.next=19;break;case 13:r.prev=13,r.t0=r.catch(0),console.error("> Encountered an unexpected error in xquery-cli child process:"),console.error("> "+r.t0.stack),process.exitCode=1,process.exit();case 19:case"end":return r.stop()}}),r,null,[[0,13]])})));return function(e){return r.apply(this,arguments)}}()))},exports.startParentProcess=function(e,t){return F.apply(this,arguments)}; //# sourceMappingURL=xquery-cli.cjs.production.min.js.map