« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/crawl-local.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/crawl-local.js')
-rw-r--r--src/crawl-local.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/crawl-local.js b/src/crawl-local.js
new file mode 100644
index 0000000..d9a9a70
--- /dev/null
+++ b/src/crawl-local.js
@@ -0,0 +1,41 @@
+#!/usr/bin/env node
+
+'use strict'
+
+const fs = require('fs')
+const path = require('path')
+
+const { promisify } = require('util')
+const readDir = promisify(fs.readdir)
+const stat = promisify(fs.stat)
+
+function crawl(dirPath) {
+  return readDir(dirPath).then(
+    res => Promise.all(res.map(item => {
+      const itemPath = path.join(dirPath, item)
+
+      return stat(itemPath).then(stats => {
+        if (stats.isDirectory()) {
+          return crawl(itemPath).then(contents => {
+            const group = [item, contents]
+            return group
+          })
+        } else if (stats.isFile()) {
+          const track = [item, itemPath]
+          return track
+        }
+      })
+    })
+  ))
+}
+
+if (process.argv.length === 2) {
+  console.log("Usage: http-music-crawl-local /example/path..")
+  console.log("..or, npm run crawl-local /example/path")
+} else {
+  const path = process.argv[2]
+
+  crawl(path)
+    .then(res => console.log(JSON.stringify(res, null, 2)))
+    .catch(err => console.error(err))
+}