Updating to have real auto-completion on string templates library.

This commit is contained in:
mike12345567 2021-01-19 15:29:49 +00:00
parent fcad70967c
commit 759a106d2d
10 changed files with 46 additions and 38 deletions

View File

@ -31,7 +31,7 @@ const {
createLoginScreen,
} = require("../../constants/screens")
const { cloneDeep } = require("lodash/fp")
const { recurseHandlebars } = require("../../utilities/handlebars")
const { objectHandlebars } = require("../../utilities/handlebars")
const { USERS_TABLE_SCHEMA } = require("../../constants")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
@ -213,8 +213,7 @@ const createEmptyAppPackage = async (ctx, app) => {
let screensAndLayouts = []
for (let layout of BASE_LAYOUTS) {
const cloned = cloneDeep(layout)
cloned.title = app.name
screensAndLayouts.push(recurseHandlebars(cloned, app))
screensAndLayouts.push(objectHandlebars(cloned, app))
}
const homeScreen = createHomeScreen(app)

View File

@ -7,7 +7,7 @@ const fs = require("fs-extra")
const uuid = require("uuid")
const AWS = require("aws-sdk")
const { prepareUpload } = require("../deploy/utils")
const handlebars = require("handlebars")
const { stringHandlebars } = require("../../../utilities/handlebars")
const {
budibaseAppsDir,
budibaseTempDir,
@ -160,11 +160,8 @@ exports.serveApp = async function(ctx) {
objectStoreUrl: objectStoreUrl(),
})
const template = handlebars.compile(
fs.readFileSync(`${__dirname}/templates/app.hbs`, "utf8")
)
ctx.body = template({
const appHbs = fs.readFileSync(`${__dirname}/templates/app.hbs`, "utf8")
ctx.body = stringHandlebars(appHbs, {
head,
body: html,
style: css.code,

View File

@ -3,7 +3,7 @@ const actions = require("./actions")
const logic = require("./logic")
const automationUtils = require("./automationUtils")
const AutomationEmitter = require("../events/AutomationEmitter")
const { recurseHandlebars } = require("../utilities/handlebars")
const { objectHandlebars } = require("../utilities/handlebars")
handlebars.registerHelper("object", value => {
return new handlebars.SafeString(JSON.stringify(value))
@ -49,7 +49,7 @@ class Orchestrator {
let automation = this._automation
for (let step of automation.definition.steps) {
let stepFn = await this.getStepFunctionality(step.type, step.stepId)
step.inputs = recurseHandlebars(step.inputs, this._context)
step.inputs = objectHandlebars(step.inputs, this._context)
step.inputs = automationUtils.cleanInputValues(
step.inputs,
step.schema.inputs

View File

@ -1,3 +1,4 @@
const stringTemplates = require("@budibase/string-templates")
exports.recurseHandlebars = stringTemplates.recurseHandlebars
exports.objectHandlebars = stringTemplates.processObject
exports.stringHandlebars = stringTemplates.processString

View File

@ -1,2 +1,2 @@
build/
dist/
node_modules/

View File

@ -2,17 +2,20 @@
"name": "@budibase/string-templates",
"version": "0.5.3",
"description": "Handlebars wrapper for Budibase templating.",
"main": "build/bundle.js",
"main": "dist/bundle.js",
"module": "dist/bundle.js",
"license": "AGPL-3.0",
"private": true,
"types": "dist/index.d.ts",
"scripts": {
"build": "rollup -c",
"dev:builder": "rollup -cw"
"dev:builder": "tsc && rollup -cw"
},
"dependencies": {
"handlebars": "^4.7.6"
},
"devDependencies": {
"typescript": "^4.1.3",
"rollup": "^2.36.2",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-builtins": "^2.1.2",

View File

@ -6,9 +6,13 @@ import builtins from "rollup-plugin-node-builtins"
export default {
input: "src/index.js",
output: {
file: "build/bundle.js",
file: "dist/bundle.js",
format: "umd",
name: "string-templates",
exports: "named",
globals: {
"fs": "fs",
},
},
external: ["fs"],
plugins: [

View File

@ -3,13 +3,6 @@ const { registerAll } = require("./helpers/index")
const HBS_CLEANING_REGEX = /{{[^}}]*}}/g
/**
* Templates are produced after an input string has been compiled, we store these templates in-case the same
* string will be processed multiple times after the app has been loaded.
* In the future storing these templates somewhere could save some processing time for large template runs.
*/
const COMPILED_TEMPLATES = {}
const hbsInstance = handlebars.create()
registerAll(hbsInstance)
@ -61,7 +54,7 @@ function cleanHandlebars(string) {
* @param {object} context The context that handlebars should fill data from.
* @returns {object|array} The structure input, as fully updated as possible.
*/
module.exports.object = (object, context) => {
module.exports.processObject = (object, context) => {
// JSON stringify will fail if there are any cycles, stops infinite recursion
try {
JSON.stringify(object)
@ -71,11 +64,11 @@ module.exports.object = (object, context) => {
for (let key of Object.keys(object)) {
let val = object[key]
if (typeof val === "string") {
module.exports.string(object[key], context)
object[key] = module.exports.processString(object[key], context)
}
// this covers objects and arrays
else if (typeof val === "object") {
object[key] = module.exports.object(object[key], context)
object[key] = module.exports.processObject(object[key], context)
}
}
return object
@ -88,18 +81,14 @@ module.exports.object = (object, context) => {
* @param {object} context An object of information which will be used to enrich the string.
* @returns {string} The enriched string, all templates should have been replaced if they can be.
*/
module.exports.string = (string, context) => {
let template = COMPILED_TEMPLATES[string]
if (template == null) {
try {
string = cleanHandlebars(string)
template = hbsInstance.compile(string)
} catch (err) {
string = attemptToCorrectError(string)
template = hbsInstance.compile(string)
}
// store the template for later, only after we know it worked
COMPILED_TEMPLATES[string] = template
module.exports.processString = (string, context) => {
let template
try {
string = cleanHandlebars(string)
template = hbsInstance.compile(string)
} catch (err) {
string = attemptToCorrectError(string)
template = hbsInstance.compile(string)
}
return template(context)
}

View File

@ -0,0 +1,10 @@
{
"include": ["src/**/*"],
"compilerOptions": {
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "dist"
}
}

View File

@ -839,6 +839,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
typescript@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==
uglify-js@^3.1.4:
version "3.12.4"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.12.4.tgz#93de48bb76bb3ec0fc36563f871ba46e2ee5c7ee"