/
fs.ts
179 lines (159 loc) · 4.85 KB
/
fs.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as fs from "fs";
import { promises as fsp } from "fs";
import { Minimatch } from "minimatch";
import { dirname, join } from "path";
/**
* Get the longest directory path common to all files.
*/
export function getCommonDirectory(files: readonly string[]): string {
if (!files.length) {
return "";
}
const roots = files.map((f) => f.split(/\\|\//));
if (roots.length === 1) {
return roots[0].slice(0, -1).join("/");
}
let i = 0;
while (new Set(roots.map((part) => part[i])).size === 1) {
i++;
}
return roots[0].slice(0, i).join("/");
}
/**
* Normalize the given path.
*
* @param path The path that should be normalized.
* @returns The normalized path.
*/
export function normalizePath(path: string) {
return path.replace(/\\/g, "/");
}
/**
* Load the given file and return its contents.
*
* @param file The path of the file to read.
* @returns The files contents.
*/
export function readFile(file: string): string {
const buffer = fs.readFileSync(file);
switch (buffer[0]) {
case 0xfe:
if (buffer[1] === 0xff) {
let i = 0;
while (i + 1 < buffer.length) {
const temp = buffer[i];
buffer[i] = buffer[i + 1];
buffer[i + 1] = temp;
i += 2;
}
return buffer.toString("ucs2", 2);
}
break;
case 0xff:
if (buffer[1] === 0xfe) {
return buffer.toString("ucs2", 2);
}
break;
case 0xef:
if (buffer[1] === 0xbb) {
return buffer.toString("utf8", 3);
}
}
return buffer.toString("utf8", 0);
}
/**
* Write a file to disc.
*
* If the containing directory does not exist it will be created.
*
* @param fileName The name of the file that should be written.
* @param data The contents of the file.
*/
export function writeFileSync(fileName: string, data: string) {
fs.mkdirSync(dirname(normalizePath(fileName)), { recursive: true });
fs.writeFileSync(normalizePath(fileName), data);
}
/**
* Write a file to disc.
*
* If the containing directory does not exist it will be created.
*
* @param fileName The name of the file that should be written.
* @param data The contents of the file.
*/
export async function writeFile(fileName: string, data: string) {
await fsp.mkdir(dirname(normalizePath(fileName)), {
recursive: true,
});
await fsp.writeFile(normalizePath(fileName), data);
}
/**
* Copy a file or directory recursively.
*/
export async function copy(src: string, dest: string): Promise<void> {
const stat = await fsp.stat(src);
if (stat.isDirectory()) {
const contained = await fsp.readdir(src);
await Promise.all(
contained.map((file) => copy(join(src, file), join(dest, file)))
);
} else if (stat.isFile()) {
await fsp.mkdir(dirname(dest), { recursive: true });
await fsp.copyFile(src, dest);
} else {
// Do nothing for FIFO, special devices.
}
}
export function copySync(src: string, dest: string): void {
const stat = fs.statSync(src);
if (stat.isDirectory()) {
const contained = fs.readdirSync(src);
contained.forEach((file) =>
copySync(join(src, file), join(dest, file))
);
} else if (stat.isFile()) {
fs.mkdirSync(dirname(dest), { recursive: true });
fs.copyFileSync(src, dest);
} else {
// Do nothing for FIFO, special devices.
}
}
/**
* Simpler version of `glob.sync` that only covers our use cases, always ignoring node_modules.
*/
export function glob(
pattern: string,
root: string,
options?: { includeDirectories?: boolean }
): string[] {
const result: string[] = [];
const mini = new Minimatch(normalizePath(pattern));
const dirs: string[][] = [normalizePath(root).split("/")];
do {
const dir = dirs.shift()!;
if (options?.includeDirectories && mini.match(dir.join("/"))) {
result.push(dir.join("/"));
}
for (const child of fs.readdirSync(dir.join("/"), {
withFileTypes: true,
})) {
if (child.isFile()) {
const childPath = [...dir, child.name].join("/");
if (mini.match(childPath)) {
result.push(childPath);
}
}
if (child.isDirectory() && child.name !== "node_modules") {
const childPath = dir.concat(child.name);
if (
mini.set.some((row) =>
mini.matchOne(childPath, row, /* partial */ true)
)
) {
dirs.push(childPath);
}
}
}
} while (dirs.length);
return result;
}