@fluent/console
Advanced tools
Comparing version 1.0.1 to 1.0.5
import { ApplicationBuilder as ApplicationBuilderBase } from '@fluent/core'; | ||
import { CommandCollection } from './command-collection'; | ||
import { CommandDescriptor } from './command-descriptor'; | ||
import { CommandHandler } from './command-handler'; | ||
import { CommandContext } from './command-context'; | ||
export interface ApplicationBuilder extends ApplicationBuilderBase { | ||
readonly commandCollection: CommandCollection; | ||
} | ||
export declare class ApplicationBuilder extends ApplicationBuilderBase implements ApplicationBuilder { | ||
readonly commandCollection: CommandCollection; | ||
defaultCommand: CommandDescriptor; | ||
useCommand(signature: string | string[], handler: new (...args: any[]) => CommandHandler): void; | ||
use(signature: string | string[], inlineHandler: (commandContext: CommandContext) => void | Promise<void>): void; | ||
} |
@@ -1,2 +0,1 @@ | ||
import { CommandHandler } from './command-handler'; | ||
import { Application as ApplicationBase } from '@fluent/core'; | ||
@@ -6,11 +5,14 @@ import { InputCommand } from './input-command'; | ||
import { Logger } from '@fluent/logging'; | ||
export interface Application extends ApplicationBase<ApplicationBuilder> { | ||
useCommand(signature: string, handler: new () => CommandHandler): void; | ||
} | ||
export declare class Application extends ApplicationBase<ApplicationBuilder> implements Application { | ||
import { Configuration } from '@fluent/core'; | ||
export declare class Application extends ApplicationBase<ApplicationBuilder> { | ||
readonly inputCommand: InputCommand; | ||
readonly applicationBuilder: ApplicationBuilder; | ||
logger: Logger; | ||
configuration: Configuration; | ||
private preventRun; | ||
constructor(inputCommand?: InputCommand); | ||
runAsync(): Promise<number>; | ||
private executeCommand(serviceProvider, commandDescriptor); | ||
private readonly configurationPaths; | ||
private loadConfiguration(); | ||
} |
@@ -11,2 +11,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Path = require("path"); | ||
const FileSystem = require("fs"); | ||
const command_descriptor_1 = require("./command-descriptor"); | ||
@@ -21,11 +23,42 @@ const core_1 = require("@fluent/core"); | ||
const di_1 = require("@fluent/di"); | ||
const core_2 = require("@fluent/core"); | ||
class Application extends core_1.Application { | ||
constructor(inputCommand = new input_command_1.InputCommand(...process.argv)) { | ||
super(inputCommand.options.get('environment') || inputCommand.options.get('env')); | ||
super(); | ||
this.inputCommand = inputCommand; | ||
this.applicationBuilder = new application_builder_1.ApplicationBuilder(); | ||
const commandCollection = this.applicationBuilder.commandCollection; | ||
this.preventRun = false; | ||
if (inputCommand.options.has('rootPath')) { | ||
this.environment.rootPath = inputCommand.options.get('rootPath'); | ||
} | ||
const environmentName = inputCommand.options.get('environment') || inputCommand.options.get('env'); | ||
if (environmentName) { | ||
this.environment.name = environmentName; | ||
} | ||
this.logger = new logging_1.Logger(this.environment); | ||
this.serviceCollection.addSingleton(logging_1.Logger, this.logger); | ||
commandCollection.add(new command_descriptor_1.CommandDescriptor('help', help_command_1.HelpCommand, new help_command_1.HelpCommand(commandCollection))); | ||
this.logger.verbose(`Environment '${this.environment.name}' selected.`); | ||
try { | ||
this.importer.scanDirectory(this.environment.rootPath); | ||
this.importer.importAll(); | ||
this.logger.verbose(`${this.importer.filePaths.size} file(s) imported: \n\n\t` + Array.from(this.importer.filePaths).join('\n\t') + '\n'); | ||
} | ||
catch (error) { | ||
this.logger.error('Auto import failed: ' + error.message, error.stack); | ||
this.preventRun = true; | ||
return; | ||
} | ||
try { | ||
this.configuration = this.loadConfiguration(); | ||
this.serviceCollection.addSingleton(core_2.Configuration, this.configuration); | ||
} | ||
catch (error) { | ||
this.logger.error('Loading configuration failed: ' + error.message, error.stack); | ||
this.preventRun = true; | ||
return; | ||
} | ||
const commandCollection = this.applicationBuilder.commandCollection; | ||
const helpCommand = new command_descriptor_1.CommandDescriptor('help', help_command_1.HelpCommand, new help_command_1.HelpCommand(commandCollection)); | ||
this.applicationBuilder.defaultCommand = helpCommand; | ||
commandCollection.add(helpCommand); | ||
static_command_collection_1.staticCommandCollection.forEach(commandDescriptor => commandCollection.add(commandDescriptor)); | ||
@@ -36,2 +69,5 @@ di_1.staticServiceCollection.forEach(serviceDescriptor => this.serviceCollection.add(serviceDescriptor)); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.preventRun) { | ||
return 0; | ||
} | ||
let serviceProvider; | ||
@@ -42,31 +78,20 @@ try { | ||
catch (error) { | ||
this.logger.error('ServiceProvider building failed.', error.message, error.stack); | ||
this.logger.error('ServiceProvider building failed: ' + error.message, error.stack); | ||
return 0; | ||
} | ||
let executed = false; | ||
let commandExecuted = false; | ||
const commandCollection = this.applicationBuilder.commandCollection; | ||
for (const commandDescriptor of commandCollection) { | ||
if (this.inputCommand.match(commandDescriptor)) { | ||
const iteratable = Array.from(commandDescriptor.options).map(option => { | ||
return [option, this.inputCommand.options.get(option) || false]; | ||
}); | ||
const commandContext = new command_context_1.CommandContext(); | ||
commandContext.options = new Map(iteratable); | ||
let instance = commandDescriptor.instance; | ||
try { | ||
if (!instance) { | ||
instance = serviceProvider.serviceResolver.resolveConstructor(commandDescriptor.handler); | ||
} | ||
yield instance.handle(commandContext); | ||
} | ||
catch (error) { | ||
this.logger.error('Command execution failed.', error.message, error.stack); | ||
} | ||
executed = true; | ||
commandExecuted = commandExecuted = yield this.executeCommand(serviceProvider, commandDescriptor); | ||
break; | ||
} | ||
} | ||
if (!executed) { | ||
const defaultCommand = this.applicationBuilder.defaultCommand; | ||
if (!commandExecuted && defaultCommand) { | ||
commandExecuted = yield this.executeCommand(serviceProvider, defaultCommand); | ||
} | ||
if (!commandExecuted) { | ||
const error = new Error(`Command signature '${Array.from(this.inputCommand.arguments).join(' ')}' not registered.`); | ||
this.logger.error('Command do not executed.', error.message, error.stack); | ||
this.logger.error('Command do not executed:' + error.message, error.stack); | ||
} | ||
@@ -76,3 +101,44 @@ return 1; | ||
} | ||
executeCommand(serviceProvider, commandDescriptor) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const iteratable = Array.from(commandDescriptor.options).map(option => { | ||
return [option, this.inputCommand.options.get(option) || false]; | ||
}); | ||
const commandContext = new command_context_1.CommandContext(); | ||
commandContext.options = new Map(iteratable); | ||
let instance = commandDescriptor.instance; | ||
try { | ||
if (!instance) { | ||
instance = serviceProvider.serviceResolver.resolveConstructor(commandDescriptor.handler); | ||
} | ||
yield instance.handle(commandContext); | ||
} | ||
catch (error) { | ||
this.logger.error('Command execution failed: ' + error.message, error.stack); | ||
} | ||
return true; | ||
}); | ||
} | ||
get configurationPaths() { | ||
const paths = []; | ||
if (this.environment.isProduction) { | ||
paths.push(Path.join(this.environment.rootPath, `configuration.json`)); | ||
} | ||
paths.push(Path.join(this.environment.rootPath, `configuration.${this.environment.name}.json`)); | ||
return paths; | ||
} | ||
loadConfiguration() { | ||
let configurationPaths = this.configurationPaths; | ||
for (const configurationPath of configurationPaths) { | ||
const fileStat = FileSystem.statSync(configurationPath); | ||
if (fileStat.isFile()) { | ||
const configuration = core_2.Configuration.readFromFile(configurationPath); | ||
this.logger.verbose(`Configuration file '${configurationPath}' loaded.`); | ||
return configuration; | ||
} | ||
} | ||
this.logger.warn(`No file of ['${configurationPaths.join('\', \'')}'] doesn't exists.`); | ||
return core_2.Configuration.convertFromObject({}); | ||
} | ||
} | ||
exports.Application = Application; |
{ | ||
"name": "@fluent/console", | ||
"version": "1.0.1", | ||
"version": "1.0.5", | ||
"description": "Fluent - console component", | ||
@@ -12,7 +12,8 @@ "main": "build/index.js", | ||
"keywords": [ | ||
"fluent", "console" | ||
"fluent", | ||
"console" | ||
], | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "https://github.com/emelnychenko/fluent" | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/emelnychenko/fluent" | ||
}, | ||
@@ -25,5 +26,5 @@ "license": "MIT", | ||
"peerDependencies": { | ||
"@fluent/core": "1.0.*", | ||
"@fluent/di": "1.0.*", | ||
"@fluent/logging": "1.0.*" | ||
"@fluent/core": "^1.0.5", | ||
"@fluent/di": "^1.0.5", | ||
"@fluent/logging": "^1.0.5" | ||
}, | ||
@@ -30,0 +31,0 @@ "devDependencies": { |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
17446
391
1