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 d22ea5c722
commit 329b76590c
10 changed files with 46 additions and 38 deletions

View File

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

View File

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

View File

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

View File

@ -1,3 +1,4 @@
const stringTemplates = require("@budibase/string-templates") 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/ node_modules/

View File

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

View File

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

View File

@ -3,13 +3,6 @@ const { registerAll } = require("./helpers/index")
const HBS_CLEANING_REGEX = /{{[^}}]*}}/g 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() const hbsInstance = handlebars.create()
registerAll(hbsInstance) registerAll(hbsInstance)
@ -61,7 +54,7 @@ function cleanHandlebars(string) {
* @param {object} context The context that handlebars should fill data from. * @param {object} context The context that handlebars should fill data from.
* @returns {object|array} The structure input, as fully updated as possible. * @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 // JSON stringify will fail if there are any cycles, stops infinite recursion
try { try {
JSON.stringify(object) JSON.stringify(object)
@ -71,11 +64,11 @@ module.exports.object = (object, context) => {
for (let key of Object.keys(object)) { for (let key of Object.keys(object)) {
let val = object[key] let val = object[key]
if (typeof val === "string") { if (typeof val === "string") {
module.exports.string(object[key], context) object[key] = module.exports.processString(object[key], context)
} }
// this covers objects and arrays // this covers objects and arrays
else if (typeof val === "object") { else if (typeof val === "object") {
object[key] = module.exports.object(object[key], context) object[key] = module.exports.processObject(object[key], context)
} }
} }
return object return object
@ -88,9 +81,8 @@ module.exports.object = (object, context) => {
* @param {object} context An object of information which will be used to enrich the string. * @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. * @returns {string} The enriched string, all templates should have been replaced if they can be.
*/ */
module.exports.string = (string, context) => { module.exports.processString = (string, context) => {
let template = COMPILED_TEMPLATES[string] let template
if (template == null) {
try { try {
string = cleanHandlebars(string) string = cleanHandlebars(string)
template = hbsInstance.compile(string) template = hbsInstance.compile(string)
@ -98,9 +90,6 @@ module.exports.string = (string, context) => {
string = attemptToCorrectError(string) string = attemptToCorrectError(string)
template = hbsInstance.compile(string) template = hbsInstance.compile(string)
} }
// store the template for later, only after we know it worked
COMPILED_TEMPLATES[string] = template
}
return template(context) 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" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= 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: uglify-js@^3.1.4:
version "3.12.4" version "3.12.4"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.12.4.tgz#93de48bb76bb3ec0fc36563f871ba46e2ee5c7ee" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.12.4.tgz#93de48bb76bb3ec0fc36563f871ba46e2ee5c7ee"