configorama 0.3.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -317,6 +317,10 @@ How is this different than the serverless variable system?
317
317
  ${merge('one', 'two')} => 'onetwo'
318
318
  ```
319
319
 
320
+ ## Alt libs
321
+
322
+ - https://github.com/01alchemist/sls-yaml
323
+
320
324
  ## Inspiration
321
325
 
322
326
  This is forked out of the [serverless framework](https://github.com/serverless/serverless/) variable system.
package/lib/main.js CHANGED
@@ -54,6 +54,7 @@ const selfRefSyntax = RegExp(/^self:/g)
54
54
  const funcRegex = /(\w+)\s*\(((?:[^()]+)*)?\s*\)\s*/
55
55
  const funcStartOfLineRegex = /^(\w+)\s*\(((?:[^()]+)*)?\s*\)\s*/
56
56
  const subFunctionRegex = /(\w+):(\w+)\s*\(((?:[^()]+)*)?\s*\)\s*/
57
+ const base64WrapperRegex = /\[_\[([A-Za-z0-9+/=\s]*)\]_\]/g
57
58
  const logLines = '─────────────────────────────────────────────────'
58
59
  const DEBUG = false
59
60
 
@@ -374,7 +375,7 @@ class Configorama {
374
375
  const transform = this.runFunction.bind(this)
375
376
  const varSyntax = this.variableSyntax
376
377
  // Traverse resolved object and run functions
377
-
378
+ // console.log('this.config', this.config)
378
379
  traverse(this.config).forEach(function (rawValue) {
379
380
  /* Pass through unknown variables */
380
381
  if (!configoramaOpts.allowUndefinedValues && typeof rawValue === 'undefined') {
@@ -407,15 +408,13 @@ class Configorama {
407
408
  this.update(funcVal)
408
409
  }
409
410
  }
411
+
412
+
410
413
  /* Allow for unknown variables to pass through */
411
414
  if (rawValue.match(/>passthrough/)) {
412
- const val = rawValue.replace(/^>passthrough/, '')
413
- const base64WrapperRegex = /\[_\[(.*)\]_\]/
414
- const matches = base64WrapperRegex.exec(val)
415
- if (matches) {
416
- const actualValue = Buffer.from(matches[1], 'base64').toString('ascii')
417
- this.update(actualValue)
418
- }
415
+ const newValues = decodeUnknown(rawValue)
416
+ // console.log('>>>> newValues', newValues)
417
+ this.update(newValues)
419
418
  }
420
419
  }
421
420
  })
@@ -778,14 +777,13 @@ class Configorama {
778
777
  * value for all instances of the given matched string.
779
778
  */
780
779
  populateVariable(valueObject, matchedString, valueToPopulate) {
780
+ let property = valueObject.value
781
781
  if (DEBUG) {
782
782
  console.log('------')
783
783
  console.log('populateVariable: valueToPopulate', valueToPopulate)
784
- console.log('matchedString', matchedString)
785
- }
786
- let property = valueObject.value
787
- if (DEBUG) {
788
- console.log('property', property)
784
+ console.log('populateVariable: matchedString', matchedString)
785
+ console.log('populateVariable: valueObject', valueObject)
786
+ console.log('populateVariable: property', property)
789
787
  console.log('------')
790
788
  }
791
789
 
@@ -803,10 +801,36 @@ class Configorama {
803
801
  // console.log('xxxx', rep)
804
802
  // console.log('valueToPopulate', valueToPopulate)
805
803
  // }
806
- property = replaceall(matchedString, valueToPopulate, property)
804
+
805
+
806
+ let currentMatchedString = matchedString
807
+ /* Address fall through values if found */
808
+ if (valueToPopulate.match(/>passthrough/)) {
809
+ const decoded = decodeUnknown(valueToPopulate)
810
+ if (decoded === property) {
811
+ currentMatchedString = valueObject.value
812
+ }
813
+ }
814
+
815
+ /*
816
+ console.log('>------')
817
+ console.log('isString og matchedString', matchedString)
818
+ console.log('isString replaceThis: matchedString', currentMatchedString)
819
+ console.log('isString withThis: valueToPopulate', valueToPopulate)
820
+ console.log('isString decode:', decodeUnknown(valueToPopulate))
821
+ console.log('isString inThis: property', property)
822
+ console.log('isString currentMatchedString', currentMatchedString)
823
+ console.log('>------')
824
+ /** */
825
+
826
+ // (replaceThis, withThis, inThis)
827
+ property = replaceall(currentMatchedString, valueToPopulate, property)
828
+ // console.log('property', property)
829
+
807
830
  // if (property.match(/^> function /g)) {
808
831
  // console.log('REPLACE after', property)
809
832
  // }
833
+
810
834
  // partial replacement, number
811
835
  } else if (_.isNumber(valueToPopulate)) {
812
836
  property = replaceall(matchedString, String(valueToPopulate), property)
@@ -861,7 +885,9 @@ ${valueObject.path}: ${valueObject.originalSource}
861
885
  }
862
886
 
863
887
  if (property && typeof property === 'string') {
888
+ // console.log('property', property)
864
889
  const prop = cleanVariable(property, this.variableSyntax)
890
+ // console.log('prop', prop)
865
891
  if (property.match(/^> function /g) && prop) {
866
892
  // console.log('PRPOpropertyproperty', property)
867
893
  // console.log('Prop', prop)
@@ -914,7 +940,9 @@ ${valueObject.path}: ${valueObject.originalSource}
914
940
  // property = newer
915
941
  // }
916
942
  }
917
-
943
+ // console.log('XXXX property', property)
944
+ // console.log('XXXX path', valueObject.path)
945
+ // console.log('XXXX originalSource', valueObject.originalSource)
918
946
  return {
919
947
  value: property,
920
948
  path: valueObject.path,
@@ -1220,7 +1248,17 @@ Like so: \${${variableString}, "fallbackValue"\}.`)
1220
1248
 
