Skip to content

Commit

Permalink
insights-desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Mar 5, 2020
1 parent 741c294 commit 290525b
Show file tree
Hide file tree
Showing 8 changed files with 811 additions and 221 deletions.
16 changes: 16 additions & 0 deletions packages/insights-desktop/bin/insights-desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env electron

const path = require('path')
const program = require('commander')
const root = path.join(__dirname, '..')
const pkg = require(path.join(root, 'package.json'))

const startInsightsDesktop = require('../src/insights-desktop')

program.version(pkg.version)
.usage('<command> [options]')
.description('Start Insights in desktop mode via Electron')

program.parse(process.argv)

startInsightsDesktop()
2 changes: 2 additions & 0 deletions packages/insights-desktop/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const startInsightsDesktop = require('./src/insights-desktop')
startInsightsDesktop()
24 changes: 24 additions & 0 deletions packages/insights-desktop/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "insights-desktop",
"description": "insights electron app",
"version": "0.0.32",
"author": "Marius Andra <[email protected]>",
"main": "main.js",
"dependencies": {
"get-port": "^5.1.1"
},
"scripts": {
"start": "electron ."
},
"devDependencies": {
},
"bin": {
"electron": "^8.0.3",
"insights-desktop": "./bin/insights-desktop"
},
"files": [
"bin",
"src",
"main.js"
]
}
90 changes: 90 additions & 0 deletions packages/insights-desktop/src/insights-desktop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

const fs = require('fs')
const path = require('path')
const getPort = require('get-port')

// 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

const configFolder = path.join((app || electron.remote.app).getPath('userData'), 'Insights Config');

function createWindow (url) {
// Create the browser window.
mainWindow = new BrowserWindow({width: 1200, height: 800})

// and load the index.html of the app.
mainWindow.loadURL(url)

// 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
})
}

function startElectron (url) {
// 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.

// 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(url)
}
})

app.on('ready', () => createWindow(url))
}

function startInsightsAndElectron () {
const startInsights = require('insights/app/start')

startInsights({
host: process.env.INSIGHTS_HOST,
port: process.env.INSIGHTS_PORT,
onListening: (app, server) => {
startElectron(process.env.INSIGHTS_PUBLIC_URL)
}
})
}

async function startInsightsDesktop () {
console.log(`Config in ${configFolder}`)
process.env.NODE_ENV = 'production'
process.env.NODE_CONFIG_DIR = configFolder;

if (!fs.existsSync(configFolder)) {
const initInsights = require('insights/app/init')
await initInsights({ dev: false, login: false, configFolder, exitWhenDone: false })
}

process.env.INSIGHTS_HOST = '127.0.0.1'
process.env.INSIGHTS_PORT = await getPort()
process.env.INSIGHTS_PUBLIC_URL = `http://${process.env.INSIGHTS_HOST}:${process.env.INSIGHTS_PORT}`

startInsightsAndElectron()
}

module.exports = startInsightsDesktop
13 changes: 9 additions & 4 deletions packages/insights/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ const createFolder = require('./lib/create-folder')
const { promisify } = require('util');
const exec = promisify(require('child_process').exec)

async function initInsights ({ dev = false, login = undefined }) {
async function initInsights ({
dev = false,
login = undefined,
configFolder = path.join(process.cwd(), '.insights'),
exitWhenDone = true
}) {
console.log(`Welcome to Insights v${pkg.version}!`)
console.log('')

const configFolder = path.join(process.cwd(), '.insights')

if (fs.existsSync(configFolder)) {
console.error('!! Fatal Error! The folder ".insights" already exists. ')
console.error(' Could not complete init.')
Expand Down Expand Up @@ -89,7 +92,9 @@ async function initInsights ({ dev = false, login = undefined }) {
console.log('')
console.log('Success! Run "insights start" to start the server!')

process.exit(0)
if (exitWhenDone) {
process.exit(0)
}
}

module.exports = initInsights
4 changes: 3 additions & 1 deletion packages/insights/app/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = function startInsights({

apiPath = process.env.INSIGHTS_API_PATH || `/api`,
// TODO: no way to configure socketPath yet, must stay at "/socket.io"
socketPath = process.env.INSIGHTS_SOCKET_PATH || '/socket.io'
socketPath = process.env.INSIGHTS_SOCKET_PATH || '/socket.io',
onListening = undefined
} = {}) {
console.log({
host,
Expand Down Expand Up @@ -62,6 +63,7 @@ module.exports = function startInsights({

server.on('listening', () => {
console.info(`--> ${publicUrl}`)
onListening && onListening(app, server)
})

return app
Expand Down
1 change: 1 addition & 0 deletions packages/insights/bin/insights
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ program
.command('start', 'start insights on port 8000')
.command('createsecret', 'create a new authentication secret')
.command('createsuperuser', 'create a new admin user')
.command('desktop', 'start desktop app (run `npm install -g insights-desktop` first)')

program.parse(process.argv)
Loading

0 comments on commit 290525b

Please sign in to comment.