Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Модуль 1 «Введение в Node.js. CLI» #1

Merged
merged 15 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mocks/mock-data.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Кристина Виновен или нет? Какая разница. Исход один. Сверхспособности не помогут. 2022-04-06T08:45:40.283Z institute-book.jpg Sell 1080 Книги;Журналы Пьер Безухов [email protected] awesome-avatar.jpg
Зелёная миля Как похудеть и не сойти с ума? А как вернуть вес обратно? Необычная история противостояния. 2022-04-11T08:45:40.284Z book.jpg Buy 888 Посуда;Разное;Диски Роланд Дискейн [email protected] photo.jpg
Тёмная башня Прими звонок и превратись в зомби. Рассказ о зомбировании через средства связи. 2022-04-05T08:45:40.284Z book.jpg Buy 780 Книги;Журналы;Посуда;Разное;Диски Олег Сидоров [email protected] photo.jpg
Долгая прогулка Идти или остановиться? Вопрос без правильного ответа. Рассказ о долгой и смертельной прогулки. 2022-04-06T08:45:40.284Z book.jpg Sell 1610 Книги Иван Иванов [email protected] awesome-avatar.jpg
40 changes: 40 additions & 0 deletions src/cli/cli-application.ts
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);
}
}
19 changes: 19 additions & 0 deletions src/cli/command-parser.ts
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;
}
}
4 changes: 4 additions & 0 deletions src/cli/commands/command.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Command {
getName(): string;
execute(...parameters: string[]): void;
}
20 changes: 20 additions & 0 deletions src/cli/commands/help.command.ts
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> # генерирует произвольное количество тестовых данных
`);
}
}
26 changes: 26 additions & 0 deletions src/cli/commands/import.command.ts
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}`);
}
}
}
50 changes: 50 additions & 0 deletions src/cli/commands/version.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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);
}
}
}
}
5 changes: 5 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './cli-application.js';
export * from './command-parser.js';
export * from './commands/help.command.js';
export * from './commands/version.command.js';
export * 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, 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();
3 changes: 3 additions & 0 deletions src/shared/libs/file-reader/file-reader.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface FileReader {
read(): void;
}
2 changes: 2 additions & 0 deletions src/shared/libs/file-reader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './file-reader.interface.js';
export * from './tsv-file-reader.js';
37 changes: 37 additions & 0 deletions src/shared/libs/file-reader/tsv-file-reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { FileReader } from './file-reader.interface.js';
import { readFileSync } from 'node:fs';
import { Offer, OfferType } from '../../types/index.js';

export class TSVFileReader implements FileReader {
private rawData = '';

constructor(
private readonly filename: string
) {}

public read(): void {
this.rawData = readFileSync(this.filename, { encoding: 'utf-8' });
}

public toArray(): Offer[] {
if (!this.rawData) {
throw new Error('File was not read');
}

return this.rawData
.split('\n')
.filter((row) => row.trim().length > 0)
.map((line) => line.split('\t'))
.map(([title, description, createdDate, image, type, price, categories, firstname, lastname, email, avatarPath]) => ({
title,
description,
postDate: new Date(createdDate),
image,
type: OfferType[type as 'Buy' | 'Sell'],
categories: categories.split(';')
.map((name) => ({name})),
price: Number.parseInt(price, 10),
user: { email, firstname, lastname, avatarPath },
}));
}
}
3 changes: 3 additions & 0 deletions src/shared/types/category.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Category = {
name: string;
}
4 changes: 4 additions & 0 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './category.type.js';
export * from './offer-type.enum.js';
export * from './user.type.js';
export * from './offer.type.js';
4 changes: 4 additions & 0 deletions src/shared/types/offer-type.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum OfferType {
Buy = 'Buy',
Sell = 'Sell',
}
14 changes: 14 additions & 0 deletions src/shared/types/offer.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { OfferType } from './offer-type.enum.js';
import { Category } from './category.type.js';
import { User } from './user.type.js';

export type Offer = {
title: string;
description: string;
postDate: Date;
image: string;
type: OfferType
price: number;
categories: Category[];
user: User;
}
6 changes: 6 additions & 0 deletions src/shared/types/user.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type User = {
email: string;
avatarPath: string;
firstname: string;
lastname: string;
}
Loading