Skip to content

Commit

Permalink
feat(Script): add appendRaw() and prependRaw() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Oct 13, 2023
1 parent 53e363d commit 52717f8
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class Script {
eventHandlers: Record<string, ScriptHandler[]> = {
init: [],
}
rawScripts = {
before: '',
after: '',
}

constructor(props: ScriptConstructorProps) {
this.filename = props.filename
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 52717f8

Please sign in to comment.