1221
1249
  /* Pass through unknown variables */
1222
1250
  if (this.opts.passThroughUnknown) {
1223
- const passThroughUnknown = `>passthrough[_[${Buffer.from(propertyString).toString('base64')}]_]`
1251
+ // console.log('passThroughUnknown propertyString', propertyString)
1252
+ const varMatches = propertyString.match(this.variableSyntax)
1253
+ let passThroughUnknown = propertyString
1254
+ /* If variables found, encode them for passthrough */
1255
+ if (varMatches && varMatches.length) {
1256
+ varMatches.forEach((m) => {
1257
+ passThroughUnknown = passThroughUnknown.replace(m, encodeUnknown(m))
1258
+ })
1259
+ }
1260
+ // console.log('passThroughUnknown propertyString:', propertyString)
1261
+ // console.log('passThroughUnknown:', passThroughUnknown)
1224
1262
  return Promise.resolve(passThroughUnknown)
1225
1263
  }
1226
1264
 
@@ -1587,6 +1625,39 @@ Remove or update the \${${variableString}} to fix
1587
1625
  return isRealVariable
1588
1626
  }
1589
1627
 
1628
+ function encodeUnknown(v) {
1629
+ return `>passthrough[_[${Buffer.from(v).toString('base64')}]_]`
1630
+ }
1631
+
1632
+ function decodeUnknown(rawValue) {
1633
+ const x = findUnknownValues(rawValue)
1634
+ let val = rawValue.replace(/>passthrough/g, '')
1635
+ if (x.length) {
1636
+ x.forEach(({ match, value }) => {
1637
+ // console.log('match', match)
1638
+ const decodedValue = Buffer.from(value, 'base64').toString('ascii')
1639
+ // console.log('decodedValue', decodedValue)
1640
+ val = val.replace(match, decodedValue)
1641
+ })
1642
+ }
1643
+ return val
1644
+ }
1645
+
1646
+ function findUnknownValues(text) {
1647
+ let matches
1648
+ let links = []
1649
+ while ((matches = base64WrapperRegex.exec(text)) !== null) {
1650
+ if (matches.index === base64WrapperRegex.lastIndex) {
1651
+ base64WrapperRegex.lastIndex++ // avoid infinite loops with zero-width matches
1652
+ }
1653
+ links.push({
1654
+ match: matches[0],
1655
+ value: matches[1]
1656
+ })
1657
+ }
1658
+ return links
1659
+ }
1660
+
1590
1661
  function isPromise(obj) { // eslint-disable-line
1591
1662
  return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
1592
1663
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "configorama",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "Variable support for configuration files",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -12,6 +12,7 @@
12
12
  "scripts": {
13
13
  "docs": "node ./scripts/docs.js",
14
14
  "test": "ava -v",
15
+ "test:api": "ava ./tests/api/api.test.js -v",
15
16
  "watch": "npm test -- --watch -v",
16
17
  "publish": "git push origin && git push origin --tags",
17
18
  "release:patch": "npm version patch && npm publish",