{"id":5666,"date":"2017-02-13T10:57:33","date_gmt":"2017-02-13T02:57:33","guid":{"rendered":"https:\/\/sdeno.com\/?p=5666"},"modified":"2017-03-05T15:33:03","modified_gmt":"2017-03-05T07:33:03","slug":"nodejs-%e5%bc%80%e5%8f%91%e7%ac%ac%e4%b8%80%e4%b8%aa-cli%e5%91%bd%e4%bb%a4%e8%a1%8c%e7%95%8c%e9%9d%a2-%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5666","title":{"rendered":"Nodejs \u5f00\u53d1\u7b2c\u4e00\u4e2a CLI(\u547d\u4ee4\u884c\u754c\u9762) \u7a0b\u5e8f"},"content":{"rendered":"<p>node.js dos\u754c\u9762 \u547d\u4ee4\u7a0b\u5e8f<\/p>\n<p>\u4ec0\u4e48\u662fcli\u547d\u4ee4<\/p>\n<p>\u7c7b\u4f3c\u4e8e\u6211\u4eec\u5e73\u65f6\u88c5win\u7cfb\u7edf\u65f6\uff0c\u8fdb\u5165\u5230\u4e00\u4e2ados\u754c\u9762\u8ba9\u6211\u4eec\u9009\u62e9\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u6216\u8005\u8f93\u5165\u76f8\u5e94\u7684\u5149\u6807\u4e4b\u540e\u5b89\u56de\u8f66\u4e4b\u540e\u5c31\u5f00\u59cb\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n<figure class=\"mdx-lazyload-container\" style=\"max-width:500px\"><div style=\"padding-top:45.4%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img width=\"500\" height=\"227\" class=\"alignnone wp-image-5667 lazyload\" title=\"node_cli\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/sdeno.com\/wp-content\/uploads\/2017\/02\/node_cli.png\" alt=\"node_cli\"><\/figure>\n<p>\u7c7b\u4f3c\u4e8e\u8fd9\u6837\u7684\u5c31\u662fcli\u547d\u4ee4\u3002<\/p>\n<p>&nbsp;<\/p>\n<pre>\/\/run.js\r\n\r\nvar fs = require('fs');\r\nvar stdin = process.stdin;\r\nvar stdout = process.stdout;\r\n\r\nvar curDir = process.cwd();\r\nfunction getDirectoryData(dir){\r\n fs.readdir(dir,function(err,files){\r\n   console.log(\"\");\r\n   if(!files.length){\r\n       return console.log(' \\0333[31m No files to show! \\033[39m\\n');\r\n   }\r\n   console.log(' Select which file or directory you want to see! \\n');\r\n \r\n   function file(i){\r\n     var filename = files[i];\r\n \r\n     var statFile = fs.statSync(__dirname + '\/' + filename);\r\n     if(statFile.isDirectory()){\r\n        console.log(' '+ ++i + ' \\033[31m' + filename + '\/\\033[39m');\r\n     }else{\r\n        console.log(' '+ ++i + ' \\033[90m' + filename + '\\033[39m');\r\n     }\r\n \r\n     if( i == files.length){\r\n       read();\r\n     }else{\r\n       file(i);\r\n     }\r\n }\r\n \r\n file(0);\r\n \r\n function read(){\r\n    console.log('');\r\n    stdout.write(' \\033[33m Enter your choice:\u3000\\033[39m');\r\n    stdin.resume();\r\n    stdin.setEncoding('utf8');\r\n \r\n    stdin.on('data',option);\r\n }\r\n \r\n  function option(data){\r\n    var filename = files[Number(data)-1];\r\n    if(fs.statSync(__dirname + '\/' + filename).isDirectory()){\r\n        stdin.pause(); \r\n        fs.readdir(__dirname + '\/' +filename,function(err,files){\r\n            console.log('');\r\n            console.log(' (' + files.length + ' files)');\r\n            files.forEach(function(file){\r\n               console.log(' -- ' + file);\r\n            });\r\n            console.log('');\r\n        })\r\n    }else{\r\n        stdin.pause();\r\n        fs.readFile(__dirname + '\/' +filename,'utf8',function(err,data){\r\n          console.log('');\r\n          console.log('\\033[90m' + data.replace(\/(.*)\/g,' $1') + '\\033[39m');\r\n        });\r\n     }\r\n  }\r\n });\r\n}\r\ngetDirectoryData(curDir);<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/blog.163.com\/hule_sky\/blog\/static\/2091622452016621103745136\/\" target=\"_blank\">http:\/\/blog.163.com\/hule_sky\/blog\/static\/2091622452016621103745136\/\u00a0<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>node.js dos\u754c\u9762 \u547d\u4ee4\u7a0b\u5e8f \u4ec0\u4e48\u662fcli\u547d\u4ee4 \u7c7b\u4f3c\u4e8e\u6211\u4eec\u5e73\u65f6\u88c5win\u7cfb\u7edf\u65f6\uff0c\u8fdb\u5165\u5230\u4e00\u4e2ados\u754c\u9762\u8ba9\u6211 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,11],"tags":[],"class_list":["post-5666","post","type-post","status-publish","format-standard","hentry","category-nodejs","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5666"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5666\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}