This repository has been archived by the owner on Dec 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from crystian/refactor-possibilities
Refactor possibilities
- Loading branch information
Showing
13 changed files
with
432 additions
and
537 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const { isObject, isObjectEmpty, isString } = require('./utils'); | ||
const { predefined } = require('./defaults'); | ||
const { messages } = require('./i18n'); | ||
|
||
|
||
/** | ||
* Interpolate all the inputs! | ||
* | ||
* Inputs in order: environment, predefined, template, shortcut | ||
* And it allow to use nested object! (not for environment) | ||
* | ||
* Note: it can be changed to get better performance. | ||
* | ||
* @param config | ||
* @returns {any} | ||
*/ | ||
const interpolateVariables = (config) => { | ||
let shortcuts = config.shortcuts, | ||
templates = config.templates || {}, | ||
environments = config.environmentsRendered || [], | ||
newShortcuts, newTemplates, newEnvironments; | ||
|
||
if (!shortcuts) { | ||
throw new Error(messages.shortcuts.notFound); | ||
} | ||
|
||
// copy structure to mutate in the easy way | ||
newShortcuts = JSON.parse(JSON.stringify(shortcuts)); | ||
newTemplates = JSON.parse(JSON.stringify(templates)); | ||
|
||
newEnvironments = _buildInterpolateEnvironmentVars(environments); | ||
|
||
// This is recursive and it is needed to pass on each stage | ||
// to template | ||
_buildInterpolateVariablesRecursive(newTemplates, newEnvironments); | ||
_buildInterpolateVariablesRecursive(newTemplates, { predefined }); | ||
_buildInterpolateVariablesRecursive(newTemplates, newTemplates); // yes to itself! | ||
// to shortcuts | ||
_buildInterpolateVariablesRecursive(newShortcuts, newEnvironments); | ||
_buildInterpolateVariablesRecursive(newShortcuts, { predefined }); | ||
_buildInterpolateVariablesRecursive(newShortcuts, newTemplates); | ||
_buildInterpolateVariablesRecursive(newShortcuts, newShortcuts, true); // yes to itself! | ||
|
||
return newShortcuts; | ||
}; | ||
|
||
/** | ||
* It gets data from the external environment, and prepare it to use. | ||
* | ||
* @param environments | ||
* @private | ||
*/ | ||
const _buildInterpolateEnvironmentVars = (environments) => { | ||
let environmentsToReturn = {}; | ||
|
||
environments.forEach(item => { | ||
let key = Object.keys(item)[0]; | ||
environmentsToReturn[key] = process.env[item[key]]; | ||
}); | ||
|
||
return environmentsToReturn; | ||
}; | ||
|
||
/** | ||
* It does the magic recursively to replace the placeholders with values, even itself | ||
* | ||
* @param values are the data and you can use it so simple or you can mix with templates (by placeholders) | ||
* @param templates are the reuse "snippet", you can use it on values and avoid to repeat your code! | ||
* @param finalFlag at the final it will check if there are some placeholders without replaced | ||
* @private | ||
*/ | ||
const _buildInterpolateVariablesRecursive = (values, templates, finalFlag) => { | ||
|
||
Object.keys(values).forEach(item => { | ||
|
||
// be careful, it is a mutable object | ||
if (isString(values[item])) { | ||
values[item] = values[item].toTemplate(templates); | ||
|
||
if (finalFlag && values[item].match(/\${.+?}/g)) { | ||
throw new Error(messages.templates.notFound.toTemplate({ template: values[item] })); | ||
} | ||
} else if (isObject(values[item]) && !isObjectEmpty(values[item])) { | ||
_buildInterpolateVariablesRecursive(values[item], templates, finalFlag); | ||
} else { | ||
throw new Error(messages.templates.invalidFormat.toTemplate({ key: item, value: values[item] })); | ||
} | ||
}); | ||
}; | ||
|
||
|
||
module.exports = { | ||
interpolateVariables | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.