Fixing up the typing in the gen collection script so that it is valid TS again and will work.

This commit is contained in:
mike12345567 2024-07-04 14:50:59 +01:00
parent 8118a55f1b
commit 332bf09116
1 changed files with 32 additions and 17 deletions

View File

@ -1,16 +1,23 @@
const HELPER_LIBRARY = "@budibase/handlebars-helpers" import { HelperFunctionBuiltin } from "../src/helpers/constants"
const helpers = require(HELPER_LIBRARY) import { readFileSync, writeFileSync } from "fs"
const { HelperFunctionBuiltin } = require("../src/helpers/constants") import { marked } from "marked"
const fs = require("fs") import { join, dirname } from "path"
const helpers = require("@budibase/handlebars-helpers")
const doctrine = require("doctrine") const doctrine = require("doctrine")
const marked = require("marked")
type HelperInfo = {
acceptsInline?: boolean
acceptsBlock?: boolean
example?: string
description: string
tags?: any[]
}
/** /**
* full list of supported helpers can be found here: * full list of supported helpers can be found here:
* https://github.com/budibase/handlebars-helpers * https://github.com/budibase/handlebars-helpers
*/ */
const { join } = require("path")
const path = require("path")
const COLLECTIONS = [ const COLLECTIONS = [
"math", "math",
@ -23,7 +30,7 @@ const COLLECTIONS = [
"uuid", "uuid",
] ]
const FILENAME = join(__dirname, "..", "src", "manifest.json") const FILENAME = join(__dirname, "..", "src", "manifest.json")
const outputJSON = {} const outputJSON: any = {}
const ADDED_HELPERS = { const ADDED_HELPERS = {
date: { date: {
date: { date: {
@ -43,7 +50,7 @@ const ADDED_HELPERS = {
}, },
} }
function fixSpecialCases(name, obj) { function fixSpecialCases(name: string, obj: any) {
const args = obj.args const args = obj.args
if (name === "ifNth") { if (name === "ifNth") {
args[0] = "a" args[0] = "a"
@ -61,7 +68,7 @@ function fixSpecialCases(name, obj) {
return obj return obj
} }
function lookForward(lines, funcLines, idx) { function lookForward(lines: string[], funcLines: string[], idx: number) {
const funcLen = funcLines.length const funcLen = funcLines.length
for (let i = idx, j = 0; i < idx + funcLen; ++i, j++) { for (let i = idx, j = 0; i < idx + funcLen; ++i, j++) {
if (!lines[i].includes(funcLines[j])) { if (!lines[i].includes(funcLines[j])) {
@ -71,7 +78,7 @@ function lookForward(lines, funcLines, idx) {
return true return true
} }
function getCommentInfo(file, func) { function getCommentInfo(file: string, func: string): HelperInfo {
const lines = file.split("\n") const lines = file.split("\n")
const funcLines = func.split("\n") const funcLines = func.split("\n")
let comment = null let comment = null
@ -98,7 +105,13 @@ function getCommentInfo(file, func) {
if (comment == null) { if (comment == null) {
return { description: "" } return { description: "" }
} }
const docs = doctrine.parse(comment, { unwrap: true }) const docs: {
acceptsInline?: boolean
acceptsBlock?: boolean
example: string
description: string
tags: any[]
} = doctrine.parse(comment, { unwrap: true })
// some hacky fixes // some hacky fixes
docs.description = docs.description.replace(/\n/g, " ") docs.description = docs.description.replace(/\n/g, " ")
docs.description = docs.description.replace(/[ ]{2,}/g, " ") docs.description = docs.description.replace(/[ ]{2,}/g, " ")
@ -120,7 +133,7 @@ function getCommentInfo(file, func) {
return docs return docs
} }
const excludeFunctions = { string: ["raw"] } const excludeFunctions: Record<string, string[]> = { string: ["raw"] }
/** /**
* This script is very specific to purpose, parsing the handlebars-helpers files to attempt to get information about them. * This script is very specific to purpose, parsing the handlebars-helpers files to attempt to get information about them.
@ -128,11 +141,13 @@ const excludeFunctions = { string: ["raw"] }
function run() { function run() {
const foundNames: string[] = [] const foundNames: string[] = []
for (let collection of COLLECTIONS) { for (let collection of COLLECTIONS) {
const collectionFile = fs.readFileSync( const collectionFile = readFileSync(
`${path.dirname(require.resolve(HELPER_LIBRARY))}/lib/${collection}.js`, `${dirname(
require.resolve("@budibase/handlebars-helpers")
)}/lib/${collection}.js`,
"utf8" "utf8"
) )
const collectionInfo = {} const collectionInfo: any = {}
// collect information about helper // collect information about helper
let hbsHelperInfo = helpers[collection]() let hbsHelperInfo = helpers[collection]()
for (let entry of Object.entries(hbsHelperInfo)) { for (let entry of Object.entries(hbsHelperInfo)) {
@ -181,7 +196,7 @@ function run() {
helper.description = marked.parse(helper.description) helper.description = marked.parse(helper.description)
} }
} }
fs.writeFileSync(FILENAME, JSON.stringify(outputJSON, null, 2)) writeFileSync(FILENAME, JSON.stringify(outputJSON, null, 2))
} }
run() run()