1 | import {promisify} from './lang/promise';
|
2 | import {writeFile} from 'fs';
|
3 |
|
4 | const writeFilePromise = promisify(writeFile);
|
5 |
|
6 | const DEFAULT_ENCODING = 'utf8';
|
7 |
|
8 | export class FileWriter {
|
9 |
|
10 | constructor(private readonly path: string,
|
11 | private append: boolean,
|
12 | private readonly encoding = DEFAULT_ENCODING) {
|
13 | }
|
14 |
|
15 | async write(string: string): Promise<void> {
|
16 | await writeFilePromise(this.path, string, this.getWriteOption());
|
17 | this.append = true;
|
18 | }
|
19 |
|
20 | private getWriteOption() {
|
21 | return {
|
22 | encoding: this.encoding,
|
23 | flag: this.append ? 'a' : 'w'
|
24 | };
|
25 | }
|
26 | }
|