UNPKG

projen

Version:

CDK for software projects

1 lines • 8.89 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"PropertiesEditor",{enumerable:!0,get:function(){return PropertiesEditor}});var _index=require("../escape/index.js"),_properties=require("../parser/properties.js");function _instanceof(e,r){return null!=r&&"undefined"!=typeof Symbol&&r[Symbol.hasInstance]?!!r[Symbol.hasInstance](e):e instanceof r}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(!_instanceof(e,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}}var DEFAULT_SEPARATOR="=",DEFAULT_COMMENT_DELIMITER="#",REGEX_NEWLINE=/\r\n|\r|\n/,buildPropertyNode=function(e,r,t,n){var o,i=!0===t.escapeUnicode,a=(0,_index.escapeKey)(e,i),s=(0,_index.escapeValue)(r,i),l=" "===t.separator?void 0:null!==(o=t.separator)&&void 0!==o?o:DEFAULT_SEPARATOR,c=l?" ":"",u=l?"".concat(" ").concat(l).concat(c):" ",p="".concat(a).concat(u).concat(s).split(REGEX_NEWLINE);return{type:"property",rawLines:p,leadingWhitespace:"",key:e,escapedKey:a,separatorLeading:" ",separatorChar:l,separatorTrailing:c,value:r,escapedValue:s,startingLineNumber:n,endingLineNumber:n+p.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++)}},PropertiesEditor=function(){function e(){return _class_call_check(this,e),_call_super(this,e,arguments)}return _inherits(e,_properties.Properties),_create_class(e,[{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,i=[];if(void 0!==(null==t?void 0:t.comment)){var a,s=null!==(o=t.commentDelimiter)&&void 0!==o?o:DEFAULT_COMMENT_DELIMITER;(a=i).push.apply(a,_to_consumable_array(buildCommentNodes(t.comment,s,0)))}if(i.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,u="before"===t.position?l.index:l.index+1;return(c=this.nodes).splice.apply(c,[u,0].concat(_to_consumable_array(i))),void recalculateLineNumbers(this.nodes)}}(n=this.nodes).push.apply(n,_to_consumable_array(i)),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,i=buildCommentNodes(e,o,0);if(null==r?void 0:r.referenceKey){var a=this.findLastProperty(r.referenceKey);if(void 0!==a){var s,l="before"===r.position?a.index:a.index+1;return(s=this.nodes).splice.apply(s,[l,0].concat(_to_consumable_array(i))),void recalculateLineNumbers(this.nodes)}}(t=this.nodes).push.apply(t,_to_consumable_array(i)),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,i=this.findLastProperty(e);if(void 0===i)return!1;var a=i.index,s=i.node,l=null!==(t=r.newKey)&&void 0!==t?t:s.key,c=null!==(n=r.newValue)&&void 0!==n?n:s.value,u=!0===r.escapeUnicode,p=u?(0,_index.escapeKey)(l,!0):void 0!==r.newKey?(0,_index.escapeKey)(l):s.escapedKey,d=u?(0,_index.escapeValue)(c,!0):void 0!==r.newValue?(0,_index.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(p).concat(m).concat(d).split(REGEX_NEWLINE),h={type:"property",rawLines:v,leadingWhitespace:s.leadingWhitespace,key:l,escapedKey:p,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=a,g=a-1;g>=0&&"property"!==this.nodes[g].type;g--)L=g;var E=null!==(o=r.commentDelimiter)&&void 0!==o?o:DEFAULT_COMMENT_DELIMITER,N=buildCommentNodes(r.newComment,E,0);(b=this.nodes).splice.apply(b,[L,a-L+1].concat(_to_consumable_array(N),[h]))}else this.nodes[a]=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 i=n,a=n-1;a>=0&&"property"!==this.nodes[a].type;a--)i=a;this.nodes.splice(i,n-i+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()}}]),e}();