Skip to content

Commit

Permalink
feat: allow custom line writers (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf authored and OrKoN committed Jun 20, 2022
1 parent 53be3b7 commit 4c945b5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/LineWriterImpl.ts → src/InMemoryLineWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
17 changes: 6 additions & 11 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('}');
Expand Down
12 changes: 6 additions & 6 deletions test/PuppeteerStringifyExtension_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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());
});
Expand All @@ -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());
});
Expand All @@ -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());
});
Expand All @@ -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());
});
Expand All @@ -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());
});
Expand Down

0 comments on commit 4c945b5

Please sign in to comment.