UNPKG

dir_tree

Version:

Creates a Searchable, Sortable & Printable Tree For Stated Directory Path.

506 lines (505 loc) 18.8 kB
#!/usr/bin/env node var DirTree = require(__dirname + '/../dir_tree'); var args = require(__dirname + '/cli_args - dtree'); var helper_funcs = require(__dirname + '/../helper_funcs'); var err = helper_funcs.err; var sizify = helper_funcs.sizify; var countify_dirs = helper_funcs.countify_dirs; var countify_files = helper_funcs.countify_files; var countify_paths = helper_funcs.countify_paths; var describe_dtn_plain = function(e) { console.log(e.path_from_root); }; var describe_dir = function(e, i) { console.log('\n#' + (i + 1)); console.log('Name : ' + e.name); console.log('Path : ' + e.path_from_root); console.log('Size : ' + sizify(e.size)); console.log('No of Dirs : ' + countify_dirs(e.dirs instanceof Array ? e.dirs.length : e.no_of_dirs)); console.log('No of Files : ' + countify_files(e.files instanceof Array ? e.files.length : e.no_of_files)); console.log('Created on : ' + e.created_on); console.log('Modified on : ' + e.modified_on); }; var describe_file = function(e, i) { console.log('\n#' + (i + 1)); console.log('Name : ' + e.name); console.log('Path : ' + e.path_from_root); console.log('Size : ' + sizify(e.size)); console.log('Created on : ' + e.created_on); console.log('Modified on : ' + e.modified_on); } var filters = []; var max_depth; var get_dtree = function(cb) { var initial_filter = filters.shift(); if (initial_filter == undefined) initial_filter = {idr: null, edr: null, ifr: null, efr: null}; initial_filter.max_depth = max_depth; var dir_tree_obj = new DirTree(targ_dir_path, initial_filter); dir_tree_obj.on('error', function(error, dtn) { err('\n Oops!\n\n Please ensure the path points to a valid dir.\n'); }); dir_tree_obj.on('data', function(dtn) { filters.forEach(function(filter) { dtn = dtn.filter_files(filter); }); if (dtn == null) err('\n Nothing Found!'); cb(dtn); }); }; var targ_dir_path = args.shift(); for (var loop_counter = 0; loop_counter < 1; ++loop_counter) { arg = args.shift(); if (arg != undefined) arg = arg.toLowerCase(); switch (arg) { case '-md': case '-max-depth': { arg = args.shift(); max_depth = parseInt(arg); if (isNaN(max_depth)) err('\n Invalid argument: Maximum depth must be a non-zero natural number.\n\n For assistance, execute:\n\n dtree -h [command]\n'); break; } default: args.unshift(arg); } } for (var loop_counter = 0; loop_counter < 1; ++loop_counter) { arg = args.shift(); if (arg != undefined) arg = arg.toLowerCase(); switch (arg) { case '-d': case '-dirs': { arg = args.shift(); if (arg == undefined) { get_dtree(function(dtn) { console.log('\nThe no of the direct descendant dirs to the "' + dtn.name + '" dir is:\n\n ' + countify_dirs(dtn.dirs.length)); dtn.dirs.sort(DirTree.alphabetic_comparator).forEach(describe_dir); process.exit(0); }); return 0; } else if (/^-p(lain)?$/.test(arg)) { get_dtree(function(dtn) { dtn.dirs.sort(DirTree.alphabetic_comparator).forEach(describe_dtn_plain); process.exit(0); }); return 0; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } case '-nod': case '-no-of-dirs': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log('\nThe no of the direct descendant dirs to the "' + dtn.name + '" dir is:\n\n ' + countify_dirs(dtn.dirs.length)); process.exit(0); }); return 0; } case '-td': case '-total-dirs': { arg = args.shift(); if (arg == undefined) { get_dtree(function(dtn) { console.log('\nThe no of both the direct & the indirect descendant dirs to the "' + dtn.name + '" dir is:\n\n ' + countify_dirs(dtn.no_of_total_dirs)); dtn.total_dirs().forEach(describe_dir); process.exit(0); }); return 0; } else if (/^-p(lain)?$/.test(arg)) { get_dtree(function(dtn) { dtn.total_dirs().forEach(describe_dtn_plain); process.exit(0); }); return 0; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } case '-notd': case '-no-of-total-dirs': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log('\nThe no of both the direct & the indirect descendant dirs to the "' + dtn.name + '" dir is:\n\n ' + countify_dirs(dtn.no_of_total_dirs)); process.exit(0); }); return 0; } case '-f': case '-files': { arg = args.shift(); if (arg == undefined) { get_dtree(function(dtn) { console.log('\nThe no of the direct descendant files to the "' + dtn.name + '" dir is:\n\n ' + countify_files(dtn.files.length)); dtn.files.forEach(describe_file); process.exit(0); }); return 0; } else if (/^-p(lain)?$/.test(arg)) { get_dtree(function(dtn) { dtn.files.forEach(describe_dtn_plain); process.exit(0); }); return 0; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } case '-nof': case '-no-of-files': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log('\nThe no of the direct descendant files to the "' + dtn.name + '" dir is:\n\n ' + countify_files(dtn.files.length)); process.exit(0); }); return 0; } case '-tf': case '-total-files': { arg = args.shift(); if (arg == undefined) { get_dtree(function(dtn) { console.log('\nThe no of both the direct & the indirect descendant files to the "' + dtn.name + '" dir is:\n\n ' + countify_files(dtn.no_of_total_files)); dtn.total_files().forEach(describe_file); process.exit(0) }); return 0; } else if (/^-p(lain)?$/.test(arg)) { get_dtree(function(dtn) { dtn.total_files().forEach(describe_dtn_plain); process.exit(0); }); return 0; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } case '-notf': case '-no-of-total-files': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log('\nThe no of both the direct & the indirect descendant files to the "' + dtn.name + '" dir is:\n\n ' + countify_files(dtn.no_of_total_files)); process.exit(0); }); return 0; } case '-dco': case '-dirs-creation-order': { arg = args.shift(); if (arg == undefined) { get_dtree(function(dtn) { console.log('\nThe order of creating dirs inside the "' + dtn.name + '" dir is:\n\n ' + dtn.dirs_creation_order().join('\n\n ')); process.exit(0); }); return 0; } else if (/^-p(lain)?$/.test(arg)) { get_dtree(function(dtn) { console.log(dtn.dirs_creation_order().join('\n')); process.exit(0); }); return 0; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } case '-ft': case '-fileless-tree': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log('\n' + dtn.fileless_tree(' ')); process.exit(0); }); return 0; } case '-sz': case '-size': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log('\nThe sum of the sizes of both the direct & the indirect descendant files to the "' + dtn.name + '" dir is:\n\n ' + sizify(dtn.size)); process.exit(0); }); return 0; } case '-sof': case '-size-of-files': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log('\nThe sum of the sizes of the direct descendant files to the "' + dtn.name + '" dir is:\n\n ' + sizify(dtn.size_of_files)); process.exit(0); }); return 0; } case '-fd': case '-filter-dirs': { var idr; var edr; var i = 0; var pattern_string; var plain = false; loop_: for (; i < 2; ++i) { arg = args.shift(); if (arg == undefined) break loop_; var groups = arg.split(/^-([eiEI])[dD][rR](?:-(i|ig|g|gi))?$/); if (groups.length != 4) { if (/^-p(lain)?$/.test(groups[0])) { plain = true; break loop_; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } switch (groups[1].toLowerCase()) { case 'i': try { var pattern_string = args.shift(); if (pattern_string == undefined) break loop_; idr = new RegExp(pattern_string, groups[2]); } catch (error) { err('\n Invalid idr!\n'); } break; case 'e': try { var pattern_string = args.shift(); if (pattern_string == undefined) break loop_; edr = new RegExp(pattern_string, groups[2]); } catch (error) { err('\n Invalid edr!\n'); } break; default: break loop_; } } if (i == 0 && idr == undefined && edr == undefined) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); else { if (!plain) { get_dtree(function(dtn) { dtn_array = dtn.filter_dirs(idr, edr); console.log('\n ' + countify_dirs(dtn_array.length) + ' found.'); dtn_array.forEach(describe_dir); process.exit(0); }); return 0; } else { get_dtree(function(dtn) { dtn_array = dtn.filter_dirs(idr, edr); dtn_array.forEach(describe_dtn_plain); process.exit(0); }); return 0; } } } case '-ff': case '-filter-files': { var idr = null; var edr = null; var ifr = null; var efr = null; var i = 0; loop_: for (; i < 4; ++i) { arg = args.shift(); if (arg == undefined) break loop_; var groups = arg.split(/^-([eiEI][fdFD])[rR](?:-(i|ig|g|gi))?$/); if (groups.length != 4) break loop_; switch (groups[1].toLowerCase()) { case 'id': try { var pattern_string = args.shift(); if (pattern_string == undefined) break loop_; idr = new RegExp(pattern_string, groups[2]); } catch (error) { err('\n Invalid idr!\n'); } break; case 'ed': try { var pattern_string = args.shift(); if (pattern_string == undefined) break loop_; edr = new RegExp(pattern_string, groups[2]); } catch (error) { err('\n Invalid edr!\n'); } break; case 'if': try { var pattern_string = args.shift(); if (pattern_string == undefined) break loop_; ifr = new RegExp(pattern_string, groups[2]); } catch (error) { err('\n Invalid ifr!\n'); } break; case 'ef': try { var pattern_string = args.shift(); if (pattern_string == undefined) break loop_; efr = new RegExp(pattern_string, groups[2]); } catch (error) { err('\n Invalid efr!\n'); } break; default: break loop_; } } if (i == 0 && idr == undefined && edr == undefined && ifr == undefined && efr == undefined) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); else { if (i != 4) args.unshift(arg); filters.push({idr: idr, edr: edr, ifr: ifr, efr: efr}); --loop_counter; break; } } case '-up': case '-unread-paths': { arg = args.shift(); if (arg == undefined) { get_dtree(function(dtn) { var arr = dtn.unread_paths; console.log('\nThe no of the direct descendant paths to the "' + dtn.name + '" dir that couldn\'t be read is:\n\n ' + countify_paths(arr.length)); if (arr.length > 0) console.log('\nUnread paths:\n\n ' + arr.join('\n\n ')); process.exit(0); }); return 0; } else if (/^-p(lain)?$/.test(arg)) { get_dtree(function(dtn) { console.log(dtn.unread_paths.join('\n')); process.exit(0); }); return 0; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } case '-noup': case '-no-of-unread-paths': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { var arr = dtn.unread_paths; console.log('\nThe no of the direct descendant paths to the "' + dtn.name + '" dir that couldn\'t be read is:\n\n ' + countify_paths(arr.length)); process.exit(0); }); return 0; } case '-tup': case '-total-unread-paths': { arg = args.shift(); if (arg == undefined) { get_dtree(function(dtn) { console.log('\nThe no of both the direct & the indirect descendant paths to the "' + dtn.name + '" dir that couldn\'t be read is:\n\n ' + countify_paths(dtn.no_of_total_unread_paths)); if (dtn.no_of_total_unread_paths > 0) { var arr = dtn.total_unread_paths(); console.log('\nUnread paths:\n\n ' + arr.join('\n\n ')); } process.exit(0); }) return 0; } else if (/^-p(lain)?$/.test(arg)) { get_dtree(function(dtn) { console.log(dtn.total_unread_paths().join('\n')); process.exit(0); }); return 0; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } case '-notup': case '-no-of-total-unread-paths': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log('\nThe no of both the direct & the indirect descendant paths to the "' + dtn.name + '" dir that couldn\'t be read is:\n\n ' + countify_paths(dtn.no_of_total_unread_paths)); process.exit(0); }); return 0; } case '-st': case '-stats': { if (args.length > 0) err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); get_dtree(function(dtn) { console.log( '\nSome stats about the "' + dtn.name + '" dir:' + '\n\n Size : ' + sizify(dtn.size) + '\n\n Size of files : ' + sizify(dtn.size_of_files) + '\n\n No of dirs : ' + countify_dirs(dtn.dirs.length) + '\n\n No of files : ' + countify_files(dtn.files.length) + '\n\n No of unread paths : ' + countify_paths(dtn.unread_paths.length) + '\n\n No of total dirs : ' + countify_dirs(dtn.no_of_total_dirs) + '\n\n No of total files : ' + countify_files(dtn.no_of_total_files) + '\n\n No of total unread paths : ' + countify_paths(dtn.no_of_total_unread_paths) + '\n\n Created on : ' + dtn.created_on + '\n\n Modified on : ' + dtn.modified_on ); process.exit(0); }); return 0; } case '-dhf': case '-dirs-having-files': { arg = args.shift(); if (arg == undefined) { get_dtree(function(dtn) { if (dtn == null) err('\n Nothing found!\n'); var total_dirs = dtn.total_dirs(); var dhf = new Array(total_dirs.length); var k = 0; total_dirs.forEach(function(dir) { if (dir.no_of_files > 0) dhf[k++] = dir; }); dhf.length = k; console.log('\n ' + countify_dirs(k) + ' found.'); dhf.forEach(describe_dir); process.exit(0); }); return 0; } else if (/^-p(lain)?$/.test(arg)) { get_dtree(function(dtn) { if (dtn == null) err('\n Nothing found!\n'); dtn.total_dirs().forEach(function(dir) { if (dir.no_of_files > 0) describe_dtn_plain(dir); }); process.exit(0); }); return 0; } else err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } case undefined: { get_dtree(function(dtn) { if (dtn == null) err('\n Nothing found!\n'); console.log('\n' + dtn.tree(' ')); process.exit(0); }); return 0; } default: { err('\n Invalid arguments.\n\n For assistance, execute:\n\n dtree -h [command]\n'); } } }