-
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 artsamoilov/module1-task2
- Loading branch information
Showing
24 changed files
with
400 additions
and
21 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 @@ | ||
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 |
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
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,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); | ||
} | ||
} |
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,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('# генерирует произвольное количество тестовых данных')} | ||
`); | ||
} | ||
} |
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,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}`); | ||
} | ||
} | ||
} |
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 ICommand { | ||
getName(): string; | ||
execute(...parameters: string[]): Promise<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,3 @@ | ||
export interface IPackageJSONConfig { | ||
version: string; | ||
} |
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 @@ | ||
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; | ||
}; |
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 { 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); | ||
} | ||
} | ||
} | ||
} |
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 { 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'; |
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'; | ||
|
||
const bootstrap = async () => { | ||
const cliApplication = new CLIApplication(); | ||
cliApplication.registerCommands([ | ||
new HelpCommand(), | ||
new VersionCommand(), | ||
new ImportCommand(), | ||
]); | ||
|
||
await cliApplication.processCommand(process.argv); | ||
}; | ||
|
||
await 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 @@ | ||
export const DECIMAL_RADIX = 10; |
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 @@ | ||
export { TSVFileReader } from './tsv-file-reader.js'; |
Oops, something went wrong.