forked from orval-labs/orval
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.ts
More file actions
74 lines (65 loc) 路 1.79 KB
/
Copy pathfile.ts
File metadata and controls
74 lines (65 loc) 路 1.79 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
import fs from 'node:fs';
import path from 'node:path';
import { glob } from 'tinyglobby';
import { isDirectory } from './assertion';
export function getFileInfo(
target = '',
{
backupFilename = 'filename',
extension = '.ts',
}: { backupFilename?: string; extension?: string } = {},
) {
const isDir = isDirectory(target);
const filePath = isDir
? path.join(target, backupFilename + extension)
: target;
const pathWithoutExtension = filePath.replace(/\.[^/.]+$/, '');
const dir = path.dirname(filePath);
const filename = path.basename(
filePath,
extension.startsWith('.') ? extension : `.${extension}`,
);
return {
path: filePath,
pathWithoutExtension,
extension,
isDirectory: isDir,
dirname: dir,
filename,
};
}
export async function removeFilesAndEmptyFolders(
patterns: string[],
dir: string,
) {
const files = await glob(patterns, {
cwd: dir,
absolute: true,
});
// Remove files
await Promise.all(files.map((file) => fs.promises.unlink(file)));
// Find and remove empty directories
const directories = await glob(['**/*'], {
cwd: dir,
absolute: true,
onlyDirectories: true,
});
// Sort directories by depth (deepest first) to ensure we can remove nested empty folders
const sortedDirectories = directories.toSorted((a, b) => {
const depthA = a.split('/').length;
const depthB = b.split('/').length;
return depthB - depthA;
});
// Remove empty directories
for (const directory of sortedDirectories) {
try {
const contents = await fs.promises.readdir(directory);
if (contents.length === 0) {
await fs.promises.rmdir(directory);
}
} catch {
// Directory might have been removed already or doesn't exist
// Continue with next directory
}
}
}