-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generators): Electron support for desktop development (#46)
- Loading branch information
1 parent
f649879
commit 14b940f
Showing
29 changed files
with
912 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ALL CREDIT TO: Maxime GRIS https://github.com/maximegris | ||
// Allow angular using electron module (native node modules) | ||
const fs = require('fs'); | ||
const f_angular = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js'; | ||
|
||
fs.readFile(f_angular, 'utf8', function (err, data) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
var result = data.replace(/target: "electron-renderer",/g, ''); | ||
var result = result.replace(/target: "web",/g, ''); | ||
var result = result.replace(/return \{/g, 'return {target: "electron-renderer",'); | ||
|
||
fs.writeFile(f_angular, result, 'utf8', function (err) { | ||
if (err) return console.log(err); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ALL CREDIT TO: Maxime GRIS https://github.com/maximegris | ||
// Allow angular using electron module (native node modules) | ||
const fs = require('fs'); | ||
const f_angular = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js'; | ||
|
||
fs.readFile(f_angular, 'utf8', function (err, data) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
var result = data.replace(/target: "electron-renderer",/g, ''); | ||
var result = result.replace(/target: "web",/g, ''); | ||
var result = result.replace(/return \{/g, 'return {target: "web",'); | ||
|
||
fs.writeFile(f_angular, result, 'utf8', function (err) { | ||
if (err) return console.log(err); | ||
}); | ||
}); |
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { app, BrowserWindow, ipcMain, screen } from 'electron'; | ||
import * as path from 'path'; | ||
import * as url from 'url'; | ||
|
||
let serve; | ||
const args = process.argv.slice(1); | ||
serve = args.some(val => val === '--serve'); | ||
|
||
let win: Electron.BrowserWindow = null; | ||
|
||
const getFromEnv = parseInt(process.env.ELECTRON_IS_DEV, 10) === 1; | ||
const isEnvSet = 'ELECTRON_IS_DEV' in process.env; | ||
const debugMode = isEnvSet | ||
? getFromEnv | ||
: process.defaultApp || | ||
/node_modules[\\/]electron[\\/]/.test(process.execPath); | ||
|
||
/** | ||
* Electron window settings | ||
*/ | ||
const mainWindowSettings: Electron.BrowserWindowConstructorOptions = { | ||
frame: true, | ||
resizable: true, | ||
focusable: true, | ||
fullscreenable: true, | ||
kiosk: false, | ||
webPreferences: { | ||
devTools: debugMode | ||
} | ||
}; | ||
|
||
/** | ||
* Hooks for electron main process | ||
*/ | ||
function initMainListener() { | ||
ipcMain.on('ELECTRON_BRIDGE_HOST', (event, msg) => { | ||
console.log('msg received', msg); | ||
if (msg === 'ping') { | ||
event.sender.send('ELECTRON_BRIDGE_CLIENT', 'pong'); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Create main window presentation | ||
*/ | ||
function createWindow() { | ||
const sizes = screen.getPrimaryDisplay().workAreaSize; | ||
|
||
if (debugMode) { | ||
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'; | ||
|
||
mainWindowSettings.width = 800; | ||
mainWindowSettings.height = 600; | ||
} else { | ||
mainWindowSettings.width = sizes.width; | ||
mainWindowSettings.height = sizes.height; | ||
mainWindowSettings.x = 0; | ||
mainWindowSettings.y = 0; | ||
} | ||
|
||
win = new BrowserWindow(mainWindowSettings); | ||
|
||
let launchPath; | ||
if (serve) { | ||
require('electron-reload')(__dirname, { | ||
electron: require(`${__dirname}/../../../node_modules/electron`) | ||
}); | ||
launchPath = 'http://localhost:4200'; | ||
win.loadURL(launchPath); | ||
} else { | ||
launchPath = url.format({ | ||
pathname: path.join(__dirname, 'index.html'), | ||
protocol: 'file:', | ||
slashes: true | ||
}); | ||
win.loadURL(launchPath); | ||
} | ||
|
||
console.log('launched electron with:', launchPath); | ||
|
||
win.on('closed', () => { | ||
// 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. | ||
win = null; | ||
}); | ||
|
||
initMainListener(); | ||
|
||
if (debugMode) { | ||
// Open the DevTools. | ||
win.webContents.openDevTools(); | ||
// client.create(applicationRef); | ||
} | ||
} | ||
|
||
try { | ||
app.on('ready', createWindow); | ||
|
||
app.on('window-all-closed', () => { | ||
if (process.platform !== 'darwin') { | ||
app.quit(); | ||
} | ||
}); | ||
|
||
app.on('activate', () => { | ||
// On macOS 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 (win === null) { | ||
createWindow(); | ||
} | ||
}); | ||
} catch (err) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "electron-<%= utils.sanitize(name) %>", | ||
"version": "1.0.0", | ||
"description": "<%= utils.classify(name) %> description.", | ||
"main": "index.js", | ||
"author": { | ||
"name": "Your name", | ||
"email": "name@company.com" | ||
}, | ||
"homepage": "https://nstudio.io/xplat", | ||
"repository": { | ||
"url": "https://github.com/nstudio/xplat" | ||
}, | ||
"license": "MIT", | ||
"build": { | ||
"appId": "com.company.<%= utils.classify(name) %>", | ||
"productName": "<%= utils.classify(name) %>", | ||
"copyright": "Copyright © 2018-2019 <%= npmScope %>", | ||
"asar": false, | ||
"npmRebuild": false, | ||
"directories": { | ||
"buildResources": "icons", | ||
"output": "../electron-<%= utils.sanitize(name) %>-packages" | ||
}, | ||
"mac": { | ||
"category": "public.app-category.developer-tools", | ||
"icon": "icon.png" | ||
}, | ||
"win": { | ||
"target": "nsis", | ||
"icon": "icon.ico" | ||
}, | ||
"linux": { | ||
"icon": "icon.png", | ||
"target": [ | ||
"AppImage", | ||
"deb", | ||
"tar.gz" | ||
], | ||
"synopsis": "<%= utils.classify(name) %>", | ||
"category": "Development" | ||
}, | ||
"nsis": { | ||
"createDesktopShortcut": "always", | ||
"installerIcon": "icon.ico", | ||
"artifactName": "<%= utils.classify(name) %>-Setup-${version}.${ext}" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"compilerOptions": { | ||
"sourceMap": true, | ||
"declaration": false, | ||
"moduleResolution": "node", | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"esModuleInterop": true, | ||
"target": "es5", | ||
"typeRoots": [ | ||
"../../node_modules/@types" | ||
], | ||
"lib": [ | ||
"es2017", | ||
"es2016", | ||
"es2015", | ||
"dom" | ||
] | ||
}, | ||
"include": [ | ||
"src/index.ts" | ||
] | ||
} |
Oops, something went wrong.