Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const fs = require("fs");
- function CounterOfWord(string) {
- return (string.split(/\s/).filter((s) => s != '').length);
- };
- function CounterOfLines(string) {
- return (string.split('\n').length - 1);
- }
- const EnteredString = fs.readFileSync(process.argv[2], "utf8");
- var c = false,
- m = false,
- w = false,
- l = false;
- let result = '';
- try {
- if (process.argv.length == 3) {
- let stats = fs.statSync(process.argv[2]);
- //result += ` ${stats["size"]}`;
- result += ` ${CounterOfWord(EnteredString) + 1}`;
- result += ` ${CounterOfLines(EnteredString)}`;
- result += ` ${EnteredString.length}`;
- } else {
- var j = 0;
- while (j < process.argv.length) {
- if (process.argv[j] == "-c")
- c = true;
- if (process.argv[j] == "-m")
- m = true;
- if (process.argv[j] == "-w")
- w = true;
- if (process.argv[j] == "-l")
- l = true;
- j++;
- }
- if (c == true) {
- let stats = fs.statSync(process.argv[process.argv.length - 1]);
- result += stats["size"];
- }
- if (m == true) {
- result += ` ${EnteredString.length}`;
- }
- if (w == true) {
- result += ` ${CounterOfWord(EnteredString)}`;
- }
- if (l == true) {
- result += ` ${CounterOfLines(EnteredString)}`;
- }
- }
- } catch (e) {
- console.error("WRONG INPUTS");
- }
- result += ` ${process.argv[2]}`;
- console.log(result);
- ///////////////////////////////////////////////////////
- ///////////////////////////////////////////////
- 'use strict'
- const fs = require("fs");
- const path = require("path");
- function tree(directory, prefix, counts_objects){
- if (!command_only_directory){
- let file_paths = fs.readdirSync(directory);
- file_paths.forEach((file_path, index) => {
- if (file_path.charAt(0) == ".") {
- return;
- }
- let absolute_path = path.join(directory, file_path);
- let is_file = fs.lstatSync(absolute_path).isFile();
- if (is_file){
- counts_objects["files"]++;
- } else {
- counts_objects["directories"]++;
- }
- if (index == file_paths.length - 1) {
- if (command_output_file[0] == false)
- console.log(`${prefix}└── ${file_path}`);
- else
- fs.appendFileSync(command_output_file[1], `\n${prefix}└── ${file_path}`)
- if ((is_file) == false)
- tree(absolute_path, `${prefix} `,counts_objects);
- } else {
- if (command_output_file[0] == false)
- console.log(`${prefix}├── ${file_path}`);
- else
- fs.appendFileSync(command_output_file[1], `\n${prefix}├── ${file_path}`);
- if ((is_file) == false) {
- tree(absolute_path, `${prefix}│
- `, counts_objects);
- }
- }
- });
- } else {
- let file_paths = fs.readdirSync(directory);
- file_paths.forEach((file_path, index) => {
- let absolute_path = path.join(directory, file_path);
- let is_dir = fs.lstatSync(absolute_path).isDirectory();
- if (file_path.charAt(0) == "." || !(is_dir)) {
- return;
- }
- counts_objects["directories"]++;
- if (index == file_paths.length - 1) {
- if (!command_output_file[0]){
- console.log(`${prefix}└── ${file_path}`);
- } else {
- fs.appendFileSync(command_output_file[1], `\n${prefix}└── ${file_path}`);
- }
- tree(absolute_path, `${prefix} `, counts_objects);
- } else {
- if (!command_output_file[0]){
- console.log(`${prefix}├── ${file_path}`);
- } else {
- fs.appendFileSync(command_output_file[1], `\n${prefix}├── ${file_path}`);
- }
- tree(absolute_path, `${prefix}│ `, counts_objects);
- }
- });
- }
- };
- let argv = process.argv.length;
- let command_only_directory = false;
- let command_output_file = [false, ""];
- var i = 0;
- while (i < argv && command_only_directory == false && command_output_file[0] == false) {
- if (process.argv[i] == "-d")
- command_only_directory = true;
- if (process.argv[i] == "-o") {
- command_output_file[0] = true;
- command_output_file[1] = process.argv[i + 1];
- if (command_output_file[1] == undefined) {
- console.error("Error! Filename is undefined");
- return;
- }
- fs.writeFileSync(command_output_file[1], "");
- }
- i++;
- }
- let directory = (process.argv[2] == "-d" || "-o" ? "." : process.argv[2]);
- const counts_objects = {
- directories: 0,
- files: 0
- };
- if (command_output_file[0] == false){
- console.log(directory);
- } else fs.appendFileSync(command_output_file[1], directory);
- tree(directory, "", counts_objects);
- if (command_only_directory == false){
- if (command_output_file[0] == false)
- console.log(`\n ${counts_objects.directories} directories, ${counts_objects.files} files`);
- else
- fs.appendFileSync(command_output_file[1], `\n ${counts_objects.directories} directories, ${counts_objects.files} files`);
- } else if (command_output_file[0] == false){
- console.log(`\n ${counts_objects.directories} directories`);
- } else {
- fs.appendFileSync (command_output_file[1], `\n ${counts_objects.directories} directories`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement