From 52717f80c715a19f60009a495447871ce70180dc Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Fri, 13 Oct 2023 22:41:42 +0200 Subject: [PATCH] feat(Script): add appendRaw() and prependRaw() methods --- src/Script.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Script.ts b/src/Script.ts index a3a07469..9c5fecdf 100644 --- a/src/Script.ts +++ b/src/Script.ts @@ -24,6 +24,10 @@ export class Script { eventHandlers: Record = { init: [], } + rawScripts = { + before: '', + after: '', + } constructor(props: ScriptConstructorProps) { this.filename = props.filename @@ -37,11 +41,7 @@ export class Script { let eventString = '' if (eventName === 'init') { - eventString += this.properties - .map((property) => { - return ` ${property.toString()}\n` - }) - .join('') + eventString += this.properties.map((property) => ` ${property.toString()}\n`).join('') } for (let handler of handlers) { @@ -58,7 +58,14 @@ export class Script { subroutines.push(subroutine.toString()) } - return eventStrings.join('\n') + '\n\n' + subroutines.join('\n') + const scriptSections = [ + this.rawScripts.before, + eventStrings.join('\n'), + subroutines.join('\n'), + this.rawScripts.after, + ] + + return scriptSections.filter((section) => section !== '').join('\n\n') } on(eventName: string, handler: ScriptHandler) { @@ -89,6 +96,14 @@ export class Script { return this } + appendRaw(script: string) { + this.rawScripts.after += (this.rawScripts.after !== '' ? '\n' : '') + script + } + + prependRaw(script: string) { + this.rawScripts.before += (this.rawScripts.before !== '' ? '\n' : '') + script + } + static handlerToString(handler: ScriptHandler) { const isHandlerNotAFunction = typeof handler === 'string' || handler instanceof ScriptCommand || Array.isArray(handler)