diff options
author | Nick Van Doorn <vandoorn.nick@gmail.com> | 2018-12-09 23:09:59 -0800 |
---|---|---|
committer | Nick Van Doorn <vandoorn.nick@gmail.com> | 2018-12-09 23:09:59 -0800 |
commit | fff69c079204bb5e55328a715cfd59ad49fdc44b (patch) | |
tree | 7d1a7b52f45ed0832c72e613adfa170ba0afcd35 | |
parent | 5d683d41983e4cfd17eaedcd05de8319a980be92 (diff) |
Implement file based Reporter
-rw-r--r-- | src/reporters/file/file.ts | 40 | ||||
-rw-r--r-- | src/reporters/file/index.ts | 1 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/reporters/file/file.ts b/src/reporters/file/file.ts new file mode 100644 index 0000000..5acde1e --- /dev/null +++ b/src/reporters/file/file.ts @@ -0,0 +1,40 @@ +import { Reporter } from '../../models/reporter.model' +import { Report } from '../../models/report.model' +import { Context } from '../../models/context.model' +import { nows as now } from '../../util' + +import { writeFile } from 'fs' + +const appendToFileName = (path: string, toAppend: string): string => { + const comps = path.split('.') + comps.splice(-1, -2, toAppend) + return comps.join('.') +} + +/** + * Implementation of a Reporter that simply dumps + * an array of Report objects into a file named + * using config.reportFilename + */ +export class File implements Reporter { + name = 'File System Reporter' + private buff: Report[] = [] + constructor(private ctx: Context) {} + public record(r: Report): void { + this.buff.push(r) + } + + public toString() { + return JSON.stringify(this.buff, null, 2) + } + + public publish(): Promise<void> { + return new Promise((resolve, reject) => { + const filename = appendToFileName(this.ctx.config.reportFilename, now()) + // the Node fs API has aged great, thanks ry + writeFile(filename, this.toString(), err => + err ? reject(err) : resolve() + ) + }) + } +} diff --git a/src/reporters/file/index.ts b/src/reporters/file/index.ts new file mode 100644 index 0000000..dd901ee --- /dev/null +++ b/src/reporters/file/index.ts @@ -0,0 +1 @@ +export { File } from './file' |