Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
60 changes: 60 additions & 0 deletions __tests__/loadExtensions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const path = require('path');
const loadExtensions = require('../loadExtensions');

describe('loadExtensions', () => {
let logger;

beforeEach(() => {
logger = {
log: jest.fn(),
error: jest.fn()
};
});

it('executes only JavaScript files inside the extensions directory', () => {
const files = ['alpha.js', 'beta.txt', 'gamma.js'];
const fsMock = {
existsSync: jest.fn(() => true),
readdirSync: jest.fn(() => files),
readFile: jest.fn((filePath, encoding, callback) => {
callback(null, 'console.log("test");');
})
};
const executeJavaScript = jest.fn(() => Promise.resolve());

loadExtensions({ webContents: { executeJavaScript } }, {
extensionsPath: '/fake/extensions',
fsModule: fsMock,
console: logger
});

expect(fsMock.readFile).toHaveBeenCalledTimes(2);
const readFilePaths = fsMock.readFile.mock.calls.map(call => call[0]);
expect(readFilePaths).toEqual([
path.join('/fake/extensions', 'alpha.js'),
path.join('/fake/extensions', 'gamma.js')
]);
expect(executeJavaScript).toHaveBeenCalledTimes(2);
});

it('logs read errors without throwing', () => {
const error = new Error('Failure');
const fsMock = {
existsSync: jest.fn(() => true),
readdirSync: jest.fn(() => ['broken.js']),
readFile: jest.fn((filePath, encoding, callback) => {
callback(error);
})
};
const executeJavaScript = jest.fn(() => Promise.resolve());

expect(() => loadExtensions({ webContents: { executeJavaScript } }, {
extensionsPath: '/fake/extensions',
fsModule: fsMock,
console: logger
})).not.toThrow();

expect(logger.error).toHaveBeenCalledWith('Error reading broken.js:', error);
expect(executeJavaScript).not.toHaveBeenCalled();
});
});
42 changes: 42 additions & 0 deletions loadExtensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const path = require('path');
const defaultFs = require('fs');

function loadExtensions(mainWindow, options = {}) {
const {
extensionsPath = path.join(__dirname, 'extensions'),
fsModule = defaultFs,
console: logger = console
} = options;

if (!mainWindow || !mainWindow.webContents || typeof mainWindow.webContents.executeJavaScript !== 'function') {
logger.error('Invalid mainWindow provided to loadExtensions.');
return;
}

if (!fsModule.existsSync(extensionsPath)) {
logger.error('Extensions folder not found!');
return;
}

fsModule.readdirSync(extensionsPath).forEach(file => {
if (!file.endsWith('.js')) {
return;
}

const scriptPath = path.join(extensionsPath, file);
logger.log(`Loading extension: ${file}`);

fsModule.readFile(scriptPath, 'utf8', (err, script) => {
if (err) {
logger.error(`Error reading ${file}:`, err);
return;
}

mainWindow.webContents.executeJavaScript(script)
.then(() => logger.log(`Successfully loaded: ${file}`))
.catch(error => logger.error(`Error executing ${file}:`, error));
});
});
}

module.exports = loadExtensions;
32 changes: 3 additions & 29 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
*/

const { app, BrowserWindow } = require('electron');
const path = require('path');
const fs = require('fs');
const path = require('path');
const loadExtensions = require('./loadExtensions');

let mainWindow;

Expand All @@ -31,37 +31,11 @@ app.whenReady().then(() => {

mainWindow.webContents.once('dom-ready', () => {
console.log("🔄 Loading extensions...");
loadExtensions();
loadExtensions(mainWindow);
});
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});

function loadExtensions() {
const EXTENSIONS_FOLDER = path.join(__dirname, 'extensions');

if (!fs.existsSync(EXTENSIONS_FOLDER)) {
console.error('Extensions folder not found!');
return;
}

fs.readdirSync(EXTENSIONS_FOLDER).forEach(file => {
if (file.endsWith('.js')) {
const scriptPath = path.join(EXTENSIONS_FOLDER, file);
console.log(`Loading extension: ${file}`);

fs.readFile(scriptPath, 'utf8', (err, script) => {
if (err) {
console.error(`Error reading ${file}:`, err);
return;
}

mainWindow.webContents.executeJavaScript(script)
.then(() => console.log(`Successfully loaded: ${file}`))
.catch(err => console.error(`Error executing ${file}:`, err));
});
}
});
}
Loading