diff --git a/.gitignore b/.gitignore index d5f19d8..093dbff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules package-lock.json +test-library diff --git a/src/main.js b/src/main.js index 55a49be..c5a5632 100644 --- a/src/main.js +++ b/src/main.js @@ -59,18 +59,19 @@ app.on('window-all-closed', () => { app.quit(); }); -ipcMain.handle('readMusicLibrary', function(event, path) { - let files = fs.promises.readdir(path) - .then(function(files) { - if (files) { - return files; - } else { - return -1; - } - }) - .catch(err => { - console.log(err); - }); +ipcMain.handle('readDir', async function(event, path) { + const options = { + "withFileTypes" : true + }; + + let files = await fs.promises.readdir(path, options); + + // This needs to be here because the way Electron passes objects doesn't pass methods + for (const i in files) { + let file = files[i]; + + files[i].isDirectory = file.isDirectory(); + } return files; }); diff --git a/src/preload.js b/src/preload.js index 540e870..8e6c1a1 100644 --- a/src/preload.js +++ b/src/preload.js @@ -18,7 +18,7 @@ contextBridge.exposeInMainWorld('api', { getAudioInfo: function(url) { return ipcRenderer.invoke('getAudioInfo', url); }, - readMusicLibrary: function(path) { - return ipcRenderer.invoke('readMusicLibrary', path); + readDir: function(path) { + return ipcRenderer.invoke('readDir', path); } }); diff --git a/src/renderer.js b/src/renderer.js index ffa9e8d..86f281a 100644 --- a/src/renderer.js +++ b/src/renderer.js @@ -9,11 +9,18 @@ console.log('test'); // const path = 'C:\\Users\\JISAUAY\\Documents\\misc\\sspm\\ChangelingForest.mp3'; const libPath = 'C:/Users/JISAUAY/Documents/misc/sspm/test-library/'; -let libEntryPathes; +let library; let currentlyPlaying; let currentlyPlayingPath; +class Dir { + constructor(path, children) { + this.path = path; + this.children = children; + } +} + // const player = new Audio(url); // let info = window.api.getAudioInfo(url); @@ -21,32 +28,63 @@ let currentlyPlayingPath; // const info = await window.api.getAudioInfo(path); // console.log(info); // }; -async function getLibraryFiles() { - return await window.api.readMusicLibrary(libPath); +async function readDir(path) { + return await window.api.readDir(path); } +async function readChildren(path) { + let dirChildren = await readDir(path); + + let children = []; + for (const i in dirChildren) { + let child = dirChildren[i]; + + if (child.isDirectory) { + children.push(await readChildren(child.path)); + } else { + children.push(child); + } + } + + return children; +} // Load Library document.addEventListener('DOMContentLoaded', async () => { const libContentDisplay = document.getElementById('libContents'); - libEntryPathes = await getLibraryFiles(); + libEntryPathes = await readDir(libPath); + + let dirChildren = await readChildren(libPath); + + let dirTest = new Dir(libPath, dirChildren); + + console.log(dirTest); + console.log(libEntryPathes); for (const file_i in libEntryPathes) { - libContentDisplay.innerHTML += `