configorama 0.6.5 → 0.6.6

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/main.js +42 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "configorama",
3
- "version": "0.6.5",
3
+ "version": "0.6.6",
4
4
  "description": "Variable support for configuration files",
5
5
  "main": "src/index.js",
6
6
  "types": "index.d.ts",
package/src/main.js CHANGED
@@ -76,6 +76,7 @@ const deepIndexReplacePattern = new RegExp(/^deep:|(\.[^}]+)*$/g)
76
76
  const deepIndexPattern = /deep\:(\d*)/
77
77
  const deepPrefixReplacePattern = /(?:^deep:)\d+\.?/g
78
78
  const fileRefSyntax = RegExp(/^file\((~?[@\{\}\:\$a-zA-Z0-9._\-\/,'" ]+?)\)/g)
79
+ const textRefSyntax = RegExp(/^text\((~?[@\{\}\:\$a-zA-Z0-9._\-\/,'" ]+?)\)/g)
79
80
  // TODO update file regex ^file\((~?[a-zA-Z0-9._\-\/, ]+?)\)
80
81
  // To match file(asyncValue.js, lol) input params
81
82
  const envRefSyntax = RegExp(/^env:/g)
@@ -230,6 +231,16 @@ class Configorama {
230
231
  },
231
232
  },
232
233
 
234
+
235
+ {
236
+ type: 'text',
237
+ prefix: 'text',
238
+ match: textRefSyntax,
239
+ resolver: (varString, o, x, pathValue) => {
240
+ return this.getValueFromFile(varString, { asRawText: true })
241
+ },
242
+ },
243
+
233
244
  // Git refs
234
245
  createGitResolver(this.configPath),
235
246
  /* Internal Resolvers */
@@ -838,6 +849,13 @@ class Configorama {
838
849
  }
839
850
  }
840
851
 
852
+ /* fix for file(JS-ref.js, raw) to keep parens and inline code */
853
+ const OPEN_PAREN_PLACEHOLDER_PATTERN = /__PH_PAREN_OPEN__/g
854
+ if (rawValue.match(OPEN_PAREN_PLACEHOLDER_PATTERN)) {
855
+ rawValue = rawValue.replace(OPEN_PAREN_PLACEHOLDER_PATTERN, '(')
856
+ this.update(rawValue)
857
+ }
858
+
841
859
  /* Allow for unknown variables to pass through */
842
860
  if (rawValue.match(/>passthrough/)) {
843
861
  const newValues = decodeUnknown(rawValue)
@@ -1984,15 +2002,19 @@ Unable to resolve configuration variable
1984
2002
  return res
1985
2003
  })
1986
2004
  }
1987
- async getValueFromFile(variableString) {
2005
+ async getValueFromFile(variableString, options) {
2006
+ const opts = options || {}
2007
+ const syntax = opts.asRawText ? textRefSyntax : fileRefSyntax
1988
2008
  // console.log('From file', `"${variableString}"`)
1989
- let matchedFileString = variableString.match(fileRefSyntax)[0]
2009
+ let matchedFileString = variableString.match(syntax)[0]
1990
2010
  // console.log('matchedFileString', matchedFileString)
1991
2011
 
1992
- // Get function input params if any supplied
1993
- var funcParamsRegex = /(\w+)\s*\(((?:[^()]+)*)?\s*\)\s*/g
2012
+ // Get function input params if any supplied https://regex101.com/r/qlNFVm/1
2013
+ // var funcParamsRegex = /(\w+)\s*\(((?:[^()]+)*)?\s*\)\s*/g
2014
+ var funcParamsRegex = /(\w+)\s*\(((?:[^()]+)*)?\s*\)/g
2015
+ // tighter (?<![.\w-])\b(\w+)\s*\(((?:[^()]+)*)?\s*\)\s*
1994
2016
  var hasParams = funcParamsRegex.exec(matchedFileString)
1995
- // console.log('args', hasParams)
2017
+
1996
2018
  let argsToPass = []
1997
2019
  if (hasParams) {
1998
2020
  const splitter = splitCsv(hasParams[2])
@@ -2000,6 +2022,7 @@ Unable to resolve configuration variable
2000
2022
  const cleanArg = trim(arg).replace(/^'|"/, '').replace(/'|"$/, '')
2001
2023
  return cleanArg
2002
2024
  })
2025
+ // console.log('argsFound', argsFound)
2003
2026
 
2004
2027
  // If function has more arguments than file path
2005
2028
  if (argsFound.length && argsFound.length > 1) {
@@ -2012,7 +2035,7 @@ Unable to resolve configuration variable
2012
2035
  // console.log('argsToPass', argsToPass)
2013
2036
 
2014
2037
  const relativePath = trimSurroundingQuotes(
2015
- matchedFileString.replace(fileRefSyntax, (match, varName) => varName.trim()).replace('~', os.homedir()),
2038
+ matchedFileString.replace(syntax, (match, varName) => varName.trim()).replace('~', os.homedir()),
2016
2039
  )
2017
2040
 
2018
2041
  // Resolve alias if the path contains alias syntax
@@ -2059,8 +2082,16 @@ ${logLines}
2059
2082
 
2060
2083
  let valueToPopulate
2061
2084
 
2085
+ const variableFileContents = fs.readFileSync(fullFilePath, 'utf-8')
2086
+
2087
+ /* handle case for referencing raw JS files to inline them */
2088
+ if (argsToPass.length && (argsToPass && argsToPass[0] && argsToPass[0].toLowerCase() === 'raw') || opts.asRawText) {
2089
+ valueToPopulate = variableFileContents.replace(/\(/g, '__PH_PAREN_OPEN__')
2090
+ return Promise.resolve(valueToPopulate)
2091
+ }
2092
+
2062
2093
  // Process JS files
2063
- if (fileExtension === 'js') {
2094
+ if (fileExtension === 'js' || fileExtension === 'cjs') {
2064
2095
  const jsFile = require(fullFilePath)
2065
2096
  let returnValueFunction = jsFile
2066
2097
  // TODO change how exported functions are referenced
@@ -2084,8 +2115,7 @@ Check if your javascript is exporting a function that returns a value.`
2084
2115
  config: this.config,
2085
2116
  opts: this.opts,
2086
2117
  }
2087
-
2088
-
2118
+
2089
2119
  valueToPopulate = returnValueFunction.call(jsFile, valueForFunction, ...argsToPass)
2090
2120
 
2091
2121
  return Promise.resolve(valueToPopulate).then((valueToPopulateResolved) => {
@@ -2107,7 +2137,6 @@ Check if your javascript is returning the correct data.`
2107
2137
  })
2108
2138
  }
2109
2139
 
2110
-
2111
2140
  if (fileExtension === 'ts') {
2112
2141
  const { executeTypeScriptFile } = require('./parsers/typescript')
2113
2142
  let returnValueFunction
@@ -2217,7 +2246,7 @@ Check if your ESM is returning the correct data.`
2217
2246
  // Process everything except JS, TS, and ESM
2218
2247
  if (fileExtension !== 'js' && fileExtension !== 'ts' && fileExtension !== 'mjs' && fileExtension !== 'esm') {
2219
2248
  /* Read initial file */
2220
- valueToPopulate = fs.readFileSync(fullFilePath, 'utf-8')
2249
+ valueToPopulate = variableFileContents
2221
2250
 
2222
2251
  // File reference has :subKey lookup. Must dig deeper
2223
2252
  if (matchedFileString !== variableString) {
@@ -2400,6 +2429,8 @@ Please use ":" to reference sub properties`
2400
2429
  varType = 'file'
2401
2430
  } else if (variableString.match(deepRefSyntax)) {
2402
2431
  varType = 'deep'
2432
+ } else if (variableString.match(textRefSyntax)) {
2433
+ varType = 'text'
2403
2434
  }
2404
2435
  if (!isValidValue(valueToPopulate)) {
2405
2436
  // console.log("MISSING", variableString)