import { mkdirp, remove } from "fs-promise"; import { _, env } from "oly-core"; import { join } from "path"; /** * Provide a safe place to store files */ export class WorkspaceProvider { @env("WORKSPACE_DIRECTORY") public directory: string = join(process.cwd(), "workspace"); @env("WORKSPACE_TMP") public tmp: string = ".tmp"; /** * Generate a filepath. Useful for temp file or test. * * @return Random filepath */ public rand(): string { return this.join(this.tmp, _.shortid()); } /** * Get absolute path of workspace file. * * @param entry * @return {string} */ public join(...entry: string[]): string { return join(this.directory, ...entry); } protected async onStart(): Promise { await mkdirp(this.directory); await remove(this.join(this.tmp)); await mkdirp(this.join(this.tmp)); } protected async onStop(): Promise { await remove(this.join(this.tmp)); } }