From 759a106d2df68a0ef9b8c30e3b78b67dd4e7524f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 19 Jan 2021 15:29:49 +0000 Subject: [PATCH] Updating to have real auto-completion on string templates library. --- .../server/src/api/controllers/application.js | 5 ++- .../src/api/controllers/static/index.js | 9 ++--- packages/server/src/automations/thread.js | 4 +-- packages/server/src/utilities/handlebars.js | 3 +- packages/string-templates/.gitignore | 2 +- packages/string-templates/package.json | 7 ++-- packages/string-templates/rollup.config.js | 6 +++- packages/string-templates/src/index.js | 33 +++++++------------ packages/string-templates/tsconfig.json | 10 ++++++ packages/string-templates/yarn.lock | 5 +++ 10 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 packages/string-templates/tsconfig.json diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index ae980be53c..ada9cf7e32 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -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) diff --git a/packages/server/src/api/controllers/static/index.js b/packages/server/src/api/controllers/static/index.js index 52e12d92e7..38f335a13d 100644 --- a/packages/server/src/api/controllers/static/index.js +++ b/packages/server/src/api/controllers/static/index.js @@ -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, diff --git a/packages/server/src/automations/thread.js b/packages/server/src/automations/thread.js index e7046ee7ea..f3d0be3734 100644 --- a/packages/server/src/automations/thread.js +++ b/packages/server/src/automations/thread.js @@ -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 diff --git a/packages/server/src/utilities/handlebars.js b/packages/server/src/utilities/handlebars.js index 3f8093dc3e..b72305dc1a 100644 --- a/packages/server/src/utilities/handlebars.js +++ b/packages/server/src/utilities/handlebars.js @@ -1,3 +1,4 @@ const stringTemplates = require("@budibase/string-templates") -exports.recurseHandlebars = stringTemplates.recurseHandlebars +exports.objectHandlebars = stringTemplates.processObject +exports.stringHandlebars = stringTemplates.processString diff --git a/packages/string-templates/.gitignore b/packages/string-templates/.gitignore index 3e2e84b087..1eae0cf670 100644 --- a/packages/string-templates/.gitignore +++ b/packages/string-templates/.gitignore @@ -1,2 +1,2 @@ -build/ +dist/ node_modules/ diff --git a/packages/string-templates/package.json b/packages/string-templates/package.json index 241f32b7ac..0addab445a 100644 --- a/packages/string-templates/package.json +++ b/packages/string-templates/package.json @@ -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", diff --git a/packages/string-templates/rollup.config.js b/packages/string-templates/rollup.config.js index adce88fb36..fa215261dc 100644 --- a/packages/string-templates/rollup.config.js +++ b/packages/string-templates/rollup.config.js @@ -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: [ diff --git a/packages/string-templates/src/index.js b/packages/string-templates/src/index.js index 506aa26f58..9b98f42597 100644 --- a/packages/string-templates/src/index.js +++ b/packages/string-templates/src/index.js @@ -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) } diff --git a/packages/string-templates/tsconfig.json b/packages/string-templates/tsconfig.json new file mode 100644 index 0000000000..40814d1ba1 --- /dev/null +++ b/packages/string-templates/tsconfig.json @@ -0,0 +1,10 @@ +{ + "include": ["src/**/*"], + + "compilerOptions": { + "allowJs": true, + "declaration": true, + "emitDeclarationOnly": true, + "outDir": "dist" + } +} diff --git a/packages/string-templates/yarn.lock b/packages/string-templates/yarn.lock index e4afac5b7b..3fc750cadb 100644 --- a/packages/string-templates/yarn.lock +++ b/packages/string-templates/yarn.lock @@ -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"