gotandajs-11-nodejs-recursive-readdir

 gotandajs-11-nodejs-recursive-readdir

Gotanda.js #11 in MobileFactory ( https://gotandajs.connpass.com/event/127779/ ) での発表資料です。

012765e2e29cd000be9e0f32236c46d2?s=128

Araya

May 10, 2019
Tweet

Transcript

  1. Node.jsͰͷrecursive readdirΛߟ͑Δ ͋Β΍ @arayaryoma Gotanda.js #11 in Mobile Factory

  2. ࣗݾ঺հ ͋Β΍ / Ѩ෦ ྇ຏ @arayaryoma @arayaryoma https://araya.dev גࣜձࣾϔϚλΠτ /

    ෭ۀϑϦʔϥϯε JavaScript/TypeScript/Go/Kotlin Angular/React/ReactNative/Next.js/Nest.js
  3. ͔͜͜Β໨ࠇํ໘ʹ10෼͘Β͍า͍ͨͱ͜ΖʹΦϑΟε͕͋ΔձࣾͰಇ͍͍ͯ·͢ ޒ൓ాͷ޷͖ͳ͓ళ͸ϗοτεϓʔϯͰ͢

  4. ୡ੒͍ͨ͜͠ͱ ͜ͷΑ͏ͳσΟϨΫτϦͰ filesΑΓԼʹ͋ΔશͯͷϑΝΠϧͷϦετΛऔಘ͍ͨ͠

  5. ·ͣߟ͑Δ͜ͱ fs.readdirʹΦϓγϣϯΛ౉͢ͱɺ΋͔͍͍ͯ͠͠ײ͡ʹऔΕΔͷͰ͸ͳ͍͔

  6. ·ͣߟ͑Δ͜ͱ ౰વͦΜͳந৅తͳ΋ͷ͸ͳ͍

  7. ੈͷதʹస͕͍ͬͯΔղܾࡦ • https://github.com/jergason/recursive-readdir • https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search • https://gist.github.com/kethinov/6658166
 
 ͳͲ… GoogleͰʮnode.js

    recursive readdirʯͱݕࡧ͢Δͱɺ ͍͔ͭ͘ղܾࡦ΍ϥΠϒϥϦ͕ݟ͔ͭΔ
  8. ੈͷதʹస͕͍ͬͯΔղܾࡦ ͜ΕΒͷ࣮૷ྫͰ͸ɺ͓͓·͔ʹ 1. fs.readdirͰσΟϨΫτϦ௚ԼͷϑΝΠϧͷϦετΛऔಘ͢Δ 2. 1ͷ݁ՌΛϧʔϓ΍ΠςϨʔλͰճ͠ɺ֤ϑΝΠϧͷύεΛfs.statؔ਺ʹ౉͠ɺ fs.StatsΦϒδΣΫτΛಘΔ 3. fs.StatsΦϒδΣΫτʹ͸isDirectory()ϝιου͕͋Γɺ͜ΕͰର৅ͷϑΝΠϧ͕ σΟϨΫτϦ͔Ͳ͏͔൑ఆ͢Δ

    4. 3ͷ݁ՌɺσΟϨΫτϦͩͬͨ৔߹͸ͦͷσΟϨΫτϦʹ͍ͭͯɺ1~3Λߦ͏
 3ͷ݁ՌɺϑΝΠϧͩͬͨ৔߹͸഑ྻfilesʹ௥Ճ͢Δ 5. ϧʔϓ͕ऴΘͬͨΒ഑ྻfilesΛฦ͢ ͱ͍͏ํ๏͕ͱΒΕ͍ͯΔɻ
  9. ੈͷதʹస͕͍ͬͯΔղܾࡦ ΤϥʔϋϯυϦϯά౳͸ແࢹͨ͠࠷খݶͷ࣮૷ྫ ಡΈ΍͢͞ͷͨΊʹಉظ࣮ߦ͞ΕΔؔ਺Λ࢖༻

  10. ੈͷதʹస͕͍ͬͯΔղܾࡦ ݱࡏͷσΟϨΫτϦ௚ԼͷϑΝΠϧҰཡΛऔಘ

  11. ੈͷதʹస͕͍ͬͯΔղܾࡦ fs.statSync()Ͱfs.StatsΦϒδΣΫτΛऔಘ͠ɺ σΟϨΫτϦ͔Ͳ͏͔൑ఆɻ ϑΝΠϧͩͬͨ৔߹͸ϑΝΠϧϦετʹ௥Ճɻ

  12. ੈͷதʹస͕͍ͬͯΔղܾࡦ ࢠσΟϨΫτϦͷϦετΛϧʔϓͰճ͠ɺ ֤ࢠσΟϨΫτϦʹରͯؔ͠਺Λ࠶ؼݺͼग़͢͠Δɻ

  13. ੈͷதʹస͕͍ͬͯΔղܾࡦ ϑΝΠϧͷϦετΛฦ͢

  14. ੈͷதʹస͕͍ͬͯΔղܾࡦ ࣮ߦ݁Ռ

  15. Node.js v10.10Ҏ߱Ͱ͸fs.statΛ࢖Θͣʹॻ͚Δ fs.readdir()ʹ{withFileTypes: true}Λ౉͢ͱɺfiles͸fs.DirentΦϒδΣΫτͷ഑ྻʹͳΔ

  16. Node.js v10.10Ҏ߱Ͱ͸fs.statΛ࢖Θͣʹॻ͚Δ fs.DirentΦϒδΣΫτʹ͸isDirectory()΍ isFile()ͳͲͷϝιου͕࣮૷͞Ε͍ͯΔ ϑΝΠϧ໊͸.nameͰऔಘͰ͖Δ -> ϑΝΠϧγεςϜίʔϧΛ1σΟϨΫτϦʹ͖ͭ 1౓Ͱࡁ·͢͜ͱ͕Ͱ͖Δ https://github.com/nodejs/node/pull/22020

  17. fs.Dirent ΦϒδΣΫτΛ࢖࣮ͬͨ૷ྫ

  18. fs.Dirent ΦϒδΣΫτΛ࢖࣮ͬͨ૷ྫ {withFileTypes: true}Λࢦఆͯ͠ fs.DirentΦϒδΣΫτΛಘΔ dirent.isDirectoryͰ σΟϨΫτϦ͔Ͳ͏͔൑ఆ 1֊૚ʹ͖ͭ1౓ͷϑΝΠϧγεςϜίʔϧ

  19. fs.Dirent ΦϒδΣΫτΛ࢖࣮ͬͨ૷ྫ {withFileTypes: true}Λࢦఆͯ͠ fs.DirentΦϒδΣΫτΛಘΔ dirent.isDirectoryͰ σΟϨΫτϦ͔Ͳ͏͔൑ఆ 1֊૚ʹ͖ͭ1౓ͷϑΝΠϧγεςϜίʔϧ

  20. ඇಉظʹ͢Δ fs.readdir()ʹίʔϧόοΫؔ਺Λ౉ͯ݁͠ՌΛॲཧͯ͠΋͍͍͕ɺ Node.js v10 Ͱ͸ɺExperimental͕ͩfs Promises API͕ఏڙ͞Ε͍ͯΔ (v12Ͱ͸stable)

  21. fs Promises API

  22. fs Promises APIΛ࢖ͬͯॻ͖௚͢

  23. ·ͱΊ •ϥΠϒϥϦΛ࢖Θͳͯ͘΋ɺͦ͜·Ͱଟ͘ͳ͍هड़ྔͰrecursive readdir͸࣮ݱͰ͖Δɻ •Node.js v10.10Ҏ͕߱࢖͑Δ৔߹͸fs.DirentΦϒδΣΫτΛ׆༻ͯ͠
 ϑΝΠϧγεςϜίʔϧͷճ਺ΛݮΒ͢͜ͱ͕Ͱ͖Δɻ •fs Promises APIΛ༻͍Δ͜ͱͰɺϑΝΠϧγεςϜૢ࡞͕PromiseϕʔεͰॻ͚Δɻ •Node.js͸minor

    version updateͰศརͳAPI͕௥Ճ͞ΕΔ͜ͱ͕͋ΔͨΊɺ
 LTSͷ࠷৽Λ࢖͍ଓ͚Δબ୒ࢶ͕औΕΔͱྑ͍ɻ
  24. ิ଍: direntͱ͍͏֓೦ʹ͍ͭͯ dirent͸Linux Programmingʹ͓͍ͯɺϑΝΠϧͷ৘ใΛ࣋ͭߏ଄ମɻ readdirؔ਺͸͜ͷߏ଄ମΛฦ͢ɻ manϖʔδ( http://man7.org/linux/man-pages/man3/readdir.3.html )ΑΓɺglibcͰͷ࣮૷