-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (46 loc) · 1.39 KB
/
index.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
import { redBright } from "chalk";
import { existsSync, statSync } from "fs";
import { dirname } from "path";
import { argv, cwd } from "process";
import { ArgumentParser, ArgumentParserResult } from "./arguments";
import { ListDirectories } from "./list";
export const directoryExists = (
directory: string,
isDirectory: boolean
): boolean => {
try {
const exists: boolean = existsSync(directory);
const isDir: boolean = isDirectory
? statSync(directory).isDirectory()
: true;
return exists && isDir;
} catch (exception) {
return false;
}
};
const validateDirectory = (
results: ArgumentParserResult
): ArgumentParserResult => {
const directory = results.command;
if (directory == null || directory == ".") {
results.command = cwd();
return results;
} else if (directory == "..") {
results.command = dirname(cwd());
}
const exists = directoryExists(results.command, true);
if (!exists) {
console.log(redBright(`${results.command} does not exist`));
process.exit();
}
return results;
};
const argumentParser: ArgumentParserResult = new ArgumentParser(
argv.slice(2)
).createParserResults();
const valid = validateDirectory(argumentParser);
const ls = new ListDirectories(valid.command, {
onlyDir: valid.flags.includes("only-dirs"),
onlyFiles: valid.flags.includes("only-files"),
createDirectoryTree: valid.flags.includes("tree"),
});