Skip to content

Commit

Permalink
quick working example
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcsapo committed Jul 5, 2017
1 parent ae3262f commit cc30a79
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ typings/

# dotenv environment variables file
.env

temp
.DS_Store
package-lock.json
release
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# local-npm-daemon
An electron application to run local-npm

> An electron application to run local-npm
## What is this?

This is an electron application to quickly startup [local-npm](http://github.com/local-npm/local-npm)

![example](./assets/example.png)

## Download

> coming soon
Binary file added assets/cloudTemplate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo.icns
Binary file not shown.
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Sample Menubar App</title>
<script charset="utf-8">require('./index')</script>
</head>
<body>
<center><h1>local-npm</h1></center>
</body>
</html>
Empty file added index.js
Empty file.
72 changes: 72 additions & 0 deletions log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict'

/**
* Redirects default log functions
*
* On Mac => ~/Library/Logs/[AppName-WithoutSpaces|Electron].log
* On Win => C:\Users\[UserName]\AppData\Local\[AppName-WithoutSpaces|Electron].log
* On Linux => ~/.[AppName-WithoutSpaces|Electron].log
*/

const os = require('os')
const fs = require('fs')
const path = require('path')
const isDev = require('electron-is-dev')

// Set default output streams (STDOUT/STDERR) and an empty log file path
var output = process.stdout
var errorOutput = process.stderr
var logFile = null

module.exports = (logFileName) => {
switch (os.platform()) {
case 'darwin':
logFile = path.join(os.homedir(), 'Library/Logs', (logFileName || 'Electron').replace(' ', '') + '.log')
break
case 'win32':
logFile = path.join(os.homedir(), 'AppData', 'Local', (logFileName || 'Electron').replace(' ', '') + '.log')
break
case 'linux':
logFile = path.join(os.homedir(), '.' + (logFileName || 'Electron').replace(' ', '') + '.log')
break
default:
// Others: leave untouched
}

// If we are in production and a log file is defined we redirect logs to that file
if (!isDev && logFile) {
output = fs.createWriteStream(logFile)
errorOutput = fs.createWriteStream(logFile)
}

// Create common logger
const logger = new console.Console(output, errorOutput)

// Override default log utilities
console.log = function () {
arguments[0] = new Date().toISOString() + ' - ' + arguments[0]
logger.log.apply(null, arguments)
}

console.debug = function () {
arguments[0] = new Date().toISOString() + ' - <Debug> ' + arguments[0]
if (isDev || (global.appSettings && global.appSettings.debug)) {
logger.log.apply(null, arguments)
}
}

console.info = function () {
arguments[0] = new Date().toISOString() + ' - <Info> ' + arguments[0]
logger.log.apply(null, arguments)
}

console.warn = function () {
arguments[0] = new Date().toISOString() + ' - <Warning> ' + arguments[0]
logger.log.apply(null, arguments)
}

console.error = function () {
arguments[0] = new Date().toISOString() + ' - <Error> ' + arguments[0]
logger.log.apply(null, arguments)
}
}
101 changes: 101 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const { app, BrowserWindow, ipcMain, Tray } = require('electron');

require('electron-debug')({enabled: true, showDevTools: true});
require('./log')('local-npm-daemon');

const path = require('path')
const spawn = require('child_process').spawn;
const npm = require('local-npm');

const assetsDir = path.join(__dirname, 'assets');
const tempDir = path.join(__dirname, 'temp');

const server = npm({
directory: tempDir,
port: 5678,
pouchPort: 3040,
logLevel: 'debug',
remote: 'https://registry.npmjs.org',
remoteSkim: 'https://replicate.npmjs.com',
url: 'http://127.0.0.1:5080'
}, () => {
console.log('listening!');
});

let tray = undefined
let window = undefined

// This method is called once Electron is ready to run our code
// It is effectively the main method of our Electron app
app.on('ready', () => {
// Setup the menubar with an icon
tray = new Tray(path.resolve(__dirname, './assets/cloudTemplate.png'));

// Add a click handler so that when the user clicks on the menubar icon, it shows
// our popup window
tray.on('click', function(event) {
toggleWindow()

// Show devtools when command clicked
if (window.isVisible() && process.defaultApp && event.metaKey) {
window.openDevTools({mode: 'detach'})
}
})

// Make the popup window for the menubar
window = new BrowserWindow({
width: 500,
height: 350,
show: false,
frame: false,
resizable: false,
})

// Only close the window on blur if dev tools isn't opened
window.on('blur', () => {
if(!window.webContents.isDevToolsOpened()) {
window.hide()
}
})
})

const toggleWindow = () => {
if (window.isVisible()) {
window.hide()
} else {
showWindow()
}
}

const showWindow = () => {
window.loadURL(`http://127.0.0.1:5678/_browse`);

const trayPos = tray.getBounds()
const windowPos = window.getBounds()
let x, y = 0
if (process.platform == 'darwin') {
x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2))
y = Math.round(trayPos.y + trayPos.height)
} else {
x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2))
y = Math.round(trayPos.y + trayPos.height * 10)
}


window.setPosition(x, y, false)
window.show()
window.focus()
}

ipcMain.on('show-window', () => {
showWindow()
})

app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
server.close();
app.quit()
}
})
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "local-npm-daemon",
"version": "0.0.1",
"description": "An electron application to run local-npm",
"main": "main.js",
"scripts": {
"start": "electron .",
"build": "electron-packager . --overwrite --platform=linux,darwin --arch=x64 --icon=./assets/logo.icns --prune=true --out=release",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/gabrielcsapo/local-npm-daemon.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/gabrielcsapo/local-npm-daemon/issues"
},
"homepage": "https://github.com/gabrielcsapo/local-npm-daemon#readme",
"devDependencies": {
"devtron": "^1.4.0",
"electron-packager": "^8.7.2",
"electron-prebuilt": "^1.4.13"
},
"dependencies": {
"electron-debug": "^1.2.0",
"electron-is-dev": "^0.2.0",
"local-npm": "^2.1.0"
}
}

0 comments on commit cc30a79

Please sign in to comment.