-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f00eb6
commit 1b71266
Showing
19 changed files
with
358 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Nice, cozy, warm big bed apartment Design interior in most sympathetic area! Complitely renovated, well-equipped, cosy studio in idyllic, over 100 years old wooden house. Calm street, fast connection to center and airport. 2024-04-07T08:45:40.283Z Amsterdam https://16.design.htmlacademy.pro/static/hotel/17.jpg https://16.design.htmlacademy.pro/static/hotel/16.jpg;https://16.design.htmlacademy.pro/static/hotel/13.jpg;https://16.design.htmlacademy.pro/static/hotel/11.jpg;https://16.design.htmlacademy.pro/static/hotel/20.jpg;https://16.design.htmlacademy.pro/static/hotel/6.jpg;https://16.design.htmlacademy.pro/static/hotel/8.jpg true false 2,4 house 5 7 709 Towels;Laptop friendly workspace;Baby seat Ангелина [email protected] https://16.design.htmlacademy.pro/static/host/avatar-angelina.jpg password1 ordinary 10 48.868610000000004;2.342499 | ||
Tile House Peaceful studio in the most wanted area in town. Quiet house Near of everything. Completely renovated. Lovely neighbourhood, lot of trendy shops, restaurants and bars in a walking distance. 2023-05-02T08:45:40.283Z Brussels https://16.design.htmlacademy.pro/static/hotel/20.jpg https://16.design.htmlacademy.pro/static/hotel/16.jpg;https://16.design.htmlacademy.pro/static/hotel/13.jpg;https://16.design.htmlacademy.pro/static/hotel/11.jpg;https://16.design.htmlacademy.pro/static/hotel/20.jpg;https://16.design.htmlacademy.pro/static/hotel/6.jpg;https://16.design.htmlacademy.pro/static/hotel/8.jpg true false 4,5 house 6 8 152 Fridge;Towels;Laptop friendly workspace;Washer Екатерина [email protected] https://16.design.htmlacademy.pro/static/host/avatar-angelina.jpg password2 pro 10 50.950361;6.961974 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Command } from './commands/command.interface.js'; | ||
import {CommandParser} from "./command-parser.js"; | ||
|
||
type CommandCollection = Record<string, Command>; | ||
|
||
export class CLIApplication { | ||
private commands: CommandCollection = {}; | ||
|
||
constructor( | ||
private readonly defaultCommand: string = '--help' | ||
) {} | ||
|
||
public registerCommands(commandList: Command[]): void { | ||
commandList.forEach((command) => { | ||
if (Object.hasOwn(this.commands, command.getName())) { | ||
throw new Error(`Command ${command.getName()} is already registered`); | ||
} | ||
this.commands[command.getName()] = command; | ||
}); | ||
} | ||
|
||
public getCommand(commandName: string): Command { | ||
return this.commands[commandName] ?? this.getDefaultCommand(); | ||
} | ||
|
||
public getDefaultCommand(): Command | never { | ||
if (! this.commands[this.defaultCommand]) { | ||
throw new Error(`The default command (${this.defaultCommand}) is not registered.`); | ||
} | ||
return this.commands[this.defaultCommand]; | ||
} | ||
|
||
public processCommand(argv: string[]): void { | ||
const parsedCommand = CommandParser.parse(argv); | ||
const [commandName] = Object.keys(parsedCommand); | ||
const command = this.getCommand(commandName); | ||
const commandArguments = parsedCommand[commandName] ?? []; | ||
command.execute(...commandArguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
type ParsedCommand = Record<string, string[]> | ||
|
||
export class CommandParser { | ||
static parse(cliArguments: string[]): ParsedCommand { | ||
const parsedCommand: ParsedCommand = {}; | ||
let currentCommand = ''; | ||
|
||
for (const argument of cliArguments) { | ||
if (argument.startsWith('--')) { | ||
parsedCommand[argument] = []; | ||
currentCommand = argument; | ||
} else if (currentCommand && argument) { | ||
parsedCommand[currentCommand].push(argument); | ||
} | ||
} | ||
|
||
return parsedCommand; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Command { | ||
getName(): string; | ||
|
||
execute(...parameters: string[]): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Command } from './command.interface.js'; | ||
|
||
export class HelpCommand implements Command { | ||
public getName(): string { | ||
return '--help'; | ||
} | ||
|
||
public async execute(..._parameters: string[]): Promise<void> { | ||
console.info(` | ||
Программа для подготовки данных для REST API сервера. | ||
Пример: | ||
cli.js --<command> [--arguments] | ||
Команды: | ||
--version: # выводит номер версии | ||
--help: # печатает этот текст | ||
--import <path>: # импортирует данные из TSV | ||
--generate <n> <path> <url> # генерирует произвольное количество тестовых данных | ||
`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Command } from './command.interface.js'; | ||
import {TSVFileReader} from "../../shared/libs/file-reader/index.js"; | ||
|
||
export class ImportCommand implements Command { | ||
public getName(): string { | ||
return '--import'; | ||
} | ||
|
||
public execute(...parameters: string[]): void { | ||
const [filename] = parameters; | ||
const fileReader = new TSVFileReader(filename.trim()); | ||
|
||
try { | ||
fileReader.read(); | ||
console.log(fileReader.toArray()); | ||
} catch (err) { | ||
|
||
if (!(err instanceof Error)) { | ||
throw err; | ||
} | ||
|
||
console.error(`Can't import data from file: ${filename}`); | ||
console.error(`Details: ${err.message}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { readFileSync } from 'node:fs'; | ||
import { resolve } from 'node:path'; | ||
|
||
import { Command } from './command.interface.js'; | ||
|
||
type PackageJSONConfig = { | ||
version: string; | ||
} | ||
|
||
function isPackageJSONConfig(value: unknown): value is PackageJSONConfig { | ||
return ( | ||
typeof value === 'object' && | ||
value !== null && | ||
!Array.isArray(value) && | ||
Object.hasOwn(value, 'version') | ||
); | ||
} | ||
|
||
export class VersionCommand implements Command { | ||
constructor( | ||
private readonly filePath: string = 'package.json' | ||
) {} | ||
|
||
private readVersion(): string { | ||
const jsonContent = readFileSync(resolve(this.filePath), 'utf-8'); | ||
const importedContent: unknown = JSON.parse(jsonContent); | ||
|
||
if (! isPackageJSONConfig(importedContent)) { | ||
throw new Error('Failed to parse json content.'); | ||
} | ||
|
||
return importedContent.version; | ||
} | ||
|
||
public getName(): string { | ||
return '--version'; | ||
} | ||
|
||
public async execute(..._parameters: string[]): Promise<void> { | ||
try { | ||
const version = this.readVersion(); | ||
console.info(version); | ||
} catch (error: unknown) { | ||
console.error(`Failed to read version from ${this.filePath}`); | ||
|
||
if (error instanceof Error) { | ||
console.error(error.message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export { CLIApplication } from './cli-application.js'; | ||
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env node | ||
import { CLIApplication, HelpCommand, VersionCommand, ImportCommand } from './cli/index.js'; | ||
|
||
function bootstrap() { | ||
const cliApplication = new CLIApplication(); | ||
cliApplication.registerCommands([ | ||
new HelpCommand(), | ||
new VersionCommand(), | ||
new ImportCommand(), | ||
]); | ||
|
||
cliApplication.processCommand(process.argv); | ||
} | ||
|
||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface FileReader { | ||
read(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { FileReader } from './file-reader.interface.js'; | ||
export { TSVFileReader } from './tsv-file-reader.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { readFileSync } from 'node:fs'; | ||
|
||
import { FileReader } from './file-reader.interface.js'; | ||
import { Offer, City, OfferType, Goods, User, Location } from '../../types/index.js'; | ||
import { TypeUser } from '../../types/user.type.js'; | ||
|
||
|
||
export class TSVFileReader implements FileReader { | ||
private rawData = ''; | ||
|
||
constructor( | ||
private readonly filename: string | ||
) {} | ||
|
||
private validateRawData(): void { | ||
if (!this.rawData) { | ||
throw new Error('File was not read'); | ||
} | ||
} | ||
|
||
private parseRawDataToOffers(): Offer[] { | ||
return this.rawData | ||
.split('\n') | ||
.filter((row) => row.trim().length > 0) | ||
.map((line) => this.parseLineToOffer(line)); | ||
} | ||
|
||
private parseLineToOffer(line: string): Offer { | ||
const [ | ||
title, | ||
description, | ||
postDate, | ||
city, | ||
previewImage, | ||
images, | ||
isPremium, | ||
isFavorite, | ||
rating, | ||
type, | ||
bedrooms, | ||
maxAdults, | ||
price, | ||
goods, | ||
firstname, | ||
email, | ||
avatarPath, | ||
password, | ||
typeUser, | ||
reviewsCount, | ||
location | ||
] = line.split('\t'); | ||
|
||
return { | ||
title, | ||
description, | ||
postDate: new Date(postDate), | ||
city: city as City, | ||
previewImage, | ||
images:this.parseImages(images), | ||
isPremium: isPremium === 'true', | ||
isFavorite: isFavorite === 'true', | ||
rating: Number.parseInt(rating, 10), | ||
type:type as OfferType, | ||
bedrooms:Number.parseInt(bedrooms,10), | ||
maxAdults:Number.parseInt(maxAdults,10), | ||
price: Number.parseInt(price, 10), | ||
goods: this.parseGoods(goods), | ||
user: this.parseUser(firstname, email, avatarPath, password, typeUser as TypeUser), | ||
reviewsCount:Number.parseInt(reviewsCount,10), | ||
location: this.parseLocation(location) | ||
}; | ||
} | ||
|
||
private parseGoods(goodsString: string): Goods[] { | ||
return goodsString.split(';').map((name) => name as Goods); | ||
} | ||
|
||
private parseLocation(locationString: string): Location { | ||
const [latitude, longitude] = locationString.split(';').map((name) => name); | ||
return {latitude:Number.parseFloat(latitude),longitude:Number.parseFloat(longitude)} ; | ||
} | ||
|
||
private parseImages(imagesString: string): string[] { | ||
return imagesString.split(';').map((name) => name); | ||
} | ||
|
||
private parseUser(firstname: string, email: string, avatarPath: string, password: string,type: TypeUser): User { | ||
return { firstname, email, avatarPath, password, type }; | ||
} | ||
|
||
public read(): void { | ||
this.rawData = readFileSync(this.filename, { encoding: 'utf-8' }); | ||
} | ||
|
||
public toArray(): Offer[] { | ||
this.validateRawData(); | ||
return this.parseRawDataToOffers(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export enum City { | ||
Paris = 'Paris', | ||
Cologne = 'Cologne', | ||
Brussels = 'Brussels', | ||
Amsterdam = 'Amsterdam', | ||
Hamburg = 'Hamburg', | ||
Dusseldorf = 'Dusseldorf', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum Goods { | ||
Breakfast = 'Breakfast', | ||
AirConditioning = 'Air conditioning', | ||
Laptop = 'Laptop friendly workspace', | ||
Baby = 'Baby seat', | ||
Washer = 'Washer', | ||
Towels = 'Towels', | ||
Fridge = 'Fridge' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export { Offer} from './offer.type.js'; | ||
export { City } from './city.type.js'; | ||
export { Goods } from './goods.type.js'; | ||
export { Location } from './location.type.js'; | ||
export { OfferType } from './offer-type.type.js'; | ||
export { User } from './user.type.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Location = { | ||
latitude: number; | ||
longitude: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export enum OfferType { | ||
Apartment = 'Apartment', | ||
House = 'House', | ||
Room = 'Room', | ||
Hotel = 'Hotel', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { City } from './city.type.js'; | ||
import { Goods } from './goods.type.js'; | ||
import { Location } from './location.type.js'; | ||
import { OfferType } from './offer-type.type.js'; | ||
import { User } from './user.type.js'; | ||
|
||
export type OfferPreview = { | ||
title: string;//наименование | ||
type: OfferType;//тип жилья | ||
price: number;//стоимость аренды | ||
city: City;//город | ||
location: Location;//координаты предложения | ||
isFavorite: boolean;//флаг избранное | ||
isPremium: boolean;//флаг премиум | ||
rating: number;//рейтинг | ||
previewImage: string;//превью изображения | ||
}; | ||
export type Offer = OfferPreview & { | ||
description: string;//описание | ||
postDate:Date;//дата публикации | ||
bedrooms: number;//количество комнат | ||
goods: Goods[];//удобства | ||
user: User;//пользователь | ||
images: string[];//фотографии жилья | ||
maxAdults: number;//Количество гостей | ||
reviewsCount :number;//количество отзывов | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type TypeUser = 'ordinary' | 'pro'; | ||
|
||
export type User = { | ||
firstname: string; | ||
email: string; | ||
avatarPath: string; | ||
password: string; | ||
type:TypeUser; | ||
|
||
} |