add linting (#27)

This commit is contained in:
Lynn 2017-03-03 16:07:48 -08:00 committed by GitHub
parent 40e6b173af
commit 71387c1dce
12 changed files with 254 additions and 210 deletions

6
.eslintignore Normal file
View File

@ -0,0 +1,6 @@
build
config
coverage
dist
installer
node_modules

30
.eslintrc Normal file
View File

@ -0,0 +1,30 @@
{
"extends": [
"airbnb-base/rules/best-practices",
"airbnb-base/rules/errors",
"airbnb-base/rules/node",
"airbnb-base/rules/strict",
"airbnb-base/rules/variables"
],
"rules": {
"comma-dangle": 0,
"vars-on-top": 0,
"one-var": 0,
"indent": [ 2, 4, {
"SwitchCase": 1
} ],
"space-in-parens": 0,
"func-names" : 0,
"eol-last": 0,
"no-use-before-define": 0,
"strict": 0
},
"env": {
"browser": true,
"node": true,
"es6": true
},
"globals": {
"SYM_API": true
}
}

View File

@ -21,8 +21,8 @@ electron.app.on('certificate-error', function(event, webContents, url, error,
return; return;
} }
let browserWin = electron.BrowserWindow.fromWebContents(webContents); const browserWin = electron.BrowserWindow.fromWebContents(webContents);
var buttonId = electron.dialog.showMessageBox(browserWin, { const buttonId = electron.dialog.showMessageBox(browserWin, {
type: 'warning', type: 'warning',
buttons: [ 'Allow', 'Deny', 'Ignore All' ], buttons: [ 'Allow', 'Deny', 'Ignore All' ],
defaultId: 1, defaultId: 1,

View File

@ -3,11 +3,13 @@
const electron = require('electron'); const electron = require('electron');
const app = electron.app; const app = electron.app;
const nodeURL = require('url'); const nodeURL = require('url');
const squirrelStartup = require('electron-squirrel-startup');
const getConfig = require('./getConfig.js'); const getConfig = require('./getConfig.js');
const { isMac } = require('./utils.js'); const { isMac } = require('./utils.js');
// exit early for squirrel installer // exit early for squirrel installer
if (require('electron-squirrel-startup')) { if (squirrelStartup) {
return; return;
} }

View File

@ -5,7 +5,6 @@
* from the renderer process. * from the renderer process.
*/ */
const electron = require('electron'); const electron = require('electron');
const path = require('path');
const windowMgr = require('./windowMgr.js'); const windowMgr = require('./windowMgr.js');
const log = require('./log.js'); const log = require('./log.js');
@ -18,8 +17,8 @@ const log = require('./log.js');
function isValidWindow(event) { function isValidWindow(event) {
if (event && event.sender) { if (event && event.sender) {
// validate that event sender is from window we created // validate that event sender is from window we created
let browserWin = electron.BrowserWindow.fromWebContents(event.sender); const browserWin = electron.BrowserWindow.fromWebContents(event.sender);
let winKey = event.sender.browserWindowOptions && const winKey = event.sender.browserWindowOptions &&
event.sender.browserWindowOptions.webPreferences && event.sender.browserWindowOptions.webPreferences &&
event.sender.browserWindowOptions.webPreferences.winKey; event.sender.browserWindowOptions.webPreferences.winKey;
@ -43,15 +42,15 @@ function isCmdAllowed(event, cmd) {
// validate that event sender is from window we created // validate that event sender is from window we created
let browserWin = electron.BrowserWindow.fromWebContents(event.sender); let browserWin = electron.BrowserWindow.fromWebContents(event.sender);
if (windowMgr.isMainWindow(browserWin)) { if (!windowMgr.isMainWindow(browserWin)) {
// allow all commands for main window // allow all commands for main window
return true; return true;
} else { }
// allow only certain cmds for child windows // allow only certain cmds for child windows
// e.g., open cmd not allowed for child windows // e.g., open cmd not allowed for child windows
return (cmdBlackList.indexOf(cmd) === -1) return (cmdBlackList.indexOf(cmd) === -1)
} }
}
return false; return false;
} }
@ -62,12 +61,16 @@ function isCmdAllowed(event, cmd) {
*/ */
electron.ipcMain.on('symphony-api', (event, arg) => { electron.ipcMain.on('symphony-api', (event, arg) => {
if (!isValidWindow(event)) { if (!isValidWindow(event)) {
/* eslint-disable no-console */
console.log('invalid window try to perform action, ignoring action.'); console.log('invalid window try to perform action, ignoring action.');
/* eslint-enable no-console */
return; return;
} }
if (!isCmdAllowed(event, arg && arg.cmd)) { if (!isCmdAllowed(event, arg && arg.cmd)) {
/* eslint-disable no-console */
console.log('cmd not allowed for this window: ' + arg.cmd); console.log('cmd not allowed for this window: ' + arg.cmd);
/* eslint-enable no-console */
return; return;
} }
@ -91,6 +94,5 @@ electron.ipcMain.on('symphony-api', (event, arg) => {
let width = arg.width || 1024; let width = arg.width || 1024;
let height = arg.height || 768; let height = arg.height || 768;
windowMgr.createChildWindow(arg.url, title, width, height); windowMgr.createChildWindow(arg.url, title, width, height);
return;
} }
}); });

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const electron = require('electron');
const log = require('./log.js'); const log = require('./log.js');
const logLevels = require('./enums/logLevels.js') const logLevels = require('./enums/logLevels.js')

View File

@ -1,36 +1,20 @@
'use strict'; 'use strict';
const electron = require('electron');
const template = [ const template = [
{ {
label: 'Edit', label: 'Edit',
submenu: [ submenu: [
{ { role: 'undo' },
role: 'undo' { role: 'redo' },
}, { type: 'separator' },
{ { role: 'cut' },
role: 'redo' { role: 'copy' },
}, { role: 'paste' },
{ { role: 'pasteandmatchstyle' },
type: 'separator' { role: 'delete' },
}, { role: 'selectall' }
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
{
role: 'selectall'
}
] ]
}, },
{ {
@ -90,7 +74,7 @@ const template = [
submenu: [ submenu: [
{ {
label: 'Learn More', label: 'Learn More',
click () { require('electron').shell.openExternal('https://www.symphony.com') } click () { electron.shell.openExternal('https://www.symphony.com') }
} }
] ]
} }

View File

@ -25,7 +25,8 @@ const api = 'symphony-api';
// Note: certain cmds are only allowed on some windows, this is checked by // Note: certain cmds are only allowed on some windows, this is checked by
// main process. // main process.
window.SYM_API = { window.SYM_API = {
version: '1.0.0', // api version // api version
version: '1.0.0',
// only allowed by main window - enforced by main process. // only allowed by main window - enforced by main process.
openWindow: function(url) { openWindow: function(url) {
@ -60,7 +61,6 @@ window.SYM_API = {
// listen for log message from main process // listen for log message from main process
local.ipcRenderer.on('log', (event, arg) => { local.ipcRenderer.on('log', (event, arg) => {
console.log('got msg:' + arg)
if (local.logger && arg && arg.level && arg.msg) { if (local.logger && arg && arg.level && arg.msg) {
local.logger({ local.logger({
logLevel: arg.level, logLevel: arg.level,
@ -72,7 +72,7 @@ local.ipcRenderer.on('log', (event, arg) => {
function updateOnlineStatus() { function updateOnlineStatus() {
local.ipcRenderer.send(api, { local.ipcRenderer.send(api, {
cmd: 'isOnline', cmd: 'isOnline',
isOnline: navigator.onLine isOnline: window.navigator.onLine
}); });
} }

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const isDevEnv = process.env.ELECTRON_DEV ? const isDevEnv = process.env.ELECTRON_DEV ?
process.env.ELECTRON_DEV.trim().toLowerCase() === "true" : false; process.env.ELECTRON_DEV.trim().toLowerCase() === 'true' : false;
const isMac = (process.platform === 'darwin'); const isMac = (process.platform === 'darwin');
@ -12,9 +12,9 @@ const isMac = (process.platform === 'darwin');
* @return {String} guid value in string * @return {String} guid value in string
*/ */
function getGuid() { function getGuid() {
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, const guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function(c) { function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16); return v.toString(16);
}); });
return guid; return guid;

View File

@ -45,10 +45,13 @@ function createMainWindow (url) {
}); });
function retry() { function retry() {
if (isOnline) { if (!isOnline) {
mainWindow.webContents && mainWindow.webContents.reload();
} else {
loadErrors.showNetworkConnectivityError(mainWindow, url, retry); loadErrors.showNetworkConnectivityError(mainWindow, url, retry);
return;
}
if (mainWindow.webContents) {
mainWindow.webContents.reload();
} }
} }
@ -63,7 +66,7 @@ function createMainWindow (url) {
}); });
mainWindow.webContents.on('did-fail-load', function(event, errorCode, mainWindow.webContents.on('did-fail-load', function(event, errorCode,
errorDesc, validatedURL, isMainFrame) { errorDesc) {
loadErrors.showLoadFailure(mainWindow, url, errorDesc, errorCode, retry); loadErrors.showLoadFailure(mainWindow, url, errorDesc, errorCode, retry);
}); });
@ -75,7 +78,7 @@ function createMainWindow (url) {
mainWindow.on('close', function(e) { mainWindow.on('close', function(e) {
if (willQuitApp) { if (willQuitApp) {
mainWindow = null; destroyMainWindow();
return; return;
} }
// mac should hide window when hitting x close // mac should hide window when hitting x close
@ -85,19 +88,23 @@ function createMainWindow (url) {
} }
}); });
mainWindow.on('closed', function () { function destroyMainWindow() {
removeWindowKey(key); removeWindowKey(key);
mainWindow.removeAllEventListeners(); if (mainWindow) {
// Dereference the window object, usually you would store windows mainWindow.removeAllListeners();
// in an array if your app supports multi windows, this is the time if (mainWindow.webContents) {
// when you should delete the corresponding element. mainWindow.webContents.removeAllListeners();
}
mainWindow = null; mainWindow = null;
}); }
}
mainWindow.on('closed', destroyMainWindow);
// open external links in default browser - window.open // open external links in default browser - window.open
mainWindow.webContents.on('new-window', function(event, url) { mainWindow.webContents.on('new-window', function(event, newWinUrl) {
event.preventDefault(); event.preventDefault();
electron.shell.openExternal(url); electron.shell.openExternal(newWinUrl);
}); });
} }
@ -141,8 +148,13 @@ function createChildWindow(url, title, width, height) {
childWindow.loadURL(url); childWindow.loadURL(url);
childWindow.on('closed', function() { childWindow.on('closed', function() {
childWindow.removeAllEventListeners();
removeWindowKey(winKey); removeWindowKey(winKey);
if (childWindow) {
childWindow.removeAllListeners();
if (childWindow.webContents) {
childWindow.webContents.removeAllListeners();
}
}
}); });
} }

View File

@ -9,15 +9,19 @@
"dev:mac": "ELECTRON_DEV=true npm run start", "dev:mac": "ELECTRON_DEV=true npm run start",
"dev:win": "SET ELECTRON_DEV=true && npm run start", "dev:win": "SET ELECTRON_DEV=true && npm run start",
"start": "electron .", "start": "electron .",
"dist-mac": "build --mac", "dist-mac": "npm run test && npm run lint && build --mac",
"dist-win": "build --win --x64", "dist-win": "npm run test && npm run lint && build --win --x64",
"dist-win-x86": "build --win --ia32", "dist-win-x86": "npm run test && npm run lint && build --win --ia32",
"unpacked-win": "build --win --x64 --dir", "unpacked-win": "npm run test && npm run lint && build --win --x64 --dir",
"unpacked-win-x86": "build --win --ia32 --dir", "unpacked-win-x86": "npm run test && npm run lint && build --win --ia32 --dir",
"test": "jest --coverage" "test": "jest --coverage",
"lint": "eslint js/**"
}, },
"build": { "build": {
"files": [ "!installer/*", "!tests/*" ], "files": [
"!installer/*",
"!tests/*"
],
"extraFiles": "config/Symphony.config", "extraFiles": "config/Symphony.config",
"appId": "symphony-electron-desktop", "appId": "symphony-electron-desktop",
"mac": { "mac": {
@ -59,6 +63,11 @@
"electron-builder": "^13.9.0", "electron-builder": "^13.9.0",
"electron-builder-squirrel-windows": "^12.3.0", "electron-builder-squirrel-windows": "^12.3.0",
"electron-packager": "^8.5.2", "electron-packager": "^8.5.2",
"eslint": "^3.16.1",
"eslint-config-airbnb": "^14.1.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.0",
"jest": "^19.0.2" "jest": "^19.0.2"
}, },
"dependencies": { "dependencies": {