UNPKG

2.3 kBPlain TextView Raw
1import { copy, exists, mkdirp, move, readFile, remove, writeFile } from "fs-extra";
2import { inject, Logger } from "oly-core";
3
4/**
5 *
6 */
7export class FileService {
8
9 @inject protected logger: Logger;
10
11 /**
12 * Check if file exists.
13 *
14 * @param filepath Absolute path
15 */
16 public async exists(filepath: string): Promise<boolean> {
17 this.logger.debug(`exists ${filepath}`);
18 return await new Promise<boolean>((resolve) => exists(filepath, resolve));
19 }
20
21 /**
22 * Read a file.
23 *
24 * @param filepath Absolute path
25 * @param options Encoding and flag
26 */
27 public async read(filepath: string, options: { encoding: "UTF-8"; flag?: string; }): Promise<string>;
28 public async read(filepath: string, options: { encoding: string; flag?: string; }): Promise<string | Buffer> {
29 this.logger.debug(`read ${filepath}`);
30 return await readFile(filepath, options);
31 }
32
33 /**
34 * Write a file.
35 *
36 * @param filepath Absolute path
37 * @param data Data
38 * @param options Encode, mode and flag
39 */
40 public async write(filepath: string,
41 data: string | Buffer,
42 options: { encoding?: string; mode?: number; flag?: string; } = {}): Promise<void> {
43 this.logger.debug(`write ${filepath}`);
44 await writeFile(filepath, data);
45 }
46
47 /**
48 * Move a file.
49 *
50 * @param filepath Source
51 * @param destination Destination
52 */
53 public async move(filepath: string, destination: string) {
54 this.logger.debug(`move ${filepath} to ${destination}`);
55 return await move(filepath, destination);
56 }
57
58 /**
59 * Copy a file.
60 *
61 * @param filepath Source
62 * @param destination Destination
63 */
64 public async copy(filepath: string, destination: string) {
65 this.logger.debug(`copy ${filepath} to ${destination}`);
66 return await copy(filepath, destination);
67 }
68
69 /**
70 * Remove directory/file.
71 *
72 * @param filepath Absolute path
73 */
74 public async remove(filepath: string) {
75 this.logger.debug(`remove ${filepath}`);
76 return await remove(filepath);
77 }
78
79 /**
80 * Ensure directory.
81 *
82 * @param filepath Absolute path
83 */
84 public async mkdirp(filepath: string) {
85 this.logger.debug(`mkdirp ${filepath}`);
86 return await mkdirp(filepath);
87 }
88}
89
\No newline at end of file