Skip to content

Commit

Permalink
Добавит команду для генерации моков
Browse files Browse the repository at this point in the history
  • Loading branch information
RnizereB committed Oct 13, 2024
1 parent 8b45a62 commit 3173111
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 3 deletions.
49 changes: 49 additions & 0 deletions src/cli/commands/generate.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import got from 'got';
import { Command } from './command.interface.js';
import { MockServerData } from '../../shared/types/index.js';
import { TSVOfferGenerator } from '../../shared/libs/offer-generator/tsv-offer-generate.js';
import { TSVFileWriter } from '../../shared/libs/file-writer/tsv-file-wtiter.js';

export class GenerateCommand implements Command {
private initialData: MockServerData;

private async load(url: string) {
try {
this.initialData = await got.get(url).json();
} catch {
throw new Error (`Can't load data from ${url}`);
}
}

private async write(filepath: string, offerCount: number) {
const tsvOfferGenerator = new TSVOfferGenerator(this.initialData);
const tsvFileWriter = new TSVFileWriter(filepath);
for (let i = 0; i < offerCount; i++) {
await tsvFileWriter.write(tsvOfferGenerator.generate());
}
}

public getName(): string {
return '--generate';
}

public async execute(...parameters: string[]): Promise<void> {
const [count, filepath, url] = parameters;
const offerCount = Number.parseInt(count, 10);

try {
await this.load(url);
await this.write(filepath, offerCount);
console.info(`File ${filepath} was created!`);

} catch (error: unknown) {
console.error('Can\'t generate data');

if (error instanceof Error) {
console.error('error');
}
} finally {
console.log(count, filepath, url);
}
}
}
1 change: 1 addition & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { CommandParser } from './command-parser.js';
export { HelpCommand } from './commands/help.command.js';
export { VersionCommand } from './commands/version.command.js';
export { ImportCommand } from './commands/import.command.js';
export { GenerateCommand } from './commands/generate.command.js';
4 changes: 2 additions & 2 deletions src/main.cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CLIApplication, HelpCommand, VersionCommand, ImportCommand } from './cli/index.js';
import { CLIApplication, HelpCommand, VersionCommand, ImportCommand, GenerateCommand} from './cli/index.js';

const bootstrap = () => {
const cliApplication = new CLIApplication();
cliApplication.registerCommands([new HelpCommand(), new VersionCommand(), new ImportCommand()]);
cliApplication.registerCommands([new HelpCommand(), new VersionCommand(), new ImportCommand(), new GenerateCommand()]);

cliApplication.processCommand(process.argv);
};
Expand Down
1 change: 1 addition & 0 deletions src/shared/libs/offer-generator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { OfferGenerator } from './offer-generator.interface.js';
3 changes: 3 additions & 0 deletions src/shared/libs/offer-generator/offer-generator.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface OfferGenerator {
generate(): string
}
55 changes: 55 additions & 0 deletions src/shared/libs/offer-generator/tsv-offer-generate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import dayjs from 'dayjs';
import { getRandomElement, getRandomItems, getRandomNumber } from '../../helpers/index.js';
import { MockServerData } from '../../types/index.js';
import { OfferGenerator } from './index.js';

const FIRST_WEEK_DAY = 1;
const LAST_WEEK_DAY = 7;

const MINIMUM_RATING = 1;
const MAXIMUM_RATING = 5;

const MAX_BEDROOMS = 5;
const MIN_BEDROOM = 1;

const MIN_ADULT = 1;
const MAX_ADULTS = 5;

const MIN_PRICE = 560;
const MAX_PRICE = 3500;

export class TSVOfferGenerator implements OfferGenerator {
constructor(private readonly mockData: MockServerData) {}

public generate(): string {
const title = getRandomElement<string>(this.mockData.title);
const description = getRandomElement<string>(this.mockData.description);
const postDate = dayjs().subtract(getRandomNumber(FIRST_WEEK_DAY, LAST_WEEK_DAY), 'day').toISOString();
const city = getRandomElement<string>(this.mockData.city);
const previewImage = getRandomElement<string>(this.mockData.previewImage);
const images = getRandomItems<string>(this.mockData.images).join(';');
const isFavorite = Math.random() < 0.5;
const isPremium = Math.random() < 0.5;
const rating = getRandomNumber(MINIMUM_RATING, MAXIMUM_RATING);
const type = getRandomElement<string>(this.mockData.type);
const bedrooms = getRandomNumber(MIN_BEDROOM, MAX_BEDROOMS);
const maxAdults = getRandomNumber(MIN_ADULT, MAX_ADULTS);
const price = getRandomNumber(MIN_PRICE, MAX_PRICE);
const goods = getRandomItems<string>(this.mockData.goods).join(';');
const name = getRandomElement<string>(this.mockData.name);
const email = getRandomElement<string>(this.mockData.email);
const avatarUser = getRandomElement<string>(this.mockData.avatarUser);
const password = getRandomElement<string>(this.mockData.password);
const typeUser = getRandomElement<string>(this.mockData.typeUser);

return [
title, description, postDate,
city, previewImage, images,
isFavorite, isPremium, rating,
type, bedrooms, maxAdults,
price, goods, name,
email, avatarUser, password,
typeUser
].join('\t');
}
}
3 changes: 2 additions & 1 deletion src/shared/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Offer } from './offer-type.js';
export { TypeUser, User} from './user-type.js';
export { TypeUser, User } from './user-type.js';
export { MockServerData } from './mock-server-data.js';
15 changes: 15 additions & 0 deletions src/shared/types/mock-server-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

export type MockServerData = {
title: string[],
description: string[],
city: string[],
previewImage: string[],
images: string[],
type: string[],
goods: string[],
name: string[];
email: string[];
avatarUser: string[];
password: string[];
typeUser: string[];
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
Expand Down

0 comments on commit 3173111

Please sign in to comment.