zoukankan      html  css  js  c++  java
  • Node.js 中的重要API:命令行工具以及FS API 首个Node应用

    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);
    });

     

      

     

     

     

      

     

     

  • 相关阅读:
    linux学习(一)
    Linux学习(用户管理)
    anyproxy mac安装
    python mitmproxy 代理
    Js常用方法map, sort
    echarts常用配置项【持续更新】
    【转】moment js 使用
    js Cannot assign to read only property 'exports' of object '#<Object>' at Modul,import 与module.exports混用问题
    a标签跳转referer漏洞
    element ui rate评分组建使用
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12051041.html
Copyright © 2011-2022 走看看