禅意coder 禅意coder
首页
  • 跟我一起学NodeJs
  • JavaScript 异步编程
  • kafka 相关知识
  • 分类
  • 标签
  • 归档
关于
GitHub
首页
  • 跟我一起学NodeJs
  • JavaScript 异步编程
  • kafka 相关知识
  • 分类
  • 标签
  • 归档
关于
GitHub
  • 一起学nodejs

    • 一起学nodejs(基本概念)
    • 一起学nodejs(微型文件系统实现)
    • 一起学nodejs(Buffer)
    • 一起学nodejs(写一个基于TCP/IP终端聊天系统)
    • nodejs_connect
  • javascript 异步编程

  • kafka知识

  • linux的奥义

  • java语言

  • 系列文章
  • 一起学nodejs
liushaoqing
2018-02-11

一起学nodejs(微型文件系统实现)

# 一起学nodejs(微型文件系统实现)

用nodejs实现一个命令行的文件系统查看工具。

为了练习nodejs cli 以及 少量的文件操作api. 收获如下:

  1. 命令行ansic color 的使用

  2. fs , path 模块的了解

  3. stdin, stdout 输入输出事件的了解

代码地址

var fs = require("fs");
path = require("path");
stdin = process.stdin;
stdout = process.stdout;

readDir(process.cwd());
function readDir(dirPath) {
  fs.readdir(dirPath, function(err, files) {
    console.log("\033[32mcurrent: \033[34m " + dirPath + " \033[39m");
    var stats = [];
    if (!files.length) {
      return console.log("   \033[31m No files to show! \033[39m \n");
    }
    console.log(
      "\033[36m" +
        "Select which file or dictionary you want to see: \n" +
        " \033[39m \n"
    );
    file(0);
    function file(i) {
      var filename = files[i];
      fs.stat(path.join(dirPath, filename), function(error, stat) {
        stats[i] = stat;
        if (stat.isDirectory()) {
          console.log("           " + i + " \033[32m" + filename + "/\033[39m");
        } else {
          console.log("           " + i + " \033[36m" + filename + "/\033[39m");
        }
        i++;
        if (i == files.length) {
          read();
        } else {
          file(i);
        }
      });
    }

    function read() {
      console.log("");
      console.log(
        "\033[36m" +
          'Enter your choice (number), or back to up file path:(example: "../") , or quit input ("quit").' +
        "\033[39m"
      );
      stdout.write("\033[36m" + "your choice is >> " + "\033[39m");
      stdin.resume(); // 输入等待
      stdin.setEncoding("utf-8");
      stdin.removeAllListeners("data"); //移除所有的监听输入的事件再注册,不然会有两次事件
      stdin.on("data", enterCallback);
    }

    function enterCallback(i) {
      if ("quit" == i.trim()) {
        console.log("\033[31m" + "Bye bye! " + "\033[39m");
        stdin.end();
      } else {
        if (isNaN(Number(i.trim()))) {
          fs.access(
            path.normalize(path.resolve(dirPath, i.trim())),
            fs.constants.R_OK,
            err => {
              if (err) {
                console.log("");
                console.log(
                  "\033[31m" +
                    "you can not access this path : " +
                    path.resolve(dirPath, i.trim()) +
                    "\033[39m"
                );
                readDir(path.resolve(dirPath));
              } else {
                readDir(path.resolve(dirPath, i.trim()));
              }
            }
          );
        }
        var oneFile = files[Number(i.trim())];
        if (!oneFile) {
          stdout.write("    \033[36m" + "Enter your choice: " + "\033[39m");
        } else {
          if (stats[Number(i.trim())].isDirectory()) {
            readDir(path.join(dirPath, oneFile));
          } else {
            fs.readFile(
              path.resolve(dirPath, files[Number(i)]),
              "utf-8",
              function(err, data) {
                if (err) {
                  console.log("", err);
                  return;
                }
                console.log("\033[32m" + "file content is:" + "\033[39m");
                console.log("");
                console.log("\033[42m" + data + "\033[40m");
                console.log("");
                readDir(dirPath);
              }
            );
          }
        }
      }
    }
  });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
编辑
#前端#nodejs
上次更新: 2021/01/23, 09:10:58
一起学nodejs(基本概念)
一起学nodejs(Buffer)

← 一起学nodejs(基本概念) 一起学nodejs(Buffer)→

最近更新
01
轮子哥编程的感悟(转载)
01-23
02
tmux 配置和使用
01-23
03
命令行日常使用配置
01-15
更多文章>
Theme by Vdoing | Copyright © 2020-2021 刘少卿 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×