2019-12-16
17:05:54
var fs = require('fs'); fs.readdir(__dirname,function(err,files){ console.log(files); });
/** * Module dependencies. */ var fs = require('fs'); fs.readdir(__dirname, function (err, files) { console.log(''); if (!files.length) { return console.log(' 33[31m No files to show! 33[39m '); } console.log(' Select which file or directory you want to see '); function file(i) { var filename = files[i]; fs.stat(__dirname + '/' + filename, function (err, stat) { if (stat.isDirectory()) { console.log(' '+i+' 33[36m' + filename + '/ 33[39m'); } else { console.log(' '+i+' 33[90m' + filename + ' 33[39m'); } if (++i == files.length) { console.log(''); process.stdout.write(' 33[33mEnter your choice: 33[39m'); process.stdin.resume(); process.stdin.setEncoding('utf8'); } else { file(i); } }); } file(0); });
/** * Module dependencies. */ var fs = require('fs') , stdin = process.stdin , stdout = process.stdout /** * Read the current directory. */ fs.readdir(__dirname, function (err, files) { console.log(''); if (!files.length) { return console.log(' 33[31m No files to show! 33[39m '); } console.log(' Select which file or directory you want to see '); // called for each file walked in the directory function file(i) { var filename = files[i]; fs.stat(__dirname + '/' + filename, function (err, stat) { if (stat.isDirectory()) { console.log(' '+i+' 33[36m' + filename + '/ 33[39m'); } else { console.log(' '+i+' 33[90m' + filename + ' 33[39m'); } if (++i == files.length) { read(); } else { file(i); } }); } // read user input when files are shown function read () { console.log(''); stdout.write(' 33[33mEnter your choice: 33[39m'); stdin.resume(); stdin.setEncoding('utf8'); stdin.on('data', option); } // called with the option supplied by the user function option (data) { if (!files[Number(data)]) { stdout.write(' 33[31mEnter your choice: 33[39m'); } else { stdin.removeListener('data', option); } } // start by walking the first file file(0); });
完成:
/** * Module dependencies. */ var fs = require('fs') , stdin = process.stdin , stdout = process.stdout /** * Read the current directory. */ fs.readdir(__dirname, function (err, files) { console.log(''); if (!files.length) { return console.log(' 33[31m No files to show! 33[39m '); } console.log(' Select which file or directory you want to see '); // called for each file walked in the directory function file(i) { var filename = files[i]; fs.stat(__dirname + '/' + filename, function (err, stat) { if (stat.isDirectory()) { console.log(' '+i+' 33[36m' + filename + '/ 33[39m'); } else { console.log(' '+i+' 33[90m' + filename + ' 33[39m'); } if (++i == files.length) { read(); } else { file(i); } }); } // read user input when files are shown function read () { console.log(''); stdout.write(' 33[33mEnter your choice: 33[39m'); stdin.resume(); stdin.setEncoding('utf8'); stdin.on('data', option); } // called with the option supplied by the user function option (data) { if (!files[Number(data)]) { stdout.write(' 33[31mEnter your choice: 33[39m'); } else { stdin.removeListener('data', option); } } // start by walking the first file file(0); });