-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfs.ts
More file actions
80 lines (75 loc) · 2.57 KB
/
fs.ts
File metadata and controls
80 lines (75 loc) · 2.57 KB
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
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { promises as fs, PathLike, ObjectEncodingOptions, Mode, OpenMode } from 'fs';
import { errorMessage, isNotFoundError } from './errors';
/**
* forceRemove forcibly removes a file or directory (recursively). If the file
* or directory does not exist, it does nothing. This is functionally equivalent
* to fs.rm, but avoids the need to handle errors for when the target file or
* directory does not exist.
*
* @param pth Path to the file or directory to remove.
*/
export async function forceRemove(pth: PathLike): Promise<void> {
try {
await fs.rm(pth, { force: true, recursive: true });
} catch (err: unknown) {
if (!isNotFoundError(err)) {
const msg = errorMessage(err);
throw new Error(`Failed to remove "${pth}": ${msg}`);
}
}
}
/**
* isEmptyDir returns true if the given directory does not exist, or exists but
* contains no files. It also returns true if the current user does not have
* permission to read the directory, since it is effectively empty from the
* viewpoint of the caller.
*
* @param dir Path to a directory.
*/
export async function isEmptyDir(dir: PathLike): Promise<boolean> {
try {
const files = await fs.readdir(dir);
return files.length <= 0;
} catch {
return true;
}
}
/**
* writeSecureFile writes a file to disk with 0600 permissions and locks the
* file during writing.
*
* @param outputPath Path in which to create the secure file.
* @param data Data to write to file.
* @param options additional options to pass to writeFile. The default options
* are permissions of 0600, write-exclusive, and flush-on-success.
*
* @returns Path to written file.
*/
export async function writeSecureFile<T extends PathLike>(
outputPath: T,
data: string | Buffer,
options?: ObjectEncodingOptions & {
mode?: Mode;
flag?: OpenMode;
flush?: boolean;
},
): Promise<T> {
const opts = Object.assign({}, { mode: 0o600, flag: 'wx', flush: true }, options);
await fs.writeFile(outputPath, data, opts);
return outputPath;
}