Adding a onlyFound option to our handlebars system so that we can enrich only the parts we have and leave other components for further enrichment.
This commit is contained in:
parent
6e3f87c798
commit
d667276fa1
|
@ -11,6 +11,7 @@ import { getEnvironmentVariables } from "../../utils"
|
|||
import { getDefinitions } from "../../../integrations"
|
||||
|
||||
const ENV_VAR_PREFIX = "env."
|
||||
const USER_PREFIX = "user"
|
||||
|
||||
async function enrichDatasourceWithValues(datasource: Datasource) {
|
||||
const cloned = cloneDeep(datasource)
|
||||
|
@ -48,7 +49,9 @@ export async function getWithEnvVars(datasourceId: string) {
|
|||
|
||||
export function isValid(datasource: Datasource) {
|
||||
const blocks = findHBSBlocks(JSON.stringify(datasource))
|
||||
const validList = blocks.filter(block => block.includes(ENV_VAR_PREFIX))
|
||||
const validList = blocks.filter(
|
||||
block => block.includes(ENV_VAR_PREFIX) || block.includes(USER_PREFIX)
|
||||
)
|
||||
return blocks.length === validList.length
|
||||
}
|
||||
|
||||
|
|
|
@ -32,11 +32,15 @@ const HELPERS = [
|
|||
// javascript helper
|
||||
new Helper(HelperFunctionNames.JS, processJS, false),
|
||||
// this help is applied to all statements
|
||||
new Helper(HelperFunctionNames.ALL, (value, { __opts }) => {
|
||||
new Helper(HelperFunctionNames.ALL, (value, inputs) => {
|
||||
const { __opts } = inputs
|
||||
if (isObject(value)) {
|
||||
return new SafeString(JSON.stringify(value))
|
||||
}
|
||||
// null/undefined values produce bad results
|
||||
if (__opts && __opts.onlyFound && value == null) {
|
||||
return __opts.input
|
||||
}
|
||||
if (value == null || typeof value !== "string") {
|
||||
return value == null ? "" : value
|
||||
}
|
||||
|
|
|
@ -146,16 +146,31 @@ module.exports.processStringSync = (string, context, opts) => {
|
|||
if (typeof string !== "string") {
|
||||
throw "Cannot process non-string types."
|
||||
}
|
||||
try {
|
||||
const template = createTemplate(string, opts)
|
||||
function process(stringPart) {
|
||||
const template = createTemplate(stringPart, opts)
|
||||
const now = Math.floor(Date.now() / 1000) * 1000
|
||||
return processors.postprocess(
|
||||
template({
|
||||
now: new Date(now).toISOString(),
|
||||
__opts: opts,
|
||||
__opts: {
|
||||
...opts,
|
||||
input: stringPart,
|
||||
},
|
||||
...context,
|
||||
})
|
||||
)
|
||||
}
|
||||
try {
|
||||
if (opts && opts.onlyFound) {
|
||||
const blocks = exports.findHBSBlocks(string)
|
||||
for (let block of blocks) {
|
||||
const outcome = process(block)
|
||||
string = string.replace(block, outcome)
|
||||
}
|
||||
return string
|
||||
} else {
|
||||
return process(string)
|
||||
}
|
||||
} catch (err) {
|
||||
return input
|
||||
}
|
||||
|
|
|
@ -221,3 +221,9 @@ describe("check find hbs blocks function", () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe("should leave HBS blocks if not found using option", () => {
|
||||
it("should replace one, leave one", async () => {
|
||||
const output = await processString("{{ a }}, {{ b }}", { b: 1 }, { onlyFound: true })
|
||||
expect(output).toBe("{{ a }}, 1")
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue