Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow custom line writers #197

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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