{"id":2944,"date":"2013-07-16T21:27:13","date_gmt":"2013-07-16T12:27:13","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=2944"},"modified":"2013-12-29T23:04:31","modified_gmt":"2013-12-29T14:04:31","slug":"post-2944","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/node-js\/2944","title":{"rendered":"[node.js]\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u518d\u5e30\u7684\u306b\u305f\u3069\u3063\u3066\u30d5\u30a1\u30a4\u30eb\u4e00\u89a7\u3092JSON\u3068\u3057\u3066\u51fa\u529b\u3059\u308b"},"content":{"rendered":"<p>node.js\u3092\u4f7f\u3063\u3066\u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u4ee5\u4e0b\u3092\u518d\u5e30\u7684\u306b\u305f\u3069\u308a\u30d5\u30a1\u30a4\u30eb\u4e00\u89a7\u3092\u51fa\u529b\u3057\u307e\u3059\u3002\uff08\u5f15\u6570\u3067\u5bfe\u8c61\u3068\u306a\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\uff09<br \/>\n\u30d5\u30a1\u30a4\u30eb\u540d\u3068\u5171\u306b\u30d5\u30a1\u30a4\u30eb\u30b5\u30a4\u30ba\u3082\u51fa\u529b\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/gist.github.com\/shimizu\/7bd8c02c3d49492d1350\">shimizu \/ filelist2json.js<\/a><\/p>\n<pre class=\"lang:js decode:true \" title=\"filelist2json.js\" >var fs = require(&quot;fs&quot;)\r\n\t, path = require(&quot;path&quot;)\r\n\t, dir = process.argv[2] || '.'; \/\/\u5f15\u6570\u304c\u7121\u3044\u3068\u304d\u306f\u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u5bfe\u8c61\u3068\u3059\u308b\r\n\r\n\r\nvar walk = function(p, callback){\r\n\tvar results = [];\r\n\t\t\r\n\tfs.readdir(p, function (err, files) {\r\n\t\tif (err) throw err;\r\n\r\n\t\tvar pending = files.length;\t\r\n\t\tif (!pending) return callback(null, results); \/\/\u5168\u3066\u306e\u30d5\u30a1\u30a4\u30eb\u53d6\u5f97\u304c\u7d42\u308f\u3063\u305f\u3089\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u547c\u3073\u51fa\u3059\r\n\t\t\r\n\t\tfiles.map(function (file) { \/\/\u30ea\u30b9\u30c8\u53d6\u5f97\r\n\t\t\treturn path.join(p, file);\r\n\t\t}).filter(function (file) {\r\n\t\t\tif(fs.statSync(file).isDirectory()) walk(file, function(err, res) { \/\/\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3060\u3063\u305f\u3089\u518d\u5e30\r\n\t\t\t\tresults.push({name:path.basename(file), children:res}); \/\/\u5b50\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092children\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u914d\u4e0b\u306b\u4fdd\u5b58\r\n\t\t\t\tif (!--pending) callback(null, results);\r\n\t\t\t });\r\n\t\t\treturn fs.statSync(file).isFile();\r\n\t\t}).forEach(function (file) { \/\/\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u4fdd\u5b58\r\n\t\t\tvar stat = fs.statSync(file);\r\n\t\t\tresults.push({file:path.basename(file), size:stat.size});\r\n\t\t\tif (!--pending) callback(null, results);\r\n\t\t});\r\n\t\t\r\n\t});\r\n}\r\n\r\nwalk(dir, function(err, results) {\r\n\tif (err) throw err;\r\n\tvar data = {name:'root', children:results};\r\n\tconsole.log(JSON.stringify(data)); \/\/\u4e00\u89a7\u51fa\u529b\r\n});<\/pre>\n<p>\u5b9f\u884c<\/p>\n<pre class=\"lang:sh decode:true \" >$ node filelist2json &gt; data.json<\/pre>\n<p>\u51fa\u529b\u5185\u5bb9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >{\r\n\tname:'root',\r\n\tchildren:[\r\n\t\tname:'child_dir1'\r\n\t\t{file:'hoge1.txt', size:1111},\r\n\t\t{file:'hoge2.txt', size:2222},\r\n\t\tchildren:[\r\n\t\t\tname:'child_dir2',\r\n\t\t\t{file:'hello1.txt', size:3333},\r\n\t\t\t{file:'hello2.txt', size:4444}\r\n\t\t]\r\n\t},\r\n\t{file:'file1.txt', size:88888},\r\n\t{file:'file2.txt', size:33333}\r\n}<\/pre>\n<p>D3.js\u306b\u6e21\u3057\u3066\u4f7f\u3046\u305f\u3081\u306b\u66f8\u3044\u305f\u30b9\u30af\u30ea\u30d7\u30c8\u306a\u306e\u3067\u3061\u3087\u3063\u3068\u7279\u6b8a\u306a\u5f62\u3067\u51fa\u529b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4e0a\u8a18\u30c7\u30fc\u30bf\u3092\u57fa\u306b\u4f5c\u3063\u305f\u3082\u306e<br \/>\n<a href=\"https:\/\/gunmagisgeek.com\/blog\/d3-js\/2942\">\u30b5\u30a4\u30ba\u306e\u5927\u304d\u306a\u30d5\u30a1\u30a4\u30eb\u3092\u53ef\u8996\u5316\u3059\u308b\uff08\u30c4\u30ea\u30fc\u30de\u30c3\u30d7\uff09<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>node.js\u3092\u4f7f\u3063\u3066\u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u4ee5\u4e0b\u3092\u518d&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2944","post","type-post","status-publish","format-standard","hentry","category-node-js"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=2944"}],"version-history":[{"count":1,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2944\/revisions"}],"predecessor-version":[{"id":2945,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2944\/revisions\/2945"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=2944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=2944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=2944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}