├── .gitignore ├── Folder └── deepFolder │ ├── 1.js │ └── 2.js ├── tsconfig.json ├── directoryTree.txt ├── package.json ├── README.md └── app.ts /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /Folder/deepFolder/1.js: -------------------------------------------------------------------------------- 1 | //test 1 2 | -------------------------------------------------------------------------------- /Folder/deepFolder/2.js: -------------------------------------------------------------------------------- 1 | // test 2 2 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "moduleResolution": "node", 4 | "types": ["node"] 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /directoryTree.txt: -------------------------------------------------------------------------------- 1 | 📄—.gitignore 2 | 📄—app.ts 3 | 📂—Folder 4 | 📂—deepFolder 5 | 📄—1.js 6 | 📄—2.js 7 | 📄—package-lock.json 8 | 📄—package.json 9 | 📄—README.md 10 | 📄—tsconfig.json 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "directory-stuff", 3 | "version": "1.0.0", 4 | "description": "Scan Directory ===", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "devDependencies": { 13 | "@types/node": "^18.11.18" 14 | }, 15 | "dependencies": { 16 | "minimist": "^1.2.8" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Directory-Tree 2 | === 3 | Application let you draw hierarchy of a the current directory. 4 | ### Install 5 | ``` 6 | npm install -g ts-node typescript '@types/node' 7 | ``` 8 | ### Run 9 | ``` 10 | ts-node app.ts 11 | ``` 12 | ## Demo 13 | ``` 14 | 📄—app.ts 15 | 📂—Folder 16 | 📂—deepFolder 17 | 📄—1.js 18 | 📄—2.js 19 | 📄—package-lock.json 20 | 📄—package.json 21 | 📄—README.md 22 | 📄—tsconfig.json 23 | ``` 24 | 25 | ## ToDo: 26 | - [ ] Add possibility to specific the folder you want to draw its hierarchy ``. 27 | - [ ] Add some details of file (How much items contains, and its size kb, date/time created ). -------------------------------------------------------------------------------- /app.ts: -------------------------------------------------------------------------------- 1 | const foldersToIgnore = ["node_modules", ".git"]; 2 | import * as fs from 'fs'; 3 | import { promises as fsPromises } from 'fs'; 4 | import { join } from 'path'; 5 | const argv = require('minimist')(process.argv.slice(2)); 6 | const save = argv.save; 7 | 8 | /** 9 | * @param {string} path - path of directory 10 | * @return {Array>} - a multidimensional array contains [ [fileName, FileType], ...] 11 | */ 12 | function readDirectory(path): Array<[string, string]> { 13 | const files = fs.readdirSync(path); 14 | const result: Array<[string, string]> = []; 15 | for (const file of files) { 16 | const stat = fs.statSync(`${path}\\${file}`); 17 | if (stat.isFile()) { 18 | result.push([file, 'file']); 19 | } else if (stat.isDirectory()) { 20 | result.push([file, 'directory']); 21 | } 22 | } 23 | return result; 24 | } 25 | 26 | 27 | /** 28 | * @param {string} fileName - name of file 29 | * @param {number} fileDeep - how much deep the file/folder 30 | * @param {string} fileType - type of file 31 | * @return {string} - generated line with icon and name of file/folder 32 | */ 33 | async function drawLine(fileName: string, fileDeep: number, fileType: string) { 34 | let start: string; 35 | let space: string = " ".repeat(fileDeep); 36 | let line: string; 37 | 38 | if (space === "") line = "—"; 39 | else line = "—"; 40 | 41 | if (fileType === "file") start = "📄"; 42 | else if (fileType === "directory") start = "📂"; 43 | else start = "|"; 44 | 45 | return `${space + start + line + fileName}\n`; 46 | } 47 | 48 | /** 49 | * @param {string} fileName - name of file 50 | * @param {string} fileType - type of file 51 | * @param {number} fileDeep - how much deep the file/folder 52 | * @param {string} path - path of file 53 | * @param {string} DirectoryHierarchy - a recursed result from a recursion function buildDirectoryTree 54 | * @return {string} - generated a tree directory 55 | */ 56 | async function buildDirectoryTree(fileName: string, fileType: string, fileDeep: number, path: string, DirectoryHierarchy: string) { 57 | if (fileType === "file") { 58 | DirectoryHierarchy += await drawLine(fileName, fileDeep, fileType); 59 | } 60 | else if (fileType == "directory") { 61 | if (foldersToIgnore.includes(fileName)) { 62 | return DirectoryHierarchy; 63 | } 64 | let currentPath = `${path}\\${fileName}`; 65 | let currentDirectory = await readDirectory(currentPath); 66 | DirectoryHierarchy += await drawLine(fileName, fileDeep, fileType); 67 | for (let file of currentDirectory) { 68 | let [fileName, fileType] = file; 69 | DirectoryHierarchy = await buildDirectoryTree(fileName, fileType, fileDeep + 1, currentPath, DirectoryHierarchy); 70 | } 71 | } 72 | return DirectoryHierarchy; 73 | } 74 | 75 | // main 76 | (async () => { 77 | let path = __dirname; 78 | const fileDirectories: string[][] = await readDirectory(path); 79 | let DirectoryHierarchy: string = ""; 80 | let fileDeep = 0; 81 | for (let file of fileDirectories) { 82 | let [fileName, fileType] = file; 83 | DirectoryHierarchy = await buildDirectoryTree(fileName, fileType, fileDeep, path, DirectoryHierarchy); 84 | } 85 | let fileName = "directoryTree"; 86 | if (save === 'txt') { 87 | try { 88 | await fsPromises.writeFile(join(__dirname, fileName + ".txt"), DirectoryHierarchy); 89 | } catch (err) { 90 | console.error(err); 91 | } 92 | } else if (save === 'img') { 93 | console.log(save); 94 | } 95 | 96 | })() --------------------------------------------------------------------------------