Skip to content

Commit

Permalink
Merge pull request #2 from artsamoilov/module1-task2
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Sep 15, 2024
2 parents 0866fec + 0636d4f commit 10e5ed1
Show file tree
Hide file tree
Showing 24 changed files with 400 additions and 21 deletions.
2 changes: 2 additions & 0 deletions mocks/mock-offers.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Champerret Heliopolis The Champerret Heliopolis offers charm and tranquility in the heart of Paris' 17th Arrondissement near Espace Champerret and Porte Maillot conference center. 2024-09-11T20:27:34Z Paris previewParis.jpg paris1.jpg;paris2.jpg;paris3.jpg;paris4.jpg;paris5.jpg;paris6.jpg 1 4.5 hotel 1 2 1000 Breakfast;Air conditioning /users/1 13 48.85661;2.351499
Düsseldorf 1995 Düsseldorf 1995 is located in Düsseldorf, just 2.4 miles from Central Station Düsseldorf and 2.8 miles from Südpark. The property is around 2.9 miles from Theater an der Kö, 3 miles from Königsallee, and 3.1 miles from German Opera on the Rhine. 2024-08-10T20:27:34Z Dusseldorf previewDusseldorf.jpg dusseldorf1.jpg;dusseldorf2.jpg;dusseldorf3.jpg;dusseldorf4.jpg;dusseldorf5.jpg;dusseldorf6.jpg 0 3.9 apartment 3 5 74000 Baby seat;Washer;Towels;Fridge /users/2 3 51.225402;6.776314
59 changes: 40 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "six-cities",
"version": "7.0.0",
"version": "0.0.1",
"description": "Проект «Шесть городов» от HTML Academy",
"keywords": [
"rest",
Expand All @@ -14,7 +14,7 @@
"lint": "eslint src/ --ext .ts",
"compile": "tsc -p tsconfig.json",
"clean": "rimraf dist",
"ts": "tsc --noEmit && node --no-warnings=--no-warnings=ExperimentalWarning --loader ts-node/esm"
"ts": "tsc --noEmit && node --no-warnings=ExperimentalWarning --loader ts-node/esm"
},
"devDependencies": {
"@types/node": "20.12.7",
Expand All @@ -30,5 +30,8 @@
"engines": {
"node": "^20.0.0",
"npm": ">=10"
},
"dependencies": {
"chalk": "5.3.0"
}
}
39 changes: 39 additions & 0 deletions src/cli/cli-application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ICommand } from './commands/types/command.interface.js';
import { parseCommand } from './commands/utils/parse-command.util.js';

export class CLIApplication {
private readonly commands: Record<string, ICommand> = {};

constructor(
private readonly defaultCommand: string = '--help',
) {}

public registerCommands(commandList: ICommand[]): void {
commandList.reduce((acc, command) => {
const commandName = command.getName();
if (acc[commandName]) {
throw new Error(`Command ${commandName} is already registered`);
}
acc[commandName] = command;
return acc;
}, this.commands);
}

public getDefaultCommand(): ICommand {
if (!this.commands[this.defaultCommand]) {
throw new Error(`The default command (${this.defaultCommand}) is not registered.`);
}
return this.commands[this.defaultCommand];
}

public getCommand(commandName: string): ICommand {
return this.commands[commandName] ?? this.getDefaultCommand();
}

public async processCommand(argv: string[]): Promise<void> {
const parsedCommand = parseCommand(argv);
const [commandName] = Object.keys(parsedCommand);
const commandArguments = parsedCommand[commandName] ?? [];
await this.getCommand(commandName).execute(...commandArguments);
}
}
22 changes: 22 additions & 0 deletions src/cli/commands/help.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ICommand } from './types/command.interface.js';
import chalk from 'chalk';

export class HelpCommand implements ICommand {
public getName(): string {
return '--help';
}

public async execute(..._parameters: string[]): Promise<void> {
console.info(`
${chalk.whiteBright('Программа для подготовки данных для REST API сервера.')}
${chalk.italic('Пример:')} ${chalk.whiteBright('cli.js')} ${chalk.cyanBright('--<command>')} ${chalk.yellowBright('[--arguments]')}
${chalk.italic('Команды:')}
${chalk.cyanBright('--version:')} ${chalk.italic.dim('# выводит номер версии приложения')}
${chalk.cyanBright('--help:')} ${chalk.italic.dim('# печатает справку по командам')}
${chalk.cyanBright('--import')} ${chalk.yellowBright('<path>')}: ${chalk.italic.dim('# импортирует данные из TSV файла')}
${chalk.cyanBright('--generate')} ${chalk.yellowBright('<n> <path> <url>')}: ${chalk.italic.dim('# генерирует произвольное количество тестовых данных')}
`);
}
}
31 changes: 31 additions & 0 deletions src/cli/commands/import.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ICommand } from './types/command.interface.js';
import { TSVFileReader } from '../../shared/libs/file-reader/index.js';

export class ImportCommand implements ICommand {
public getName(): string {
return '--import';
}

public async execute(...parameters: string[]): Promise<void> {
const [fileName] = parameters;

if (!fileName || fileName.trim() === '') {
throw new Error('File name is missing.');
}

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}`);
}
}
}
4 changes: 4 additions & 0 deletions src/cli/commands/types/command.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ICommand {
getName(): string;
execute(...parameters: string[]): Promise<void>;
}
3 changes: 3 additions & 0 deletions src/cli/commands/types/package-json-config.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IPackageJSONConfig {
version: string;
}
15 changes: 15 additions & 0 deletions src/cli/commands/utils/parse-command.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const parseCommand = (cliArgs: string[]): Record<string, string[]> => {
const parsedCommand: Record<string, string[]> = {};
let currentCommand = '';

for (const argument of cliArgs) {
if (argument.startsWith('--')) {
parsedCommand[argument] = [];
currentCommand = argument;
} else if (currentCommand && argument) {
parsedCommand[currentCommand].push(argument);
}
}

return parsedCommand;
};
47 changes: 47 additions & 0 deletions src/cli/commands/version.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

import { ICommand } from './types/command.interface.js';
import { IPackageJSONConfig } from './types/package-json-config.interface.js';

export class VersionCommand implements ICommand {
constructor(
private readonly filePath: string = 'package.json'
) {}

private isPackageJSONConfig(value: unknown): asserts value is IPackageJSONConfig {
if (!(
typeof value === 'object' &&
value !== null &&
!Array.isArray(value) &&
Object.hasOwn(value, 'version')
)) {
throw new Error();
}
}

private readVersion(): string {
const jsonContent = readFileSync(resolve(this.filePath), { encoding: 'utf-8' });
const importedContent: unknown = JSON.parse(jsonContent);
this.isPackageJSONConfig(importedContent);

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) {
console.error(`Failed to read version from ${this.filePath}`);

if (error instanceof Error) {
console.error(error.message);
}
}
}
}
4 changes: 4 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { CLIApplication } from './cli-application.js';
export { HelpCommand } from './commands/help.command.js';
export { VersionCommand } from './commands/version.command.js';
export { ImportCommand } from './commands/import.command.js';
15 changes: 15 additions & 0 deletions src/main.cli.ts
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';

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

await cliApplication.processCommand(process.argv);
};

await bootstrap();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DECIMAL_RADIX = 10;
1 change: 1 addition & 0 deletions src/shared/libs/file-reader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TSVFileReader } from './tsv-file-reader.js';
Loading

0 comments on commit 10e5ed1

Please sign in to comment.