« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--package.json3
-rw-r--r--src/loop-play.js13
-rw-r--r--yarn.lock26
4 files changed, 34 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index a165968..b75c28e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,3 @@
-/.*.wav
-/.temp-track
 .DS_Store
 node_modules
 playlist.json
diff --git a/package.json b/package.json
index 5efd88e..804df46 100644
--- a/package.json
+++ b/package.json
@@ -5,6 +5,7 @@
   "dependencies": {
     "cheerio": "^1.0.0-rc.1",
     "node-fetch": "^1.7.0",
-    "sanitize-filename": "^1.6.1"
+    "sanitize-filename": "^1.6.1",
+    "tempy": "^0.1.0"
   }
 }
diff --git a/src/loop-play.js b/src/loop-play.js
index 48b5790..4d8dd0a 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -1,6 +1,7 @@
 'use strict'
 
 const fs = require('fs')
+const tempy = require('tempy')
 
 const { spawn } = require('child_process')
 const { promisify } = require('util')
@@ -9,7 +10,6 @@ const sanitize = require('sanitize-filename')
 const promisifyProcess = require('./promisify-process')
 
 const writeFile = promisify(fs.writeFile)
-const unlink = promisify(fs.unlink)
 
 module.exports = async function loopPlay(fn) {
   // Looping play function. Takes one argument, the "pick" function,
@@ -28,14 +28,16 @@ module.exports = async function loopPlay(fn) {
     const [ title, href ] = picked
     console.log(`Downloading ${title}..\n${href}`)
 
-    const wavFile = `.${sanitize(title)}.wav`
+    const wavDir = tempy.directory()
+    const wavFile = wavDir + `.${sanitize(title)}.wav`
+    const downloadFile = tempy.file()
 
     const res = await fetch(href)
     const buffer = await res.buffer()
-    await writeFile('./.temp-track', buffer)
+    await writeFile(downloadFile, buffer)
 
     try {
-      await convert('./.temp-track', wavFile)
+      await convert(downloadFile, wavFile)
     } catch(err) {
       console.warn("Failed to convert " + title)
       console.warn("Selecting a new track\n")
@@ -43,8 +45,6 @@ module.exports = async function loopPlay(fn) {
       return await downloadNext()
     }
 
-    await unlink('./.temp-track')
-
     return wavFile
   }
 
@@ -53,7 +53,6 @@ module.exports = async function loopPlay(fn) {
   while (wavFile) {
     const nextPromise = downloadNext()
     await playFile(wavFile)
-    await unlink(wavFile)
     wavFile = await nextPromise
   }
 }
diff --git a/yarn.lock b/yarn.lock
index c18f89b..ae001ba 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -27,6 +27,10 @@ core-util-is@~1.0.0:
   version "1.0.2"
   resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
 
+crypto-random-string@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
+
 css-select@~1.2.0:
   version "1.2.0"
   resolved "https://registry.yarnpkg.com/css-select/-/css-select-1.2.0.tgz#2b3a110539c5355f1cd8d314623e870b121ec858"
@@ -128,6 +132,10 @@ parse5@^3.0.1:
   dependencies:
     "@types/node" "^6.0.46"
 
+pify@^2.3.0:
+  version "2.3.0"
+  resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
+
 process-nextick-args@~1.0.6:
   version "1.0.7"
   resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3"
@@ -160,12 +168,30 @@ string_decoder@~1.0.0:
   dependencies:
     safe-buffer "^5.0.1"
 
+temp-dir@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-1.0.0.tgz#0a7c0ea26d3a39afa7e0ebea9c1fc0bc4daa011d"
+
+tempy:
+  version "0.1.0"
+  resolved "https://registry.yarnpkg.com/tempy/-/tempy-0.1.0.tgz#8527413cd07100834fcc9cbb8242be95ba0e1fee"
+  dependencies:
+    pify "^2.3.0"
+    temp-dir "^1.0.0"
+    unique-string "^1.0.0"
+
 truncate-utf8-bytes@^1.0.0:
   version "1.0.2"
   resolved "https://registry.yarnpkg.com/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz#405923909592d56f78a5818434b0b78489ca5f2b"
   dependencies:
     utf8-byte-length "^1.0.1"
 
+unique-string@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a"
+  dependencies:
+    crypto-random-string "^1.0.0"
+
 utf8-byte-length@^1.0.1:
   version "1.0.4"
   resolved "https://registry.yarnpkg.com/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz#f45f150c4c66eee968186505ab93fcbb8ad6bf61"