ut-tools
Version:
Build and Release management automation package.
2 lines (1 loc) • 11.1 kB
JavaScript
import{isObject as e,cloneDeep as r,get as t,set as n,has as o,trimStart as i}from"lodash";import{createScanner as s,findNodeAtOffset as a,getNodePath as c,visit as f,printParseErrorCode as l}from"jsonc-parser";import u,{getOrder as p,ORDER_KEY_ID as h}from"@stoplight/ordered-object-literal";import{DiagnosticSeverity as y}from"@stoplight/types";import g from"safe-stable-stringify";const d=r=>e(r)&&"$ref"in r&&"string"==typeof r.$ref,b=e=>e.length>0&&("#"===e||/^#\S*$/.test(e)),m=(e,r,t)=>{const n=e.toString();let o="",i=n,s=0,a=i.indexOf(r);for(;a>-1;)o+=n.substring(s,s+a)+t,i=i.substring(a+r.length,i.length),s+=a+r.length,a=i.indexOf(r);return i.length>0&&(o+=n.substring(n.length-i.length,n.length)),o},w=e=>"number"==typeof e?e:m(m(e,"~","~0"),"/","~1"),O=e=>v(e),v=e=>{if(e&&"object"!=typeof e)throw new TypeError("Invalid type: path must be an array of segments.");return 0===e.length?"#":`#/${e.map(w).join("/")}`};function j(e){try{return decodeURIComponent(e)}catch(r){return e}}const A=/%[0-9a-f]+/gi,E=e=>{let r;try{r=decodeURIComponent(e)}catch(t){r=e.replace(A,j)}return m(m(r,"~1","/"),"~0","~")},$=e=>x(e),x=e=>{if("string"!=typeof e)throw new TypeError("Invalid type: JSON Pointers are represented as strings.");if(0===e.length||"#"!==e[0])throw new URIError("Invalid JSON Pointer syntax; URI fragment identifiers must begin with a hash.");if(1===e.length)return[];if("/"!==e[1])throw new URIError("Invalid JSON Pointer syntax.");return(e=>{const r=e.length,t=[];let n=-1;for(;++n<r;)t.push(E(e[n]));return t})(e.substring(2).split("/"))},S=e=>{if("string"!=typeof e||0===e.length||b(e))return null;const r=e.indexOf("#");return-1===r?e:e.slice(0,r)};function _(e){if("object"!=typeof e||null===e)return!1;const r=Object.getPrototypeOf(e);return null===r||r===Object.prototype||"function"==typeof e.constructor&&Function.toString.call(Object)===Function.toString.call(e.constructor)}function k(e,r){return function e(r,t,n){if(null!==S(t))throw new ReferenceError("Cannot resolve external references");const o=$(t);let i=r;for(const s of o){if(!_(i)&&!Array.isArray(i)||!(s in i))throw new ReferenceError(`Could not resolve '${t}'`);if(_(i=i[s])&&"$ref"in i){if(n.includes(i))return n[n.length-1];if(n.push(i),"string"!=typeof i.$ref)throw new TypeError("$ref should be a string");i=e(r,i.$ref,n)}}if(0===n.length)return i;const s=n[n.length-1];return _(s)&&("summary"in s||"description"in s)?Object.assign(Object.assign(Object.assign({},i),"description"in s?{description:s.description}:null),"summary"in s?{summary:s.summary}:null):i}(e,r,[])}const N=(e,r,t)=>{const n={value:e,path:t};r.onEnter&&r.onEnter(n);for(const n of Object.keys(e)){const o=e[n];r.onProperty&&r.onProperty({parent:e,parentPath:t,property:n,propertyValue:o}),"object"==typeof o&&null!==o&&N(o,r,t.concat(n))}r.onLeave&&r.onLeave(n)},P=(e,r)=>{"object"==typeof e&&null!==e&&N(e,"function"==typeof r?{onProperty:r}:r,[])},T="#/__bundled__",I="#/__errors__",R=({document:e,path:t,bundleRoot:n="#/__bundled__",errorsRoot:o="#/__errors__",cloneDocument:i=!0},s)=>{if(t===n||t===o)throw new Error("Roots do not make any sense");const a=i?r(e):e;return K(a,$(n),$(o))(t,{[t]:!0},s)},K=(e,r,i)=>{const s=new Set,a=(c,f,l,u={},p={},h={})=>{const y=$(c),g=t(e,y);P(l||g,({parent:i})=>{if(d(i)&&b(i.$ref)){const l=i.$ref;if(h[l])return;if(u[l])return void(i.$ref=u[l]);let y,g,d,b,m;try{let n;if(y=$(l),Array.isArray(t(e,y.slice(0,-1)))){n=`${y[y.length-2]}_${y[y.length-1]}`}else n=y[y.length-1];d=n;let o=1;for(;s.has(d);)if(d=`${n}_${++o}`,o>20)throw new Error(`Keys ${n}_2 through ${n}_20 already taken.`);s.add(d),g=[...r,d],b=O(g)}catch(e){h[l]=e.message}if(!y||!g||!b)return;if("object"==typeof e&&null!==e)try{m=k(Object(e),l)}catch(r){m=t(e,y)}if(void 0!==m&&(u[l]=b,i.$ref=b,!o(p,g))){const r=t(e,y);Array.isArray(r)?n(p,g,new Array(r.length).fill(null)):"object"==typeof r&&n(p,g,{}),n(p,g,m),f[l]||(f[l]=!0,a(c,f,m,u,p,h),f[l]=!1)}}});const m=t(p,r);return m&&Object.keys(m).length&&n(g,r,m),(Object.keys(h).length||o(e,i))&&n(g,i,o(e,i)?t(e,i):h),g};return a},M=e=>m(m(e,"~1","/"),"~0","~"),U=(e,r)=>{const t=new WeakMap;return function e(n,o){let i;if(r&&(n=r(n)),_(n)||Array.isArray(n)){const r=t.get(n);return r?{$ref:r}:(t.set(n,O(o)),Array.isArray(n)?i=n.map((r,t)=>e(r,[...o,String(t)])):(i={},Object.keys(n).forEach(r=>{i[r]=e(n[r],[...o,r])})),t.delete(n),i)}return n}(e,[])},C=e=>m(m(e,"~","~0"),"//","/~1"),D=e=>{if("string"!=typeof e||0===e.length)return null;const r=e.indexOf("#");return-1===r?null:e.slice(r)},J=e=>{const r=s(e,!0);if(r.scan(),1!==r.getToken())return;if(r.scan(),2===r.getToken())return;if(10!==r.getToken())throw new SyntaxError("Unexpected character");const t=r.getTokenValue();if(r.scan(),6!==r.getToken())throw new SyntaxError("Colon expected");switch(r.scan(),r.getToken()){case 10:return[t,r.getTokenValue()];case 11:return[t,Number(r.getTokenValue())];case 8:return[t,!0];case 9:return[t,!1];case 7:return[t,null];case 16:throw new SyntaxError("Unexpected character");case 17:throw new SyntaxError("Unexpected end of file");default:return}},V=({lineMap:e,ast:r},t)=>{const n=e[t.line],o=e[t.line+1];if(void 0===n)return;const i=a(r,void 0===o?n+t.character:Math.min(o,n+t.character),!0);if(void 0===i)return;const s=c(i);return 0!==s.length?s:void 0};function F(e){return M(e.split("/").pop()||"")}const L=({lineMap:e,ast:r},t,n=!1)=>{const o=function(e,r,t){e:for(const n of r){const r=Number.isInteger(Number(n))?Number(n):n;if("string"==typeof r||"number"==typeof r&&"array"!==e.type){if("object"!==e.type||!Array.isArray(e.children))return t?e:void 0;for(const t of e.children)if(Array.isArray(t.children)&&t.children[0].value===String(r)&&2===t.children.length){e=t.children[1];continue e}return t?e:void 0}if("array"!==e.type||r<0||!Array.isArray(e.children)||r>=e.children.length)return t?e:void 0;e=e.children[r]}return e}(r,t,n);if(void 0!==o&&void 0!==o.range)return{range:o.range}};const W=(e,r={disallowComments:!0})=>{const t=[],{ast:n,data:o,lineMap:i}=B(e,t,r);return{data:o,diagnostics:t,ast:n,lineMap:i}};function B(e,r=[],t){const n=z(e);let o={type:"array",offset:-1,length:-1,children:[],parent:void 0},i=null,s=[];const a=new WeakMap,c=[];function h(e){"property"===o.type&&(o.length=e-o.offset,o=o.parent)}function g(e,r,t){return{start:{line:e,character:r},end:{line:e,character:r+t}}}function d(e){return o.children.push(e),e}function b(e){Array.isArray(s)?s.push(e):null!==i&&(s[i]=e)}function m(e){b(e),c.push(s),s=e,i=null}function w(){s=c.pop()}f(e,{onObjectBegin:(e,r,n,i)=>{o=d({type:"object",offset:e,length:-1,parent:o,children:[],range:g(n,i,r)}),!1===t.ignoreDuplicateKeys&&a.set(o,[]),m(function(e){return e?u({}):{}}(!0===t.preserveKeyOrder))},onObjectProperty:(e,n,c,f,l)=>{if((o=d({type:"property",offset:n,length:-1,parent:o,children:[]})).children.push({type:"string",value:e,offset:n,length:c,parent:o}),!1===t.ignoreDuplicateKeys){const t=a.get(o.parent);t&&(0!==t.length&&t.includes(e)?r.push({range:g(f,l,c),message:"DuplicateKey",severity:y.Error,path:G(o),code:20}):t.push(e))}!0===t.preserveKeyOrder&&function(e,r){if(!(r in e))return;const t=p(e),n=t.indexOf(r);-1!==n&&(t.splice(n,1),t.push(r))}(s,e),i=e},onObjectEnd:(e,r,n,i)=>{!1===t.ignoreDuplicateKeys&&a.delete(o),o.length=e+r-o.offset,o.range&&(o.range.end.line=n,o.range.end.character=i+r),o=o.parent,h(e+r),w()},onArrayBegin:(e,r,t,n)=>{o=d({type:"array",offset:e,length:-1,parent:o,children:[],range:g(t,n,r)}),m([])},onArrayEnd:(e,r,t,n)=>{o.length=e+r-o.offset,o.range&&(o.range.end.line=t,o.range.end.character=n+r),o=o.parent,h(e+r),w()},onLiteralValue:(e,r,t,n,i)=>{d({type:q(e),offset:r,length:t,parent:o,value:e,range:g(n,i,t)}),h(r+t),b(e)},onSeparator:(e,r,t)=>{"property"===o.type&&(":"===e?o.colonOffset=r:","===e&&h(r))},onError:(e,t,n,o,i)=>{r.push({range:g(o,i,n),message:l(e),severity:y.Error,code:e})}},t);const O=o.children[0];return O&&delete O.parent,{ast:O,data:s[0],lineMap:n}}function q(e){switch(typeof e){case"boolean":return"boolean";case"number":return"number";case"string":return"string";default:return"null"}}const z=e=>{const r=[0];let t=0;for(;t<e.length;t++)"\n"===e[t]&&r.push(t+1);return r.push(t+1),r};function G(e,r=[]){return"property"===e.type&&r.unshift(e.children[0].value),void 0!==e.parent?("array"===e.parent.type&&void 0!==e.parent.parent&&r.unshift(e.parent.children.indexOf(e)),G(e.parent,r)):r}const H=(e,r,t)=>{if(!e||!Object.hasOwnProperty.call(e,r)||r===t)return e;const n={};for(const[o,i]of Object.entries(e))o===r?n[t]=i:o in n||(n[o]=i);return n};function Q(e){return _(e)||Array.isArray(e)}function X(e,r,t){if(t.length<=1||r.length<=1)throw Error("Source/target path must not be empty and point at root");if(0===r.indexOf(t))throw Error("Target path cannot be contained within source");const n=$(r);let o=e;for(const e of n){if(!Q(o))return;o=o[e]}if(!Q(o))return;const i=$(t);let s=e;for(const[e,r]of i.entries()){if(!Q(s)||r in s)return;const t=e===i.length-1?o:{};s[r]=t,s=t}delete e[n[0]],function e(r,t,n){for(const o of Object.keys(r)){const i=r[o];if("$ref"!==o)Q(i)&&e(i,t,n);else{if("string"!=typeof i||!b(i))continue;0===i.indexOf(t)&&(r[o]=i.replace(t,n))}}}(e,r,t)}const Y=(e,r)=>{if("string"!=typeof e)return e;try{const t=Z(e);return"string"==typeof t?t:JSON.parse(e,r)}catch(e){return}},Z=e=>{const r=Number(e);return Number.isFinite(r)?String(r)===e?r:e:NaN},ee=(e,r,t)=>{if("string"==typeof e)return e;try{return JSON.stringify(e,r,t)}catch(n){return g(e,r,t)}},re=(e,r)=>{if(e instanceof Array){if(r instanceof Array){if(r.length>e.length)return!1;for(const t in r){if(!r.hasOwnProperty(t))continue;const n=parseInt(e[t]),o=parseInt(r[t]);if(isNaN(n)&&isNaN(o)){if(e[t]!==r[t])return!1}else if(n!==o)return!1}}}else{if("string"!=typeof e)return!1;if("string"==typeof r)return e.startsWith(r)}return!0},te=(e,r,t)=>{const n=ee(e,r,t);if(void 0===n)throw new Error("The value could not be stringified");return n};function ne(e){return e.replace(/^(\/|#\/)/,"").split("/").map(M).map(oe).join(".")}function oe(e){return e.includes(".")?`["${e.replace(/"/g,'\\"')}"]`:e}const ie=Symbol.for(h),se={ownKeys:e=>ie in e?e[ie]:Reflect.ownKeys(e)},ae=e=>new Proxy(e,se);function ce(e,r){if("string"==typeof e&&"string"==typeof r)return i(e,r);if(!(e&&Array.isArray(e)&&e.length&&r&&Array.isArray(r)&&r.length))return e;let t=0;for(const n in e)if(e.hasOwnProperty(n)){if(e[n]!==r[n])break;t++}return e.slice(t)}export{T as BUNDLE_ROOT,I as ERRORS_ROOT,ie as KEYS,R as bundleTarget,E as decodePointer,M as decodePointerFragment,U as decycle,C as encodePointer,w as encodePointerFragment,D as extractPointerFromRef,S as extractSourceFromRef,J as getFirstPrimitiveProperty,V as getJsonPathForPosition,F as getLastPathSegment,L as getLocationForJsonPath,d as hasRef,b as isLocalRef,_ as isPlainObject,B as parseTree,W as parseWithPointers,O as pathToPointer,$ as pointerToPath,H as renameObjectKey,X as reparentBundleTarget,k as resolveInlineRef,Y as safeParse,ee as safeStringify,re as startsWith,te as stringify,ne as toPropertyPath,ae as trapAccess,P as traverse,ce as trimStart};