diff --git a/src/LineWriterImpl.ts b/src/InMemoryLineWriter.ts similarity index 95% rename from src/LineWriterImpl.ts rename to src/InMemoryLineWriter.ts index 249f6727..19e12ffc 100644 --- a/src/LineWriterImpl.ts +++ b/src/InMemoryLineWriter.ts @@ -16,7 +16,7 @@ import { LineWriter } from './LineWriter.js'; -export class LineWriterImpl implements LineWriter { +export class InMemoryLineWriter implements LineWriter { #indentation: string; #currentIndentation = 0; #lines: string[] = []; diff --git a/src/stringify.ts b/src/stringify.ts index 2dbe9888..63980634 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -14,13 +14,15 @@ limitations under the License. */ -import { LineWriterImpl } from './LineWriterImpl.js'; +import { InMemoryLineWriter } from './InMemoryLineWriter.js'; +import { LineWriter } from './LineWriter.js'; import { PuppeteerStringifyExtension } from './PuppeteerStringifyExtension.js'; import type { Step, UserFlow } from './Schema.js'; import { StringifyExtension } from './StringifyExtension.js'; export interface StringifyOptions { extension?: StringifyExtension; + writer?: LineWriter; indentation?: string; } @@ -39,15 +41,8 @@ export async function stringify( if (!opts) { opts = {}; } - let ext = opts.extension; - if (!ext) { - ext = new PuppeteerStringifyExtension(); - } - - if (!opts.indentation) { - opts.indentation = ' '; - } - const out = new LineWriterImpl(opts.indentation); + const ext = opts.extension ?? new PuppeteerStringifyExtension(); + const out = opts.writer ?? new InMemoryLineWriter(opts.indentation ?? ' '); await ext.beforeAllSteps?.(out, flow); for (const step of flow.steps) { @@ -80,7 +75,7 @@ export async function stringifyStep( if (!opts.indentation) { opts.indentation = ' '; } - const out = new LineWriterImpl(opts.indentation); + const out = new InMemoryLineWriter(opts.indentation); await ext.beforeEachStep?.(out, step); await ext.stringifyStep(out, step); diff --git a/test/LineWriterImpl_test.ts b/test/InMemoryLineWriter_test.ts similarity index 85% rename from test/LineWriterImpl_test.ts rename to test/InMemoryLineWriter_test.ts index 652b97d2..52241123 100644 --- a/test/LineWriterImpl_test.ts +++ b/test/InMemoryLineWriter_test.ts @@ -14,12 +14,12 @@ limitations under the License. */ -import { LineWriterImpl } from '../src/LineWriterImpl.js'; +import { InMemoryLineWriter } from '../src/InMemoryLineWriter.js'; import { assert } from 'chai'; -describe('LineWriterImpl', () => { +describe('InMemoryLineWriter', () => { it('should open and close blocks', () => { - const out = new LineWriterImpl(' '); + const out = new InMemoryLineWriter(' '); out.appendLine('{').startBlock(); out.appendLine('console.log("test");'); out.endBlock().appendLine('}'); diff --git a/test/PuppeteerStringifyExtension_test.ts b/test/PuppeteerStringifyExtension_test.ts index 27bc4253..5df97a2c 100644 --- a/test/PuppeteerStringifyExtension_test.ts +++ b/test/PuppeteerStringifyExtension_test.ts @@ -15,7 +15,7 @@ */ import snapshot from 'snap-shot-it'; -import { LineWriterImpl } from '../src/LineWriterImpl.js'; +import { InMemoryLineWriter } from '../src/InMemoryLineWriter.js'; import { PuppeteerStringifyExtension } from '../src/PuppeteerStringifyExtension.js'; describe('PuppeteerStringifyExtension', () => { @@ -31,7 +31,7 @@ describe('PuppeteerStringifyExtension', () => { }; const flow = { title: 'test', steps: [step] }; - const writer = new LineWriterImpl(' '); + const writer = new InMemoryLineWriter(' '); await ext.stringifyStep(writer, step, flow); snapshot(writer.toString()); }); @@ -47,7 +47,7 @@ describe('PuppeteerStringifyExtension', () => { }; const flow = { title: 'test', steps: [step] }; - const writer = new LineWriterImpl(' '); + const writer = new InMemoryLineWriter(' '); await ext.stringifyStep(writer, step, flow); snapshot(writer.toString()); }); @@ -62,7 +62,7 @@ describe('PuppeteerStringifyExtension', () => { }; const flow = { title: 'test', steps: [step] }; - const writer = new LineWriterImpl(' '); + const writer = new InMemoryLineWriter(' '); await ext.stringifyStep(writer, step, flow); snapshot(writer.toString()); }); @@ -76,7 +76,7 @@ describe('PuppeteerStringifyExtension', () => { }; const flow = { title: 'test', steps: [step] }; - const writer = new LineWriterImpl(' '); + const writer = new InMemoryLineWriter(' '); await ext.stringifyStep(writer, step, flow); snapshot(writer.toString()); }); @@ -90,7 +90,7 @@ describe('PuppeteerStringifyExtension', () => { }; const flow = { title: 'test', steps: [step] }; - const writer = new LineWriterImpl(' '); + const writer = new InMemoryLineWriter(' '); await ext.stringifyStep(writer, step, flow); snapshot(writer.toString()); });