Skip to content

Commit

Permalink
feat: add ShellScript class with external deps zx and add trace sto…
Browse files Browse the repository at this point in the history
…rage
  • Loading branch information
danpacho committed Mar 17, 2024
1 parent 3a5f0b7 commit 31771b7
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions packages/build_system/src/publisher/shell.script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { type Logger } from '@blogger/logger'
import { ProcessOutput, $ as zx$ } from 'zx'
import { Queue } from '../utils/queue'
// import { ShellError } from './shell.error'

type Command = string

interface ShellTraceStorageConstructor {
logger: Logger
maxTraceCount: number
}
class ShellTraceStorage {
public constructor(private readonly options: ShellTraceStorageConstructor) {
this.storage = new Queue<{
command: Command
output: ProcessOutput
}>({
size: this.options.maxTraceCount,
})
}

private storage: Queue<{
command: Command
output: ProcessOutput
}>

public add(command: Command, output: ProcessOutput) {
this.storage.enqueue({ command, output })
}

public get length() {
return this.storage.length
}

public getLatest() {
return this.storage.getTop()
}

public search(command: Command): ProcessOutput | undefined {
for (const trace of this.storage.queue.values()) {
if (trace.command === command) return trace.output
}
return undefined
}

public getCommandTrace(): Array<string> {
return Array.from(this.storage.queue.values()).map(
(trace) => trace.command
)
}

public getOutputTrace(): Array<ProcessOutput> {
return Array.from(this.storage.queue.values()).map(
(trace) => trace.output
)
}
}

interface ShellScriptConstructor extends ShellTraceStorageConstructor {}
export class ShellScript {
constructor(public readonly options: ShellScriptConstructor) {
this.traceStorage = new ShellTraceStorage({
...options,
})
}

public readonly traceStorage: ShellTraceStorage

public async $(
commands: TemplateStringsArray,
...expressions: Array<string>
): Promise<ProcessOutput> {
try {
const output: ProcessOutput = await zx$(commands, ...expressions)
this.traceStorage.add(commands.join(' '), output)
return output
} catch (e) {
//TODO: process shell error based on traceStorage
if (e instanceof Error) {
throw e
} else {
throw new Error('Unknown Error')
}
}
}
}

0 comments on commit 31771b7

Please sign in to comment.