Paul Lewis | 911c1b8 | 2019-12-02 12:46:15 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env node |
| 2 | |
| 3 | (function(){ |
| 4 | |
| 5 | try { |
| 6 | var dive = require('dive'); |
| 7 | } catch (ex) { |
| 8 | console.error('this example requires "dive", please run "npm install dive"'); |
| 9 | process.exit(1); |
| 10 | } |
| 11 | |
| 12 | var treeify = require('../treeify'), |
| 13 | path = require('path'), |
| 14 | fs = require('fs'), |
| 15 | rootDir = process.argv.length < 3 ? '.' : process.argv[2], |
| 16 | tree = {}; |
| 17 | |
| 18 | if ( ! fs.existsSync(rootDir)) { |
| 19 | console.error('path "' + rootDir + '" does not exist - unable to proceed!'); |
| 20 | process.exit(1); |
| 21 | } |
| 22 | |
| 23 | console.log(rootDir !== '.' ? path.relative(process.cwd(), rootDir) : '.'); |
| 24 | |
| 25 | function scanComplete() { |
| 26 | process.stdout.write('\r \r'); |
| 27 | console.log(treeify.asTree(tree, true)); |
| 28 | } |
| 29 | |
| 30 | dive(rootDir, { all: true, directories: true }, function(err, thisPath) { |
| 31 | var relativePath = path.relative(rootDir, thisPath), |
| 32 | node = tree; |
| 33 | |
| 34 | if (relativePath.indexOf('..') !== 0) { |
| 35 | relativePath.split(path.sep).forEach(function(part) { |
| 36 | typeof node[part] !== 'object' && (node[part] = {}); |
| 37 | node = node[part]; |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | }, scanComplete); |
| 42 | |
| 43 | process.stdout.write('wait... '); |
| 44 | |
| 45 | })(); |