Advertisement
And1

Untitled

Dec 26th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. 'use strict'
  2.  
  3. const fs = require("fs");
  4.  
  5. function CounterOfWord(string) {
  6. return (string.split(/\s/).filter((s) => s != '').length);
  7. };
  8.  
  9. function CounterOfLines(string) {
  10. return (string.split('\n').length - 1);
  11. }
  12. const EnteredString = fs.readFileSync(process.argv[2], "utf8");
  13. var c = false,
  14. m = false,
  15. w = false,
  16. l = false;
  17.  
  18. let result = '';
  19.  
  20. try {
  21. if (process.argv.length == 3) {
  22. let stats = fs.statSync(process.argv[2]);
  23. //result += ` ${stats["size"]}`;
  24. result += ` ${CounterOfWord(EnteredString) + 1}`;
  25. result += ` ${CounterOfLines(EnteredString)}`;
  26. result += ` ${EnteredString.length}`;
  27. } else {
  28. var j = 0;
  29. while (j < process.argv.length) {
  30. if (process.argv[j] == "-c")
  31. c = true;
  32. if (process.argv[j] == "-m")
  33. m = true;
  34. if (process.argv[j] == "-w")
  35. w = true;
  36. if (process.argv[j] == "-l")
  37. l = true;
  38. j++;
  39. }
  40. if (c == true) {
  41. let stats = fs.statSync(process.argv[process.argv.length - 1]);
  42. result += stats["size"];
  43. }
  44. if (m == true) {
  45. result += ` ${EnteredString.length}`;
  46. }
  47. if (w == true) {
  48. result += ` ${CounterOfWord(EnteredString)}`;
  49. }
  50. if (l == true) {
  51. result += ` ${CounterOfLines(EnteredString)}`;
  52. }
  53. }
  54. } catch (e) {
  55. console.error("WRONG INPUTS");
  56. }
  57.  
  58. result += ` ${process.argv[2]}`;
  59.  
  60. console.log(result);
  61.  
  62. ///////////////////////////////////////////////////////
  63. ///////////////////////////////////////////////
  64.  
  65. 'use strict'
  66. const fs = require("fs");
  67. const path = require("path");
  68.  
  69. function tree(directory, prefix, counts_objects){
  70. if (!command_only_directory){
  71. let file_paths = fs.readdirSync(directory);
  72. file_paths.forEach((file_path, index) => {
  73. if (file_path.charAt(0) == ".") {
  74. return;
  75. }
  76. let absolute_path = path.join(directory, file_path);
  77. let is_file = fs.lstatSync(absolute_path).isFile();
  78. if (is_file){
  79. counts_objects["files"]++;
  80. } else {
  81. counts_objects["directories"]++;
  82. }
  83. if (index == file_paths.length - 1) {
  84. if (command_output_file[0] == false)
  85. console.log(`${prefix}└── ${file_path}`);
  86. else
  87. fs.appendFileSync(command_output_file[1], `\n${prefix}└── ${file_path}`)
  88. if ((is_file) == false)
  89. tree(absolute_path, `${prefix} `,counts_objects);
  90. } else {
  91. if (command_output_file[0] == false)
  92. console.log(`${prefix}├── ${file_path}`);
  93. else
  94. fs.appendFileSync(command_output_file[1], `\n${prefix}├── ${file_path}`);
  95. if ((is_file) == false) {
  96. tree(absolute_path, `${prefix}│
  97. `, counts_objects);
  98. }
  99. }
  100. });
  101. } else {
  102. let file_paths = fs.readdirSync(directory);
  103. file_paths.forEach((file_path, index) => {
  104. let absolute_path = path.join(directory, file_path);
  105. let is_dir = fs.lstatSync(absolute_path).isDirectory();
  106.  
  107. if (file_path.charAt(0) == "." || !(is_dir)) {
  108. return;
  109. }
  110. counts_objects["directories"]++;
  111. if (index == file_paths.length - 1) {
  112. if (!command_output_file[0]){
  113. console.log(`${prefix}└── ${file_path}`);
  114. } else {
  115. fs.appendFileSync(command_output_file[1], `\n${prefix}└── ${file_path}`);
  116. }
  117. tree(absolute_path, `${prefix} `, counts_objects);
  118. } else {
  119. if (!command_output_file[0]){
  120. console.log(`${prefix}├── ${file_path}`);
  121. } else {
  122. fs.appendFileSync(command_output_file[1], `\n${prefix}├── ${file_path}`);
  123. }
  124. tree(absolute_path, `${prefix}│ `, counts_objects);
  125. }
  126. });
  127. }
  128. };
  129.  
  130. let argv = process.argv.length;
  131. let command_only_directory = false;
  132. let command_output_file = [false, ""];
  133. var i = 0;
  134. while (i < argv && command_only_directory == false && command_output_file[0] == false) {
  135. if (process.argv[i] == "-d")
  136. command_only_directory = true;
  137. if (process.argv[i] == "-o") {
  138. command_output_file[0] = true;
  139. command_output_file[1] = process.argv[i + 1];
  140. if (command_output_file[1] == undefined) {
  141. console.error("Error! Filename is undefined");
  142. return;
  143. }
  144. fs.writeFileSync(command_output_file[1], "");
  145. }
  146. i++;
  147. }
  148.  
  149. let directory = (process.argv[2] == "-d" || "-o" ? "." : process.argv[2]);
  150. const counts_objects = {
  151. directories: 0,
  152. files: 0
  153. };
  154. if (command_output_file[0] == false){
  155. console.log(directory);
  156. } else fs.appendFileSync(command_output_file[1], directory);
  157.  
  158. tree(directory, "", counts_objects);
  159. if (command_only_directory == false){
  160. if (command_output_file[0] == false)
  161. console.log(`\n ${counts_objects.directories} directories, ${counts_objects.files} files`);
  162. else
  163. fs.appendFileSync(command_output_file[1], `\n ${counts_objects.directories} directories, ${counts_objects.files} files`);
  164. } else if (command_output_file[0] == false){
  165. console.log(`\n ${counts_objects.directories} directories`);
  166. } else {
  167. fs.appendFileSync (command_output_file[1], `\n ${counts_objects.directories} directories`);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement