Skip to content

Commit

Permalink
feat: adds stream to tsv file writer
Browse files Browse the repository at this point in the history
  • Loading branch information
artsamoilov committed Sep 22, 2024
1 parent 4d2c5ba commit b8db73c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/cli/commands/generate.command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import got from 'got';
import { appendFile } from 'node:fs/promises';

import { ICommand } from './types/command.interface.js';
import { DECIMAL_RADIX } from '../../shared/constants/decimal-radix.const.js';
import { IMockServerData } from '../../shared/types/mock-server-data.interface.js';
import { TSVOfferGenerator } from '../../shared/libs/offer-generator/tsv-offer-generator.js';
import { getErrorMessage } from '../../shared/utils/getErrorMessage.js';
import { TSVFileWriter } from '../../shared/libs/file-writer/index.js';

export class GenerateCommand implements ICommand {
private initialData: IMockServerData;
Expand All @@ -19,8 +20,10 @@ export class GenerateCommand implements ICommand {

private async write(filePath: string, offersQuantity: number) {
const tsvOfferGenerator = new TSVOfferGenerator(this.initialData);
const tsvFileWriter = new TSVFileWriter(filePath);

for (let i = 0; i < offersQuantity; i++) {
await appendFile(filePath, `${tsvOfferGenerator.generate()}\n`, { encoding: 'utf8' });
await tsvFileWriter.write(tsvOfferGenerator.generate());
}
}

Expand All @@ -38,10 +41,7 @@ export class GenerateCommand implements ICommand {
console.info(`File ${filePath} was created!`);
} catch (error: unknown) {
console.error('Can\'t generate data');

if (error instanceof Error) {
console.error(error.message);
}
console.error(getErrorMessage(error));
}
}
}
1 change: 1 addition & 0 deletions src/shared/libs/file-writer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TSVFileWriter } from './tsv-file-writer.js';
26 changes: 26 additions & 0 deletions src/shared/libs/file-writer/tsv-file-writer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { IFileWriter } from './types/file-writer.interface.js';
import { WriteStream, createWriteStream } from 'node:fs';

export class TSVFileWriter implements IFileWriter {
private readonly stream: WriteStream;

constructor(fileName: string) {
this.stream = createWriteStream(fileName, {
flags: 'w',
encoding: 'utf-8',
autoClose: true,
});
}

public async write(row: string): Promise<void> {
const writeSuccess = this.stream.write(`${row}\n`);

if (!writeSuccess) {
return new Promise<void>((resolve) => {
this.stream.once('drain',() => resolve());
});
}

return Promise.resolve();
}
}
3 changes: 3 additions & 0 deletions src/shared/libs/file-writer/types/file-writer.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IFileWriter {
write(row: string): Promise<void>;
}

0 comments on commit b8db73c

Please sign in to comment.