diff options
Diffstat (limited to 'src/js/_codegen/helpers.ts')
-rw-r--r-- | src/js/_codegen/helpers.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/js/_codegen/helpers.ts b/src/js/_codegen/helpers.ts new file mode 100644 index 000000000..7f0c29ba0 --- /dev/null +++ b/src/js/_codegen/helpers.ts @@ -0,0 +1,44 @@ +import fs from "fs"; +import path from "path"; + +export function fmtCPPString(str: string) { + return ( + '"' + + str + .replace(/\\/g, "\\\\") + .replace(/"/g, '\\"') + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/\t/g, "\\t") + .replace(/\?/g, "\\?") + // https://stackoverflow.com/questions/1234582 + '"' + ); +} + +export function cap(str: string) { + return str[0].toUpperCase() + str.slice(1); +} + +export function low(str: string) { + if (str.startsWith("JS")) { + return "js" + str.slice(2); + } + + return str[0].toLowerCase() + str.slice(1); +} + +export function readdirRecursive(root: string): string[] { + const files = fs.readdirSync(root, { withFileTypes: true }); + return files.flatMap(file => { + const fullPath = path.join(root, file.name); + return file.isDirectory() ? readdirRecursive(fullPath) : fullPath; + }); +} + +export function resolveSyncOrNull(specifier: string, from: string) { + try { + return Bun.resolveSync(specifier, from); + } catch { + return null; + } +} |