Convert helpers

This commit is contained in:
Adria Navarro 2024-02-21 19:40:50 +01:00
parent cdf251f9cc
commit b457b0e023
8 changed files with 43 additions and 53 deletions

View File

@ -1,11 +1,7 @@
class JsErrorTimeout extends Error { export class JsErrorTimeout extends Error {
code = "ERR_SCRIPT_EXECUTION_TIMEOUT" code = "ERR_SCRIPT_EXECUTION_TIMEOUT"
constructor() { constructor() {
super() super()
} }
} }
module.exports = {
JsErrorTimeout,
}

View File

@ -1,4 +1,4 @@
class Helper { export default class Helper {
constructor(name, fn, useValueFallback = true) { constructor(name, fn, useValueFallback = true) {
this.name = name this.name = name
this.fn = fn this.fn = fn
@ -25,5 +25,3 @@ class Helper {
handlebars.unregisterHelper(this.name) handlebars.unregisterHelper(this.name)
} }
} }
module.exports = Helper

View File

@ -1,4 +1,4 @@
module.exports.HelperFunctionBuiltin = [ export const HelperFunctionBuiltin = [
"#if", "#if",
"#unless", "#unless",
"#each", "#each",
@ -15,11 +15,11 @@ module.exports.HelperFunctionBuiltin = [
"with", "with",
] ]
module.exports.HelperFunctionNames = { export const HelperFunctionNames = {
OBJECT: "object", OBJECT: "object",
ALL: "all", ALL: "all",
LITERAL: "literal", LITERAL: "literal",
JS: "js", JS: "js",
} }
module.exports.LITERAL_MARKER = "%LITERAL%" export const LITERAL_MARKER = "%LITERAL%"

View File

@ -1,4 +1,4 @@
const dayjs = require("dayjs") import dayjs from "dayjs"
dayjs.extend(require("dayjs/plugin/duration")) dayjs.extend(require("dayjs/plugin/duration"))
dayjs.extend(require("dayjs/plugin/advancedFormat")) dayjs.extend(require("dayjs/plugin/advancedFormat"))
dayjs.extend(require("dayjs/plugin/isoWeek")) dayjs.extend(require("dayjs/plugin/isoWeek"))
@ -83,7 +83,7 @@ function setLocale(str, pattern, options) {
dayjs.locale(opts.lang || opts.language) dayjs.locale(opts.lang || opts.language)
} }
module.exports.date = (str, pattern, options) => { export const date = (str, pattern, options) => {
const config = initialConfig(str, pattern, options) const config = initialConfig(str, pattern, options)
// if no args are passed, return a formatted date // if no args are passed, return a formatted date
@ -109,7 +109,7 @@ module.exports.date = (str, pattern, options) => {
return date.format(config.pattern) return date.format(config.pattern)
} }
module.exports.duration = (str, pattern, format) => { export const duration = (str, pattern, format) => {
const config = initialConfig(str, pattern) const config = initialConfig(str, pattern)
setLocale(config.str, config.pattern) setLocale(config.str, config.pattern)

View File

@ -1,6 +1,6 @@
const helpers = require("@budibase/handlebars-helpers") import helpers from "@budibase/handlebars-helpers"
const { date, duration } = require("./date") import { date, duration } from "./date"
const { HelperFunctionBuiltin } = require("./constants") import { HelperFunctionBuiltin } from "./constants"
/** /**
* full list of supported helpers can be found here: * full list of supported helpers can be found here:
@ -24,10 +24,10 @@ const ADDED_HELPERS = {
duration: duration, duration: duration,
} }
exports.externalCollections = EXTERNAL_FUNCTION_COLLECTIONS export const externalCollections = EXTERNAL_FUNCTION_COLLECTIONS
exports.addedHelpers = ADDED_HELPERS export const addedHelpers = ADDED_HELPERS
exports.registerAll = handlebars => { export function registerAll(handlebars) {
for (let [name, helper] of Object.entries(ADDED_HELPERS)) { for (let [name, helper] of Object.entries(ADDED_HELPERS)) {
handlebars.registerHelper(name, helper) handlebars.registerHelper(name, helper)
} }
@ -52,17 +52,17 @@ exports.registerAll = handlebars => {
}) })
} }
// add date external functionality // 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)) { for (let name of Object.keys(ADDED_HELPERS)) {
handlebars.unregisterHelper(name) handlebars.unregisterHelper(name)
} }
for (let name of module.exports.externalHelperNames) { for (let name of externalHelperNames) {
handlebars.unregisterHelper(name) handlebars.unregisterHelper(name)
} }
exports.externalHelperNames = [] externalHelperNames = []
} }
exports.externalHelperNames = [] export const externalHelperNames = []

View File

@ -1,13 +1,13 @@
const Helper = require("./Helper") import Helper from "./Helper"
const { SafeString } = require("handlebars") import { SafeString } from "handlebars"
const externalHandlebars = require("./external") import * as externalHandlebars from "./external"
const { processJS } = require("./javascript") import { processJS } from "./javascript"
const { import {
HelperFunctionNames, HelperFunctionNames,
HelperFunctionBuiltin, HelperFunctionBuiltin,
LITERAL_MARKER, LITERAL_MARKER,
} = require("./constants") } from "./constants"
const { getJsHelperList } = require("./list") export { getJsHelperList } from "./list"
const HTML_SWAPS = { const HTML_SWAPS = {
"<": "&lt;", "<": "&lt;",
@ -70,31 +70,29 @@ const HELPERS = [
}), }),
] ]
module.exports.HelperNames = () => { export function HelperNames() {
return Object.values(HelperFunctionNames).concat( return Object.values(HelperFunctionNames).concat(
HelperFunctionBuiltin, HelperFunctionBuiltin,
externalHandlebars.externalHelperNames externalHandlebars.externalHelperNames
) )
} }
module.exports.registerMinimum = handlebars => { export function registerMinimum(handlebars) {
for (let helper of HELPERS) { for (let helper of HELPERS) {
helper.register(handlebars) helper.register(handlebars)
} }
} }
module.exports.registerAll = handlebars => { export function registerAll(handlebars) {
module.exports.registerMinimum(handlebars) registerMinimum(handlebars)
// register imported helpers // register imported helpers
externalHandlebars.registerAll(handlebars) externalHandlebars.registerAll(handlebars)
} }
module.exports.unregisterAll = handlebars => { export function unregisterAll(handlebars) {
for (let helper of HELPERS) { for (let helper of HELPERS) {
helper.unregister(handlebars) helper.unregister(handlebars)
} }
// unregister all imported helpers // unregister all imported helpers
externalHandlebars.unregisterAll(handlebars) externalHandlebars.unregisterAll(handlebars)
} }
module.exports.getJsHelperList = getJsHelperList

View File

@ -1,18 +1,17 @@
const { atob, isBackendService, isJSAllowed } = require("../utilities") import { atob, isBackendService, isJSAllowed } from "../utilities"
const cloneDeep = require("lodash.clonedeep") import cloneDeep from "lodash.clonedeep"
const { LITERAL_MARKER } = require("../helpers/constants") import { LITERAL_MARKER } from "../helpers/constants"
const { getJsHelperList } = require("./list") import { getJsHelperList } from "./list"
// The method of executing JS scripts depends on the bundle being built. // 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). // This setter is used in the entrypoint (either index.js or index.mjs).
let runJS let runJS
module.exports.setJSRunner = runner => (runJS = runner) export const setJSRunner = runner => (runJS = runner)
module.exports.removeJSRunner = () => {
runJS = undefined export const removeJSRunner = () => (runJS = undefined)
}
let onErrorLog let onErrorLog
module.exports.setOnErrorLog = delegate => (onErrorLog = delegate) export const setOnErrorLog = delegate => (onErrorLog = delegate)
// Helper utility to strip square brackets from a value // Helper utility to strip square brackets from a value
const removeSquareBrackets = value => { const removeSquareBrackets = value => {
@ -41,7 +40,7 @@ const getContextValue = (path, context) => {
} }
// Evaluates JS code against a certain context // Evaluates JS code against a certain context
module.exports.processJS = (handlebars, context) => { export function processJS(handlebars, context) {
if (!isJSAllowed() || (isBackendService() && !runJS)) { if (!isJSAllowed() || (isBackendService() && !runJS)) {
throw new Error("JS disabled in environment.") throw new Error("JS disabled in environment.")
} }

View File

@ -1,4 +1,4 @@
const { date, duration } = require("./date") import { date, duration } from "./date"
// https://github.com/evanw/esbuild/issues/56 // https://github.com/evanw/esbuild/issues/56
const externalCollections = { const externalCollections = {
@ -13,8 +13,7 @@ const externalCollections = {
uuid: require("@budibase/handlebars-helpers/lib/uuid"), uuid: require("@budibase/handlebars-helpers/lib/uuid"),
} }
const helpersToRemoveForJs = ["sortBy"] export const helpersToRemoveForJs = ["sortBy"]
module.exports.helpersToRemoveForJs = helpersToRemoveForJs
const addedHelpers = { const addedHelpers = {
date: date, date: date,
@ -23,7 +22,7 @@ const addedHelpers = {
let helpers = undefined let helpers = undefined
module.exports.getJsHelperList = () => { export function getJsHelperList() {
if (helpers) { if (helpers) {
return helpers return helpers
} }