-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.ts
96 lines (87 loc) · 3.15 KB
/
app.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
const foldersToIgnore = ["node_modules", ".git"];
import * as fs from 'fs';
import { promises as fsPromises } from 'fs';
import { join } from 'path';
const argv = require('minimist')(process.argv.slice(2));
const save = argv.save;
/**
* @param {string} path - path of directory
* @return {Array<Array<string>>} - a multidimensional array contains [ [fileName, FileType], ...]
*/
function readDirectory(path): Array<[string, string]> {
const files = fs.readdirSync(path);
const result: Array<[string, string]> = [];
for (const file of files) {
const stat = fs.statSync(`${path}\\${file}`);
if (stat.isFile()) {
result.push([file, 'file']);
} else if (stat.isDirectory()) {
result.push([file, 'directory']);
}
}
return result;
}
/**
* @param {string} fileName - name of file
* @param {number} fileDeep - how much deep the file/folder
* @param {string} fileType - type of file
* @return {string} - generated line with icon and name of file/folder
*/
async function drawLine(fileName: string, fileDeep: number, fileType: string) {
let start: string;
let space: string = " ".repeat(fileDeep);
let line: string;
if (space === "") line = "—";
else line = "—";
if (fileType === "file") start = "📄";
else if (fileType === "directory") start = "📂";
else start = "|";
return `${space + start + line + fileName}\n`;
}
/**
* @param {string} fileName - name of file
* @param {string} fileType - type of file
* @param {number} fileDeep - how much deep the file/folder
* @param {string} path - path of file
* @param {string} DirectoryHierarchy - a recursed result from a recursion function buildDirectoryTree
* @return {string} - generated a tree directory
*/
async function buildDirectoryTree(fileName: string, fileType: string, fileDeep: number, path: string, DirectoryHierarchy: string) {
if (fileType === "file") {
DirectoryHierarchy += await drawLine(fileName, fileDeep, fileType);
}
else if (fileType == "directory") {
if (foldersToIgnore.includes(fileName)) {
return DirectoryHierarchy;
}
let currentPath = `${path}\\${fileName}`;
let currentDirectory = await readDirectory(currentPath);
DirectoryHierarchy += await drawLine(fileName, fileDeep, fileType);
for (let file of currentDirectory) {
let [fileName, fileType] = file;
DirectoryHierarchy = await buildDirectoryTree(fileName, fileType, fileDeep + 1, currentPath, DirectoryHierarchy);
}
}
return DirectoryHierarchy;
}
// main
(async () => {
let path = __dirname;
const fileDirectories: string[][] = await readDirectory(path);
let DirectoryHierarchy: string = "";
let fileDeep = 0;
for (let file of fileDirectories) {
let [fileName, fileType] = file;
DirectoryHierarchy = await buildDirectoryTree(fileName, fileType, fileDeep, path, DirectoryHierarchy);
}
let fileName = "directoryTree";
if (save === 'txt') {
try {
await fsPromises.writeFile(join(__dirname, fileName + ".txt"), DirectoryHierarchy);
} catch (err) {
console.error(err);
}
} else if (save === 'img') {
console.log(save);
}
})()