Skip to content

Commit

Permalink
Use newer syntax for javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Nov 30, 2018
1 parent 2e73b66 commit 0a52687
Showing 1 changed file with 18 additions and 40 deletions.
58 changes: 18 additions & 40 deletions tree.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,39 @@
#!/usr/bin/env node

var fs = require('fs');
var path = require('path');

function Tree() {
this.dirCount = 0;
this.fileCount = 0;
}

Tree.prototype._register = function (filepath) {
if (fs.lstatSync(filepath).isDirectory()) {
this.dirCount += 1;
} else {
this.fileCount += 1;
}
};

Tree.prototype.walk = function (directory, prefix) {
var _this = this;
var filepaths = fs.readdirSync(directory);
const fs = require("fs");
const path = require("path");

filepaths.forEach(function (filepath, index) {
var absolute;
var isDirectory;
const walk = (directory, prefix, counts) => {
const filepaths = fs.readdirSync(directory);

if (filepath.charAt(0) == '.') {
filepaths.forEach((filepath, index) => {
if (filepath.charAt(0) == ".") {
return;
}
absolute = path.join(directory, filepath);
_this._register(absolute);

isDirectory = fs.lstatSync(absolute).isDirectory();
const absolute = path.join(directory, filepath);
const isDirectory = fs.lstatSync(absolute).isDirectory();

counts[isDirectory ? "dirs" : "files"] += 1;

if (index == filepaths.length - 1) {
console.log(prefix + '└── ' + filepath);
console.log(`${prefix}└── ${filepath}`);
if (isDirectory) {
_this.walk(absolute, prefix + ' ');
walk(absolute, `${prefix} `, counts);
}
} else {
console.log(prefix + '├── ' + filepath);
console.log(`${prefix}├── ${filepath}`);
if (isDirectory) {
_this.walk(absolute, prefix + '│   ');
walk(absolute, `${prefix}│   `, counts);
}
}
});
};

Tree.prototype.summary = function () {
return this.dirCount.toString() + ' directories, ' + this.fileCount + ' files';
}
const directory = process.argv[2] || ".";
const counts = { dirs: 0, files: 0 };

var directory = '.';
if (process.argv.length > 2) {
directory = process.argv[2];
}
console.log(directory);
walk(directory, "", counts);

var tree = new Tree();
tree.walk(directory, '');
console.log("\n" + tree.summary());
console.log(`\n${counts.dirs} directories, ${counts.files} files`);

0 comments on commit 0a52687

Please sign in to comment.