From 313ba9dedf2ec261fed5b884441018d132cc80f1 Mon Sep 17 00:00:00 2001 From: empathicqubit Date: Thu, 21 Jun 2018 00:33:26 -0400 Subject: [PATCH] Automatically create .env file on first run. --- config.js | 2 +- index.js | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/config.js b/config.js index fa9cf06..7afb4d3 100644 --- a/config.js +++ b/config.js @@ -1,3 +1,3 @@ module.exports = { - files: JSON.parse(process.env.AUDIO_FILES_ARRAY), + files: JSON.parse(process.env.AUDIO_FILES_ARRAY || "[]"), }; diff --git a/index.js b/index.js index 7499f99..4e4cc7b 100755 --- a/index.js +++ b/index.js @@ -4,10 +4,14 @@ require('dotenv').config(); const express = require('express'); const q = require('q'); const fs = require('fs'); +const stat = q.denodeify(fs.stat); +const writeFile = q.denodeify(fs.writeFile); +const readdir = q.denodeify(fs.readdir); const cors = require('cors'); const execFile = q.denodeify(require('child_process').execFile); const musicMetadata = require('music-metadata'); const path = require('path'); + const ws = require('./ws'); const config = require('./config.js'); @@ -47,8 +51,26 @@ const getDuration = (filename) => { }); }; +const audioDir = path.join(__dirname, 'frontend/public/audio'); + q.resolve() - .then(() => getDuration(path.join(__dirname + '/frontend/public/audio', files[0]))) + .then(() => { + const envPath = path.join(__dirname, '.env'); + return stat(envPath) + .catch(() => { + return readdir(audioDir) + .then(files => { + if(files.length) { + const contents = ` +AUDIO_FILES_ARRAY=${JSON.stringify(files)} +`; + + return writeFile(envPath, contents, 'utf8'); + } + }); + }) + }) + .then(() => getDuration(path.join(audioDir, files[0]))) .then(audioLength => { let startTime = Date.now(); @@ -56,6 +78,5 @@ q.resolve() }) .catch(e => { console.error(e); - metaFile.close(); process.exit(1); });