zoukankan      html  css  js  c++  java
  • 【xinsir】分享一个查找文件的脚手架

     1 program.command('find <name>').action(name => {
     2   if (name) {
     3     inquirer
     4       .prompt([
     5         {
     6           type: 'input',
     7           name: 'isAll',
     8           message: '输入的文件名是否是文件全称 yes/no',
     9           validate(input) {
    10             let arr = ['yes', 'no'];
    11             if (!arr.includes(input)) {
    12               return '请输入yes/no';
    13             }
    14             return true;
    15           }
    16         }
    17       ])
    18       .then(answers => {
    19         const spinner = ora(`正在寻找${name}...`);
    20         spinner.start();
    21         try {
    22           let isAll = answers.isAll === 'yes'; // 是否为全称
    23           let files = []; // 找到文件名的路径存储
    24           // 递归遍历目录下的文件
    25           function readDirSync(path) {
    26             var pa = fs.readdirSync(path);
    27             pa.forEach(function(ele, index) {
    28               var info = fs.statSync(path + '/' + ele);
    29               if (info.isDirectory()) {
    30                 readDirSync(path + '/' + ele);
    31               } else {
    32                 if((!isAll && ele.includes(name)) || (isAll && ele === name)) {
    33                   files.push(path + '/' + ele);
    34                 }
    35               }
    36             });
    37           }
    38           readDirSync('.');
    39           if (files.length > 0) {
    40             spinner.succeed();
    41             files = files.join('
    ');
    42             console.log(symbols.success, chalk.green('文件已找到,文件路径如下:
    ' + files));
    43           } else {
    44             spinner.fail();
    45             console.log(symbols.error, chalk.red(`该目录下未含有名为'${name}'的文件`));
    46           }
    47         } catch (error) {
    48           console.log(symbols.error, chalk.red(error));
    49           spinner.fail();
    50           console.log(symbols.error, chalk.red('在寻找文件过程出错'));
    51         }
    52       });
    53   } else {
    55     console.log(
    56       symbols.error,
    57       chalk.red('请输入完整命令:xinsir find xx(寻找的文件名)')
    58     );
    59   }
    60 });
    61 program.parse(process.argv);

    效果如下:

  • 相关阅读:
    微信小程序支付
    python中 try、except、finally执行顺序
    磁盘设备在 Linux 下的表示方法
    sanic中间件和监听器
    sed命令详解
    awk命令详解
    gd库
    php中计算二维数组中某一元素之和
    Linux SVN 命令详解
    PHP array 操作函数
  • 原文地址:https://www.cnblogs.com/xinsir/p/11805206.html
Copyright © 2011-2022 走看看