Skip to content

Commit

Permalink
feat(core-kernel): filesystem abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Aug 22, 2019
1 parent 85b6b60 commit 0f7e589
Show file tree
Hide file tree
Showing 15 changed files with 449 additions and 61 deletions.
9 changes: 9 additions & 0 deletions packages/core-kernel/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ export class Application extends Container implements Kernel.IApplication {
return this.resolve<Contracts.Kernel.IEventDispatcher>("event");
}

/**
* @readonly
* @type {Contracts.Kernel.IFilesystem}
* @memberof Application
*/
public get filesystem(): Contracts.Kernel.IFilesystem {
return this.resolve<Contracts.Kernel.IFilesystem>("filesystem");
}

/**
* @readonly
* @type {Contracts.Database.IDatabaseService}
Expand Down
4 changes: 3 additions & 1 deletion packages/core-kernel/src/bootstrap/app/load-factories.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CacheFactory } from "../../services/cache";
import { LoggerFactory } from "../../services/log";
import { FilesystemFactory } from "../../services/filesystem";
import { LoggerFactory } from "../../services/logger";
import { QueueFactory } from "../../services/queue";
import { AbstractBootstrapper } from "../bootstrapper";

Expand All @@ -13,6 +14,7 @@ export class LoadFactories extends AbstractBootstrapper {
* @memberof LoadFactories
*/
public async bootstrap(): Promise<void> {
this.app.bind("factoryFilesystem", new FilesystemFactory(this.app));
this.app.bind("factoryLogger", new LoggerFactory(this.app));
this.app.bind("factoryCache", new CacheFactory(this.app));
this.app.bind("factoryQueue", new QueueFactory(this.app));
Expand Down
20 changes: 18 additions & 2 deletions packages/core-kernel/src/bootstrap/app/load-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { JsonObject } from "type-fest";
import { ConfigFactory, ConfigRepository } from "../../config";
import { IConfigAdapter } from "../../contracts/core-kernel";
import { EventDispatcher } from "../../services/events";
import { LoggerFactory } from "../../services/log";
import { ConsoleLogger } from "../../services/log/adapters/console";
import { FilesystemFactory } from "../../services/filesystem";
import { LocalAdapter } from "../../services/filesystem/adapters/local";
import { LoggerFactory } from "../../services/logger";
import { ConsoleLogger } from "../../services/logger/adapters/console";
import { AbstractBootstrapper } from "../bootstrapper";

/**
Expand All @@ -16,13 +18,27 @@ export class LoadServices extends AbstractBootstrapper {
* @memberof LoadServices
*/
public async bootstrap(): Promise<void> {
await this.registerFilesystem();

this.registerEventDispatcher();

await this.registerLogger();

this.registerConfigLoader();
}

/**
* @private
* @returns {Promise<void>}
* @memberof LoadServices
*/
private async registerFilesystem(): Promise<void> {
this.app.bind(
"filesystem",
await this.app.resolve<FilesystemFactory>("factoryFilesystem").make(new LocalAdapter()),
);
}

/**
* @private
* @memberof LoadServices
Expand Down
6 changes: 6 additions & 0 deletions packages/core-kernel/src/contracts/core-kernel/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IPeerService } from "../core-p2p";
import { IConnection } from "../core-transaction-pool";
import { IContainer } from "./container";
import { IEventDispatcher } from "./event-dispatcher";
import { IFilesystem } from "./filesystem";
import { ILogger } from "./logger";

export interface IApplication extends IContainer {
Expand All @@ -18,6 +19,11 @@ export interface IApplication extends IContainer {
*/
readonly events: IEventDispatcher;

/**
* Get an instance of the application filesystem.
*/
readonly filesystem: IFilesystem;

/**
* Get an instance of the application database.
*/
Expand Down
123 changes: 123 additions & 0 deletions packages/core-kernel/src/contracts/core-kernel/filesystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { IApplication } from "./application";

export interface IFilesystem {
/**
* Create a new instance of the filesystem.
*
* @param {IApplication} app
* @returns {Promise<IFilesystem>}
* @memberof IFilesystem
*/
make(app: IApplication): Promise<IFilesystem>;

/**
* Determine if a file exists.
*
* @param {string} path
* @returns {Promise<boolean>}
* @memberof IFilesystem
*/
exists(path: string): Promise<boolean>;

/**
* Get the contents of a file.
*
* @param {string} path
* @returns {Promise<Buffer>}
* @memberof IFilesystem
*/
get(path: string): Promise<Buffer>;

/**
* Write the contents of a file.
*
* @param {string} path
* @param {string} contents
* @returns {Promise<boolean>}
* @memberof IFilesystem
*/
put(path: string, contents: string): Promise<boolean>;

/**
* Delete the file at a given path.
*
* @param {string} path
* @returns {Promise<boolean>}
* @memberof IFilesystem
*/
delete(path: string): Promise<boolean>;

/**
* Copy a file to a new location.
*
* @param {string} from
* @param {string} to
* @returns {Promise<boolean>}
* @memberof IFilesystem
*/
copy(from: string, to: string): Promise<boolean>;

/**
* Move a file to a new location.
*
* @param {string} from
* @param {string} to
* @returns {Promise<boolean>}
* @memberof IFilesystem
*/
move(from: string, to: string): Promise<boolean>;

/**
* Get the file size of a given file.
*
* @param {string} path
* @returns {Promise<number>}
* @memberof IFilesystem
*/
size(path: string): Promise<number>;

/**
* Get the file's last modification time.
*
* @param {string} path
* @returns {Promise<number>}
* @memberof IFilesystem
*/
lastModified(path: string): Promise<number>;

/**
* Get an array of all files in a directory.
*
* @param {string} directory
* @returns {Promise<string[]>}
* @memberof IFilesystem
*/
files(directory: string): Promise<string[]>;

/**
* Get all of the directories within a given directory.
*
* @param {string} directory
* @returns {Promise<string[]>}
* @memberof IFilesystem
*/
directories(directory: string): Promise<string[]>;

/**
* Create a directory.
*
* @param {*} path
* @returns {Promise<boolean>}
* @memberof IFilesystem
*/
makeDirectory(path): Promise<boolean>;

/**
* Recursively delete a directory.
*
* @param {string} directory
* @returns {Promise<boolean>}
* @memberof IFilesystem
*/
deleteDirectory(directory: string): Promise<boolean>;
}
1 change: 1 addition & 0 deletions packages/core-kernel/src/contracts/core-kernel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./cache";
export * from "./config";
export * from "./container";
export * from "./event-dispatcher";
export * from "./filesystem";
export * from "./logger";
Loading

0 comments on commit 0f7e589

Please sign in to comment.