« 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
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/crawl-http.js (renamed from src/crawl-recursive.js)4
-rw-r--r--src/crawl-local.js41
2 files changed, 43 insertions, 2 deletions
diff --git a/src/crawl-recursive.js b/src/crawl-http.js
index 2656279..189ba28 100755
--- a/src/crawl-recursive.js
+++ b/src/crawl-http.js
@@ -76,8 +76,8 @@ function getHTMLLinks(text) {
 }
 
 if (process.argv.length === 2) {
-  console.log("Usage: http-music-crawl-recursive http://.../example/path/")
-  console.log("..or, npm run crawl-recursive -- http://...")
+  console.log("Usage: http-music-crawl-http http://.../example/path/")
+  console.log("..or, npm run crawl-http -- http://.../example/path/")
 } else {
   let url = process.argv[2]
 
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))
+}