-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
80 lines (68 loc) · 2.16 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import fs from 'fs/promises';
import { dirname } from 'path';
import normalize from '@/normalize';
import notExists from '@/exists/not';
import makeDir from '@/makeDir';
/**
* 类型:append 为 false,overwrite 为 true (append is false, overwrite is true)
*/
type TWriteAppendFalseOverwriteTrue = {
append?: false;
overwrite?: true;
};
/**
* 类型:append 为 true,overwrite 为 false (append is true, overwrite is false)
*/
type TWriteAppendTrueOverwriteFalse = {
append?: true;
overwrite?: false;
};
/**
* 类型:编码选项 (encoding options)
*/
type TWriteEncodingOption = {
encoding?: BufferEncoding;
};
/**
* 类型:写入选项 (write options)
*/
type TWriteOptions = (TWriteAppendFalseOverwriteTrue | TWriteAppendTrueOverwriteFalse) & TWriteEncodingOption;
/**
* 抛出错误 (throw error)
*/
const throwErr = () => {
throw new Error('The file already exists and does not overwrite or append, so it cannot be written.');
};
/**
* 将内容写入指定的文件 (write content to the specified file)
* @param {string} filePath 文件路径 (file path)
* @param {string} content 要写入的内容 (content to write)
* @param {TWriteOptions} options 写入选项 (write options)
* @returns {Promise<boolean>} 如果写入成功,则返回 `true`;否则返回 `false (if the write is successful, `true` is returned; otherwise `false` is returned)
*/
const write = async (filePath: string, content: string, options: TWriteOptions = { overwrite: true, encoding: 'utf8' }): Promise<boolean> => {
const { append = false, overwrite = true, encoding = 'utf8' } = options;
try {
const absPath = normalize(filePath);
const dirPath = dirname(absPath);
await makeDir(dirPath);
if ((await notExists(absPath)) || overwrite) {
await fs.writeFile(absPath, content, { encoding });
} else if (append) {
await fs.appendFile(absPath, content, { encoding });
} else {
throwErr();
}
return true;
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'EEXIST') {
if (append || overwrite) {
return true;
} else {
throwErr();
}
}
throw err;
}
};
export default write;