UNPKG

961 BPlain TextView Raw
1import { mkdirp, remove } from "fs-promise";
2import { _, env } from "oly-core";
3import { join } from "path";
4
5/**
6 * Provide a safe place to store files
7 */
8export class WorkspaceProvider {
9
10 @env("WORKSPACE_DIRECTORY")
11 public directory: string = join(process.cwd(), "workspace");
12
13 @env("WORKSPACE_TMP")
14 public tmp: string = ".tmp";
15
16 /**
17 * Generate a filepath. Useful for temp file or test.
18 *
19 * @return Random filepath
20 */
21 public rand(): string {
22 return this.join(this.tmp, _.shortid());
23 }
24
25 /**
26 * Get absolute path of workspace file.
27 *
28 * @param entry
29 * @return {string}
30 */
31 public join(...entry: string[]): string {
32 return join(this.directory, ...entry);
33 }
34
35 protected async onStart(): Promise<void> {
36 await mkdirp(this.directory);
37 await remove(this.join(this.tmp));
38 await mkdirp(this.join(this.tmp));
39 }
40
41 protected async onStop(): Promise<void> {
42 await remove(this.join(this.tmp));
43 }
44}