-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.ts
66 lines (61 loc) · 2.14 KB
/
list.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
import { readdir, statSync } from "fs";
import { blueBright, Chalk, cyanBright, redBright, yellowBright } from "chalk";
import { join } from "path";
import { directoryExists } from "./index";
import { gitignore, ignore } from "./gitignore";
import { DirectoryTree } from "./tree";
interface Options {
onlyDir?: boolean;
onlyFiles?: boolean;
createDirectoryTree?: boolean;
}
export class ListDirectories {
private readonly directory: string;
private ignores: Array<string> = [];
private options: Options;
constructor(directory: string, options: Options) {
this.directory = directory;
this.options = options;
this.executeLsCommand(this.directory);
}
private executeLsCommand(directory: string): void | null {
if (!directoryExists(directory, true)) {
console.log(redBright(`ERROR: ${directory} is not a directory`));
process.exit();
}
this.ignores = gitignore(this.directory);
if (this.options.createDirectoryTree) {
const tree = new DirectoryTree(this.directory, this.options.onlyDir);
return null;
}
readdir(directory, (err: any, content: Array<string>): void => {
if (err) {
console.log(redBright(`ERROR: ${err.msg}`));
process.exit();
}
content
.filter((contentItem: string): boolean => {
if (this.options.onlyDir) {
return directoryExists(join(directory, contentItem), true);
} else if (this.options.onlyFiles) {
return directoryExists(join(directory, contentItem), false);
}
return true;
})
.forEach((filename: string): void => {
try {
if (ignore(filename, directory, this.ignores)) {
const size: number = statSync(filename).size;
const isDir: boolean = directoryExists(
join(directory, filename),
true
);
const color: Chalk = isDir ? blueBright : yellowBright;
const sizeString: string = `size:${size} bytes`;
console.log(`${color(filename)} -> [${cyanBright(sizeString)}]`);
}
} catch (exception) {}
});
});
}
}