UNPKG

projen

Version:

CDK for software projects

1 lines 8.54 kB
function _array_like_to_array(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function _array_without_holes(e){if(Array.isArray(e))return _array_like_to_array(e)}function _assert_this_initialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _call_super(e,r,t){return r=_get_prototype_of(r),_possible_constructor_return(e,_is_native_reflect_construct()?Reflect.construct(r,t||[],_get_prototype_of(e).constructor):r.apply(e,t))}function _class_call_check(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _create_class(e,r,t){return r&&_defineProperties(e.prototype,r),t&&_defineProperties(e,t),e}function _get_prototype_of(e){return _get_prototype_of=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},_get_prototype_of(e)}function _inherits(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(r&&r.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),r&&_set_prototype_of(e,r)}function _is_native_reflect_construct(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(_is_native_reflect_construct=function(){return!!e})()}function _iterable_to_array(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _possible_constructor_return(e,r){return!r||"object"!==_type_of(r)&&"function"!=typeof r?_assert_this_initialized(e):r}function _set_prototype_of(e,r){return _set_prototype_of=Object.setPrototypeOf||function(e,r){return e.__proto__=r,e},_set_prototype_of(e,r)}function _to_consumable_array(e){return _array_without_holes(e)||_iterable_to_array(e)||_unsupported_iterable_to_array(e)||_non_iterable_spread()}function _type_of(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e}function _unsupported_iterable_to_array(e,r){if(e){if("string"==typeof e)return _array_like_to_array(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(t):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_array_like_to_array(e,r):void 0}}import{escapeKey,escapeValue}from"../escape/index.js";import{Properties}from"../parser/properties.js";var DEFAULT_SEPARATOR="=",DEFAULT_COMMENT_DELIMITER="#",REGEX_NEWLINE=/\r\n|\r|\n/,buildPropertyNode=function(e,r,t,n){var o,a=!0===t.escapeUnicode,i=escapeKey(e,a),s=escapeValue(r,a),l=" "===t.separator?void 0:null!==(o=t.separator)&&void 0!==o?o:DEFAULT_SEPARATOR,c=l?" ":"",p=l?"".concat(" ").concat(l).concat(c):" ",u="".concat(i).concat(p).concat(s).split(REGEX_NEWLINE);return{type:"property",rawLines:u,leadingWhitespace:"",key:e,escapedKey:i,separatorLeading:" ",separatorChar:l,separatorTrailing:c,value:r,escapedValue:s,startingLineNumber:n,endingLineNumber:n+u.length-1}},buildCommentNodes=function(e,r,t){return e.split(REGEX_NEWLINE).map(function(e,n){return""===e?{type:"blank",rawLine:"",lineNumber:t+n}:{type:"comment",rawLine:"".concat(r," ").concat(e),leadingWhitespace:"",delimiter:r,body:" ".concat(e),lineNumber:t+n}})},recalculateLineNumbers=function(e){for(var r=1,t=0;t<e.length;t++){var n=e[t];if("property"===n.type){var o=n.rawLines.length;e[t]={type:"property",rawLines:n.rawLines,leadingWhitespace:n.leadingWhitespace,key:n.key,escapedKey:n.escapedKey,separatorLeading:n.separatorLeading,separatorChar:n.separatorChar,separatorTrailing:n.separatorTrailing,value:n.value,escapedValue:n.escapedValue,startingLineNumber:r,endingLineNumber:r+o-1},r+=o}else"comment"===n.type?(e[t]={type:"comment",rawLine:n.rawLine,leadingWhitespace:n.leadingWhitespace,delimiter:n.delimiter,body:n.body,lineNumber:r},r++):(e[t]={type:"blank",rawLine:n.rawLine,lineNumber:r},r++)}};export var PropertiesEditor=function(e){"use strict";function r(){return _class_call_check(this,r),_call_super(this,r,arguments)}return _inherits(r,e),_create_class(r,[{key:"findFirstProperty",value:function(e){for(var r=0;r<this.nodes.length;r++){var t=this.nodes[r];if("property"===t.type&&t.key===e)return{index:r,node:t}}}},{key:"findLastProperty",value:function(e){for(var r=this.nodes.length-1;r>=0;r--){var t=this.nodes[r];if("property"===t.type&&t.key===e)return{index:r,node:t}}}},{key:"insert",value:function(e,r,t){var n,o,a=[];if(void 0!==(null==t?void 0:t.comment)){var i,s=null!==(o=t.commentDelimiter)&&void 0!==o?o:DEFAULT_COMMENT_DELIMITER;(i=a).push.apply(i,_to_consumable_array(buildCommentNodes(t.comment,s,0)))}if(a.push(buildPropertyNode(e,r,{escapeUnicode:null==t?void 0:t.escapeUnicode,separator:null==t?void 0:t.separator},0)),null==t?void 0:t.referenceKey){var l=this.findLastProperty(t.referenceKey);if(void 0!==l){var c,p="before"===t.position?l.index:l.index+1;return(c=this.nodes).splice.apply(c,[p,0].concat(_to_consumable_array(a))),void recalculateLineNumbers(this.nodes)}}(n=this.nodes).push.apply(n,_to_consumable_array(a)),recalculateLineNumbers(this.nodes)}},{key:"insertComment",value:function(e,r){var t,n,o=null!==(n=null==r?void 0:r.commentDelimiter)&&void 0!==n?n:DEFAULT_COMMENT_DELIMITER,a=buildCommentNodes(e,o,0);if(null==r?void 0:r.referenceKey){var i=this.findLastProperty(r.referenceKey);if(void 0!==i){var s,l="before"===r.position?i.index:i.index+1;return(s=this.nodes).splice.apply(s,[l,0].concat(_to_consumable_array(a))),void recalculateLineNumbers(this.nodes)}}(t=this.nodes).push.apply(t,_to_consumable_array(a)),recalculateLineNumbers(this.nodes)}},{key:"insertBlankLine",value:function(e){var r={type:"blank",rawLine:"",lineNumber:0};if(null==e?void 0:e.referenceKey){var t=this.findLastProperty(e.referenceKey);if(void 0!==t){var n="before"===e.position?t.index:t.index+1;return this.nodes.splice(n,0,r),void recalculateLineNumbers(this.nodes)}}this.nodes.push(r),recalculateLineNumbers(this.nodes)}},{key:"update",value:function(e,r){var t,n,o,a=this.findLastProperty(e);if(void 0===a)return!1;var i=a.index,s=a.node,l=null!==(t=r.newKey)&&void 0!==t?t:s.key,c=null!==(n=r.newValue)&&void 0!==n?n:s.value,p=!0===r.escapeUnicode,u=p?escapeKey(l,!0):void 0!==r.newKey?escapeKey(l):s.escapedKey,d=p?escapeValue(c,!0):void 0!==r.newValue?escapeValue(c):s.escapedValue,_=r.separator?" "===r.separator?void 0:r.separator:s.separatorChar,y=r.separator?" ":s.separatorLeading,f=r.separator?_?" ":"":s.separatorTrailing,m=_?"".concat(y).concat(_).concat(f):y,v="".concat(u).concat(m).concat(d).split(REGEX_NEWLINE),h={type:"property",rawLines:v,leadingWhitespace:s.leadingWhitespace,key:l,escapedKey:u,separatorLeading:y,separatorChar:_,separatorTrailing:f,value:c,escapedValue:d,startingLineNumber:s.startingLineNumber,endingLineNumber:s.startingLineNumber+v.length-1};if(void 0!==r.newComment){for(var b,L=i,g=i-1;g>=0&&"property"!==this.nodes[g].type;g--)L=g;var N=null!==(o=r.commentDelimiter)&&void 0!==o?o:DEFAULT_COMMENT_DELIMITER,E=buildCommentNodes(r.newComment,N,0);(b=this.nodes).splice.apply(b,[L,i-L+1].concat(_to_consumable_array(E),[h]))}else this.nodes[i]=h;return recalculateLineNumbers(this.nodes),!0}},{key:"upsert",value:function(e,r,t){void 0!==this.findLastProperty(e)?this.update(e,{newValue:r,escapeUnicode:null==t?void 0:t.escapeUnicode,separator:null==t?void 0:t.separator,newComment:null==t?void 0:t.comment,commentDelimiter:null==t?void 0:t.commentDelimiter}):this.insert(e,r,t)}},{key:"delete",value:function(e,r){var t="first"===(null==r?void 0:r.occurrence)?this.findFirstProperty(e):this.findLastProperty(e);if(void 0!==t){var n=t.index,o=t.node;if(!1!==(null==r?void 0:r.deleteLeadingNodes)){for(var a=n,i=n-1;i>=0&&"property"!==this.nodes[i].type;i--)a=i;this.nodes.splice(a,n-a+1)}else this.nodes.splice(n,1);return recalculateLineNumbers(this.nodes),o}}},{key:"deleteAll",value:function(e){for(var r=[],t=this.nodes.length-1;t>=0;t--){var n=this.nodes[t];"property"===n.type&&n.key===e&&(this.nodes.splice(t,1),r.push(n))}return r.length>0&&recalculateLineNumbers(this.nodes),r.reverse()}}]),r}(Properties);