UNPKG

645 BPlain TextView Raw
1import {promisify} from './lang/promise';
2import {writeFile} from 'fs';
3
4const writeFilePromise = promisify(writeFile);
5
6const DEFAULT_ENCODING = 'utf8';
7
8export 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}