-
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.
Merge pull request #2 from skudinva/module1-task2
- Loading branch information
Showing
27 changed files
with
512 additions
and
17 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
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 @@ | ||
1 Canal View Prinsengracht Discover daily local life in city center, friendly neighborhood, clandestine casino, karaoke, old-style artisans, art gallery and artist studio downstairs. 2024-08-10T08:11:23.283Z Paris https://16.design.htmlacademy.pro/static/hotel/3.jpg https://16.design.htmlacademy.pro/static/hotel/17.jpg;https://16.design.htmlacademy.pro/static/hotel/8.jpg;https://16.design.htmlacademy.pro/static/hotel/4.jpg;https://16.design.htmlacademy.pro/static/hotel/10.jpg;https://16.design.htmlacademy.pro/static/hotel/13.jpg;https://16.design.htmlacademy.pro/static/hotel/12.jpg true false 3 house 1 6 235 Kitchen;Babyseat;Breakfast;CableTV Angelina 48.868610000000004 2.342499 | ||
2 Wood and stone place Relax, rejuvenate and unplug in this ultimate rustic getaway experience in the country. In our beautiful screened Pondhouse, you can gaze at the stars and listen to the sounds of nature from your cozy warm bed. 2024-06-11T10:05:00.283Z Paris https://16.design.htmlacademy.pro/static/hotel/15.jpg https://16.design.htmlacademy.pro/static/hotel/3.jpg;https://16.design.htmlacademy.pro/static/hotel/4.jpg;https://16.design.htmlacademy.pro/static/hotel/9.jpg;https://16.design.htmlacademy.pro/static/hotel/2.jpg;https://16.design.htmlacademy.pro/static/hotel/1.jpg;https://16.design.htmlacademy.pro/static/hotel/10.jpg true false 4,6 house 4 3 376 CableTV;Fridge;Babyseat;Washer;Airconditioning Mikhail 48.858610000000006 2.330499 |
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,14 @@ | ||
import { UserInfo } from '../src/shared/types/index.js'; | ||
|
||
export const usersMock: UserInfo[] = [ | ||
{ | ||
name: 'Angelina', | ||
avatarUrl: 'http://avatar.ru/images/1.png', | ||
isPro: true, | ||
}, | ||
{ | ||
name: 'Mikhail', | ||
avatarUrl: 'http://avatar.ru/images/2.png', | ||
isPro: false, | ||
}, | ||
]; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -30,5 +30,8 @@ | |
"engines": { | ||
"node": "^20.0.0", | ||
"npm": ">=10" | ||
}, | ||
"dependencies": { | ||
"chalk": "^5.3.0" | ||
} | ||
} |
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 { CommandParser } from './command-parser.js'; | ||
import { Command } from './commands/command.interface.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,4 @@ | ||
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,36 @@ | ||
import chalk from 'chalk'; | ||
import { Command } from './command.interface.js'; | ||
|
||
export class HelpCommand implements Command { | ||
public getName(): string { | ||
return '--help'; | ||
} | ||
|
||
public async execute(..._parameters: string[]): Promise<void> { | ||
const helpText = this.getCommandsFormat(` | ||
--version: # выводит номер версии | ||
--help: # печатает этот текст | ||
--import <path>: # импортирует данные из TSV | ||
--generate <n> <path> <url>: # генерирует произвольное количество тестовых данных`); | ||
|
||
console.info(` | ||
Программа для подготовки данных для REST API сервера. | ||
Пример: | ||
${chalk.green('cli.js --<command> [--arguments]')} | ||
Команды: ${helpText} | ||
`); | ||
} | ||
|
||
private getCommandsFormat(text: string): string { | ||
return text | ||
.split('\n') | ||
.map((line) => { | ||
if (!line.trim().length) { | ||
return line; | ||
} | ||
const textCols = line.split(':'); | ||
return [chalk.blue(textCols[0]), textCols[1]].join(':'); | ||
}) | ||
.join('\n'); | ||
} | ||
} |
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,25 @@ | ||
import { TSVFileReader } from '../../shared/libs/file-reader/index.js'; | ||
import { Command } from './command.interface.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,49 @@ | ||
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,5 @@ | ||
export { CLIApplication } from './cli-application.js'; | ||
export { CommandParser } from './command-parser.js'; | ||
export { HelpCommand } from './commands/help-command.js'; | ||
export { ImportCommand } from './commands/import-command.js'; | ||
export { VersionCommand } from './commands/version-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,20 @@ | ||
#!/usr/bin/env node | ||
import { | ||
CLIApplication, | ||
HelpCommand, | ||
ImportCommand, | ||
VersionCommand, | ||
} 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,47 @@ | ||
import { CityName } from './types/city-name.enum.js'; | ||
import { City } from './types/city.type.js'; | ||
|
||
export const Cities: [City, City, City, City, City, City] = [ | ||
{ | ||
name: CityName.Paris, | ||
location: { | ||
latitude: 48.85661, | ||
longitude: 2.351499, | ||
}, | ||
}, | ||
{ | ||
name: CityName.Cologne, | ||
location: { | ||
latitude: 50.938361, | ||
longitude: 6.959974, | ||
}, | ||
}, | ||
{ | ||
name: CityName.Brussels, | ||
location: { | ||
latitude: 50.846557, | ||
longitude: 4.351697, | ||
}, | ||
}, | ||
{ | ||
name: CityName.Amsterdam, | ||
location: { | ||
latitude: 52.37454, | ||
longitude: 4.897976, | ||
}, | ||
}, | ||
{ | ||
name: CityName.Hamburg, | ||
location: { | ||
latitude: 53.550341, | ||
longitude: 10.000654, | ||
}, | ||
}, | ||
{ | ||
name: CityName.Dusseldorf, | ||
location: { | ||
latitude: 51.225402, | ||
longitude: 6.776314, | ||
}, | ||
}, | ||
]; |
Oops, something went wrong.