I have setup electron for my angular 8 project and I can use electron now to build desktop app from my angular 8 project. Here is the structure of my project:
+ dist
+ e2e
+ electron => which includes main.ts & tsconfig.json & dist, and inside dist is my main.js
+ node_modules
+ src
Here is my main.ts file
import { app, BrowserWindow, ipcMain } from 'electron'
import * as path from 'path'
import * as url from 'url'
import * as fs from "fs";
let win: BrowserWindow
app.on('ready', createWindow)
app.on('activate', () => {
if (win === null) {
createWindow()
}
});
function createWindow() {
win = new BrowserWindow({ fullscreen: true })
win.loadURL(
url.format({
pathname: path.join(__dirname, `/panelfinal/index.html`),
protocol: 'file:',
slashes: true,
})
)
win.webContents.openDevTools()
win.on('closed', () => {
win = null
})
}
ipcMain.on('getFiles', (event, arg) => {
console.log(event + "this is event");
console.log(arg + " this is arg");
const files = fs.readdirSync(__dirname);
console.log(files + " Electron");
win.webContents.send('getFilesResponse', files)
})
I want to read files from file system with electron and I have created a electron service file and from there I try to make call to Ipc
to access the file system. Here is my electron service:
export class ElectronService {
public ipc: IpcRenderer
constructor(private snackBar: SnackbarService) {
if ((<any>window).require) {
try {
this.ipc = (<any>window).require('electron').ipcRenderer
} catch (error) {
throw error
}
} else {
console.warn('not for this platform')
}
}
async getFiles(plaques: string[]) {
new Promise<string[]>((resolve, reject) => {
this.ipc.once("getFilesResponse", (event, arg) => {
resolve(arg);
});
this.ipc.send("getFiles", plaques);
});
}
}
when I call the getFiles()
method in my electron I get this error:
Cannot read property 'once' of undefined
Any ideas why?