Advertisement
And1

Tree

Dec 15th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. const fs = require("fs");
  4. const path = require("path");
  5. let argvLgt = process.argv.length;
  6. let isKeyD = true;
  7. let isKeyO = [false, ""];
  8. for (let i = 0; i < argvLgt || (isKeyD == false && isKeyO == false); i++){
  9.     if (process.argv[i] == "-o") {
  10.         isKeyO[0] = true;
  11.         isKeyO[1] = process.argv[i + 1];
  12.         if (isKeyO[1] == undefined) {
  13.             console.error(new Error("Missing argument to -o option"));
  14.             return
  15.         }
  16.         fs.writeFileSync(isKeyO[1], "");
  17.     }
  18.     if (process.argv[i] == "-d") isKeyD = true;
  19. }
  20. //tree получает путь к файлу, префикс, если файл/папка вложенные
  21. //и массив счетчиков файлов/папок
  22. const tree = (directory, pref, counts) => {
  23.     if (!isKeyD){
  24.         let filePaths = fs.readdirSync(directory);
  25.         //запоминание элементов в папке (директтории)
  26.         filePaths.forEach((filePath, index) => {
  27.             if (filePath.charAt(0) == ".") {
  28.                     return
  29.             };
  30.             //если файл - скрытый - пропускаем
  31.  
  32.             let absolute = path.join(directory, filePath);
  33.             //путь к текущей папке от "корня"
  34.             let isFile = fs.lstatSync(absolute).isFile();
  35.             //проверка, является ли объект файлом (значение true/falce)
  36.             if (isFile)
  37.                 counts["files"]++;
  38.             else
  39.                 counts["directories"]++;
  40.  
  41.             // если файл явлется последним в директории, выводится с символом
  42.             //последнего файла
  43.             if (index == filePaths.length - 1) {
  44.                 if (!isKeyO[0])
  45.                     console.log(`${pref}└── ${filePath}`);
  46.                 else fs.appendFileSync(isKeyO[1], `\n${pref}└── ${filePath}`)
  47.                 //&{...} - unquote (аналогия в скиме, `(,funcName))
  48.                 if (!(isFile)) {
  49.                     tree(absolute, `${pref}   `,counts);
  50.                 }
  51.             } else {
  52.                 if (!isKeyO[0])
  53.                     console.log(`${pref}├── ${filePath}`);
  54.                 else fs.appendFileSync(isKeyO[1], `\n${pref}├── ${filePath}`);
  55.                 if (!(isFile)) {
  56.                     tree(absolute, `${pref}│   `, counts);
  57.                 }
  58.             }
  59.         });
  60.     } else {
  61.         let filePaths = fs.readdirSync(directory);
  62.         filePaths.forEach((filePath, index) => {
  63.             let absolute = path.join(directory, filePath);
  64.             let isDir = fs.lstatSync(absolute).isDirectory();
  65.  
  66.             if (filePath.charAt(0) == "." || !(isDir)) {
  67.                 return;
  68.             }
  69.             counts["directories"]++;
  70.        
  71.             if (index == filePaths.length - 1) {
  72.                 if (!isKeyO[0])
  73.                     console.log(`${pref}└── ${filePath}`);
  74.                 else fs.appendFileSync(isKeyO[1], `\n${pref}└── ${filePath}`);
  75.                 tree(absolute, `${pref}   `, counts);
  76.             } else {
  77.                 if (!isKeyO[0]){
  78.                     console.log(`${pref}├── ${filePath}`);
  79.                 } else fs.appendFileSync(isKeyO[1], `\n${pref}├── ${filePath}`)
  80.                 tree(absolute, `${pref}│   `, counts);
  81.             }
  82.         });
  83.     }
  84. };
  85.  
  86. let directory = process.argv[2] || ".";
  87. const counts = {
  88.     directories: 0,
  89.     files: 0
  90. };
  91. if (!isKeyO[0]){
  92. console.log(directory);
  93. } else fs.appendFileSync(isKeyO[1], directory);
  94.  
  95. tree(directory, "", counts);
  96. if (!isKeyD){
  97.     if (!isKeyO[0])
  98.         console.log(`\n ${counts.directories} directories, ${counts.files} files`);
  99.     else
  100.         fs.appendFileSync(isKeyO[1], `\n ${counts.directories} directories, ${counts.files} files`);
  101. }
  102. else if (!isKeyO[0])
  103.         console.log(`\n ${counts.directories} directories`);
  104.     else fs.appendFileSync (isKeyO[1], `\n ${counts.directories} directories`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement