Skip to content

Instantly share code, notes, and snippets.

@josephj
Created February 7, 2020 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephj/21cc11479ab092896176c12efcc90545 to your computer and use it in GitHub Desktop.
Save josephj/21cc11479ab092896176c12efcc90545 to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File Listing</title>
</head>
<body>
<h1>File Listing</h1>
<section>
<input
type="text" name="dir-path" id="dir-path" value=""
placeholder="/Volumes/Joseph Sony">
(e.g. /Volumes/Joseph Sony)
<br/>
<button id="btn-list-files">List Files</button>
</section>
<ul id="usb-list"></ul>
<ul id="file-list"></ul>
<script>require('./renderer.js');</script>
</body>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
const fs = require('fs');
const os = require('os');
function listFiles(dirPath = os.homedir()) {
if (!fs.existsSync(dirPath)) {
console.log('Folder not existing');
return;
}
fs.readdir(dirPath, (err, files) => {
if (err) {
console.log('Unable to scan directory: ' + err);
return;
}
const list = document.querySelector('#file-list');
let html = [];;
files.forEach(name => {
html.push(`<li>${name}</li>`);
});
list.innerHTML = html.join('');
});
}
const pathInputEl = document.querySelector('#dir-path');
pathInputEl.value = os.homedir();
const btnEl = document.getElementById('btn-list-files')
btnEl.addEventListener('click', () => listFiles(pathInputEl.value));
// const usbDetect = require('usb-detection');
// usbDetect.startMonitoring();
// const usbListEl = document.querySelector('#usb-list');
// const updateUsbList = () => {
// usbDetect.find((err, devices) => {
// console.log(devices);
// });
// };
// usbDetect.on('add', updateUsbList);
// usbDetect.on('remove', updateUsbList);
// updateUsbList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment