Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
55 lines (47 loc) · 1.37 KB
/
Copy pathutils.ts
File metadata and controls
55 lines (47 loc) · 1.37 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
import * as childProcess from "child_process";
import * as fs from "fs";
import * as path from "path";
export function exists(filePath: string): boolean {
try {
return fs.existsSync(filePath);
} catch (ignore) {
return false;
}
}
export function readFile(filePath: string): string {
const _filePath = resolveFilePath(filePath);
return fs.readFileSync(_filePath, "utf8");
}
export const resolveFilePath = (filePath: string): string => {
if (filePath.length === 0) {
return filePath;
}
if (filePath[0] === "~") {
return path.join(process.env.HOME!, filePath.slice(1));
}
return filePath;
}
export function readStdin(): Promise<string> {
return new Promise<string>((resolve): void => {
let content = "";
process.stdin.resume();
process.stdin.on("data", (buf) => {
content += buf.toString("utf8");
});
process.stdin.on("end", () => resolve(content));
});
}
export function writeFile(filePath: string, content: string): void {
return fs.writeFileSync(filePath, content);
}
export function appendFileSync(filePath: string, content: string): void {
return fs.appendFileSync(filePath, content);
}
export function execute(cmd: string): string {
return childProcess.execSync(cmd).toString("utf8");
}
export function removeFile(filePath: string): void {
if (fs.existsSync(filePath)) {
return fs.unlinkSync(filePath);
}
}