Convert helpers
This commit is contained in:
parent
cdf251f9cc
commit
b457b0e023
|
@ -1,11 +1,7 @@
|
|||
class JsErrorTimeout extends Error {
|
||||
export class JsErrorTimeout extends Error {
|
||||
code = "ERR_SCRIPT_EXECUTION_TIMEOUT"
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
JsErrorTimeout,
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
class Helper {
|
||||
export default class Helper {
|
||||
constructor(name, fn, useValueFallback = true) {
|
||||
this.name = name
|
||||
this.fn = fn
|
||||
|
@ -25,5 +25,3 @@ class Helper {
|
|||
handlebars.unregisterHelper(this.name)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Helper
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
module.exports.HelperFunctionBuiltin = [
|
||||
export const HelperFunctionBuiltin = [
|
||||
"#if",
|
||||
"#unless",
|
||||
"#each",
|
||||
|
@ -15,11 +15,11 @@ module.exports.HelperFunctionBuiltin = [
|
|||
"with",
|
||||
]
|
||||
|
||||
module.exports.HelperFunctionNames = {
|
||||
export const HelperFunctionNames = {
|
||||
OBJECT: "object",
|
||||
ALL: "all",
|
||||
LITERAL: "literal",
|
||||
JS: "js",
|
||||
}
|
||||
|
||||
module.exports.LITERAL_MARKER = "%LITERAL%"
|
||||
export const LITERAL_MARKER = "%LITERAL%"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const dayjs = require("dayjs")
|
||||
import dayjs from "dayjs"
|
||||
dayjs.extend(require("dayjs/plugin/duration"))
|
||||
dayjs.extend(require("dayjs/plugin/advancedFormat"))
|
||||
dayjs.extend(require("dayjs/plugin/isoWeek"))
|
||||
|
@ -83,7 +83,7 @@ function setLocale(str, pattern, options) {
|
|||
dayjs.locale(opts.lang || opts.language)
|
||||
}
|
||||
|
||||
module.exports.date = (str, pattern, options) => {
|
||||
export const date = (str, pattern, options) => {
|
||||
const config = initialConfig(str, pattern, options)
|
||||
|
||||
// if no args are passed, return a formatted date
|
||||
|
@ -109,7 +109,7 @@ module.exports.date = (str, pattern, options) => {
|
|||
return date.format(config.pattern)
|
||||
}
|
||||
|
||||
module.exports.duration = (str, pattern, format) => {
|
||||
export const duration = (str, pattern, format) => {
|
||||
const config = initialConfig(str, pattern)
|
||||
|
||||
setLocale(config.str, config.pattern)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const helpers = require("@budibase/handlebars-helpers")
|
||||
const { date, duration } = require("./date")
|
||||
const { HelperFunctionBuiltin } = require("./constants")
|
||||
import helpers from "@budibase/handlebars-helpers"
|
||||
import { date, duration } from "./date"
|
||||
import { HelperFunctionBuiltin } from "./constants"
|
||||
|
||||
/**
|
||||
* full list of supported helpers can be found here:
|
||||
|
@ -24,10 +24,10 @@ const ADDED_HELPERS = {
|
|||
duration: duration,
|
||||
}
|
||||
|
||||
exports.externalCollections = EXTERNAL_FUNCTION_COLLECTIONS
|
||||
exports.addedHelpers = ADDED_HELPERS
|
||||
export const externalCollections = EXTERNAL_FUNCTION_COLLECTIONS
|
||||
export const addedHelpers = ADDED_HELPERS
|
||||
|
||||
exports.registerAll = handlebars => {
|
||||
export function registerAll(handlebars) {
|
||||
for (let [name, helper] of Object.entries(ADDED_HELPERS)) {
|
||||
handlebars.registerHelper(name, helper)
|
||||
}
|
||||
|
@ -52,17 +52,17 @@ exports.registerAll = handlebars => {
|
|||
})
|
||||
}
|
||||
// add date external functionality
|
||||
exports.externalHelperNames = externalNames.concat(Object.keys(ADDED_HELPERS))
|
||||
externalHelperNames = externalNames.concat(Object.keys(ADDED_HELPERS))
|
||||
}
|
||||
|
||||
exports.unregisterAll = handlebars => {
|
||||
export function unregisterAll(handlebars) {
|
||||
for (let name of Object.keys(ADDED_HELPERS)) {
|
||||
handlebars.unregisterHelper(name)
|
||||
}
|
||||
for (let name of module.exports.externalHelperNames) {
|
||||
for (let name of externalHelperNames) {
|
||||
handlebars.unregisterHelper(name)
|
||||
}
|
||||
exports.externalHelperNames = []
|
||||
externalHelperNames = []
|
||||
}
|
||||
|
||||
exports.externalHelperNames = []
|
||||
export const externalHelperNames = []
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
const Helper = require("./Helper")
|
||||
const { SafeString } = require("handlebars")
|
||||
const externalHandlebars = require("./external")
|
||||
const { processJS } = require("./javascript")
|
||||
const {
|
||||
import Helper from "./Helper"
|
||||
import { SafeString } from "handlebars"
|
||||
import * as externalHandlebars from "./external"
|
||||
import { processJS } from "./javascript"
|
||||
import {
|
||||
HelperFunctionNames,
|
||||
HelperFunctionBuiltin,
|
||||
LITERAL_MARKER,
|
||||
} = require("./constants")
|
||||
const { getJsHelperList } = require("./list")
|
||||
} from "./constants"
|
||||
export { getJsHelperList } from "./list"
|
||||
|
||||
const HTML_SWAPS = {
|
||||
"<": "<",
|
||||
|
@ -70,31 +70,29 @@ const HELPERS = [
|
|||
}),
|
||||
]
|
||||
|
||||
module.exports.HelperNames = () => {
|
||||
export function HelperNames() {
|
||||
return Object.values(HelperFunctionNames).concat(
|
||||
HelperFunctionBuiltin,
|
||||
externalHandlebars.externalHelperNames
|
||||
)
|
||||
}
|
||||
|
||||
module.exports.registerMinimum = handlebars => {
|
||||
export function registerMinimum(handlebars) {
|
||||
for (let helper of HELPERS) {
|
||||
helper.register(handlebars)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.registerAll = handlebars => {
|
||||
module.exports.registerMinimum(handlebars)
|
||||
export function registerAll(handlebars) {
|
||||
registerMinimum(handlebars)
|
||||
// register imported helpers
|
||||
externalHandlebars.registerAll(handlebars)
|
||||
}
|
||||
|
||||
module.exports.unregisterAll = handlebars => {
|
||||
export function unregisterAll(handlebars) {
|
||||
for (let helper of HELPERS) {
|
||||
helper.unregister(handlebars)
|
||||
}
|
||||
// unregister all imported helpers
|
||||
externalHandlebars.unregisterAll(handlebars)
|
||||
}
|
||||
|
||||
module.exports.getJsHelperList = getJsHelperList
|
||||
|
|
|
@ -1,18 +1,17 @@
|
|||
const { atob, isBackendService, isJSAllowed } = require("../utilities")
|
||||
const cloneDeep = require("lodash.clonedeep")
|
||||
const { LITERAL_MARKER } = require("../helpers/constants")
|
||||
const { getJsHelperList } = require("./list")
|
||||
import { atob, isBackendService, isJSAllowed } from "../utilities"
|
||||
import cloneDeep from "lodash.clonedeep"
|
||||
import { LITERAL_MARKER } from "../helpers/constants"
|
||||
import { getJsHelperList } from "./list"
|
||||
|
||||
// The method of executing JS scripts depends on the bundle being built.
|
||||
// This setter is used in the entrypoint (either index.js or index.mjs).
|
||||
let runJS
|
||||
module.exports.setJSRunner = runner => (runJS = runner)
|
||||
module.exports.removeJSRunner = () => {
|
||||
runJS = undefined
|
||||
}
|
||||
export const setJSRunner = runner => (runJS = runner)
|
||||
|
||||
export const removeJSRunner = () => (runJS = undefined)
|
||||
|
||||
let onErrorLog
|
||||
module.exports.setOnErrorLog = delegate => (onErrorLog = delegate)
|
||||
export const setOnErrorLog = delegate => (onErrorLog = delegate)
|
||||
|
||||
// Helper utility to strip square brackets from a value
|
||||
const removeSquareBrackets = value => {
|
||||
|
@ -41,7 +40,7 @@ const getContextValue = (path, context) => {
|
|||
}
|
||||
|
||||
// Evaluates JS code against a certain context
|
||||
module.exports.processJS = (handlebars, context) => {
|
||||
export function processJS(handlebars, context) {
|
||||
if (!isJSAllowed() || (isBackendService() && !runJS)) {
|
||||
throw new Error("JS disabled in environment.")
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const { date, duration } = require("./date")
|
||||
import { date, duration } from "./date"
|
||||
|
||||
// https://github.com/evanw/esbuild/issues/56
|
||||
const externalCollections = {
|
||||
|
@ -13,8 +13,7 @@ const externalCollections = {
|
|||
uuid: require("@budibase/handlebars-helpers/lib/uuid"),
|
||||
}
|
||||
|
||||
const helpersToRemoveForJs = ["sortBy"]
|
||||
module.exports.helpersToRemoveForJs = helpersToRemoveForJs
|
||||
export const helpersToRemoveForJs = ["sortBy"]
|
||||
|
||||
const addedHelpers = {
|
||||
date: date,
|
||||
|
@ -23,7 +22,7 @@ const addedHelpers = {
|
|||
|
||||
let helpers = undefined
|
||||
|
||||
module.exports.getJsHelperList = () => {
|
||||
export function getJsHelperList() {
|
||||
if (helpers) {
|
||||
return helpers
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue