« get me outta code hell

Local downloader - 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:
authorliam4 <towerofnix@gmail.com>2017-06-04 10:26:54 -0300
committerliam4 <towerofnix@gmail.com>2017-06-04 10:26:59 -0300
commit9fcf30b57a7a8f0fc86f1187d2cad72e8eaaa37e (patch)
treea5ddfda168eabc4ac4bff3430ae45fbb3325bc2e /src
parent507bd7b0c0f185a87b4213b5c95b3b3859fe9528 (diff)
Local downloader
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))
+}