2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 02:28:55 +00:00

Update eslint and use Airbnb style

- Add `npm run lint:fix` command
- Cleanup inferIcon.js logic slightly
This commit is contained in:
Jia Hao Goh 2017-04-29 22:52:12 +08:00
parent 461c7a38f0
commit 8f78dd03af
48 changed files with 1795 additions and 1850 deletions

View File

@ -1,49 +0,0 @@
module.exports = {
globals: {
// mocha
describe: false,
it: false,
before: false,
beforeEach: false,
after: false,
afterEach: false
},
rules: {
indent: [
2,
4,
{SwitchCase: 1}
],
quotes: [
2,
'single'
],
'linebreak-style': [
2,
'unix'
],
semi: [
2,
'always'
],
'max-len': 0,
'require-jsdoc': 0,
'padded-blocks': 0,
'no-throw-literal': 0,
camelcase: 0,
'valid-jsdoc': 0,
'no-path-concat': 1,
'quote-props': [2, 'as-needed'],
'no-warning-comments': 1,
'no-control-regex': 0
},
env: {
es6: true,
browser: true,
node: true
},
ecmaFeatures: {
modules: true
},
extends: 'google'
};

8
.eslintrc.yml Normal file
View File

@ -0,0 +1,8 @@
extends: airbnb-base
plugins:
- import
rules:
# TODO: Remove this when we have shifted away from the async package
no-shadow: 'warn'
# Gulpfiles and tests use dev dependencies
import/no-extraneous-dependencies: ['error', { devDependencies: ['gulpfile.babel.js', 'gulp/**/**.js', 'test/**/**.js']}]

View File

@ -15,23 +15,19 @@ Please include the following in your new issue:
See [here](https://github.com/jiahaog/nativefier#development) for instructions on how to set up a development environment. See [here](https://github.com/jiahaog/nativefier#development) for instructions on how to set up a development environment.
Follow the current code style, and make sure tests and lints pass before submitting with the following commands: We follow the [Airbnb Style Guide](https://github.com/airbnb/javascript), please make sure tests and lints pass when you submit your pull request.
Run the following command before submitting the pull request: The following commands might be helpful:
```bash ```bash
# Run tests and linting # Run specs and lint
$ npm run ci npm run ci
```
Or you can run them separately: # Run specs only
npm run test
```bash # Run linter only
# Tests npm run lint
$ npm run test
# Lint source files
$ npm run lint
``` ```
Thank you so much for your contribution! Thank you so much for your contribution!

2
app/.eslintrc.yml Normal file
View File

@ -0,0 +1,2 @@
settings:
import/core-modules: [ electron ]

View File

@ -1,3 +1,5 @@
// Because we are changing the properties of `mainWindow` in initContextMenu()
/* eslint-disable no-param-reassign */
import { Menu, ipcMain, shell, clipboard, BrowserWindow } from 'electron'; import { Menu, ipcMain, shell, clipboard, BrowserWindow } from 'electron';
function initContextMenu(mainWindow) { function initContextMenu(mainWindow) {
@ -8,10 +10,9 @@ function initContextMenu(mainWindow) {
click: () => { click: () => {
if (targetHref) { if (targetHref) {
shell.openExternal(targetHref); shell.openExternal(targetHref);
return;
}
} }
}, },
},
{ {
label: 'Open in new window', label: 'Open in new window',
click: () => { click: () => {
@ -22,7 +23,7 @@ function initContextMenu(mainWindow) {
mainWindow.useDefaultWindowBehaviour = true; mainWindow.useDefaultWindowBehaviour = true;
mainWindow.webContents.send('contextMenuClosed'); mainWindow.webContents.send('contextMenuClosed');
} },
}, },
{ {
label: 'Copy link location', label: 'Copy link location',
@ -34,8 +35,8 @@ function initContextMenu(mainWindow) {
mainWindow.useDefaultWindowBehaviour = true; mainWindow.useDefaultWindowBehaviour = true;
mainWindow.webContents.send('contextMenuClosed'); mainWindow.webContents.send('contextMenuClosed');
} },
} },
]; ];
const contextMenu = Menu.buildFromTemplate(contextMenuTemplate); const contextMenu = Menu.buildFromTemplate(contextMenuTemplate);

View File

@ -2,15 +2,15 @@ import {BrowserWindow, ipcMain} from 'electron';
import path from 'path'; import path from 'path';
function createLoginWindow(loginCallback) { function createLoginWindow(loginCallback) {
var loginWindow = new BrowserWindow({ const loginWindow = new BrowserWindow({
width: 300, width: 300,
height: 400, height: 400,
frame: false, frame: false,
resizable: false resizable: false,
}); });
loginWindow.loadURL('file://' + path.join(__dirname, '/static/login/login.html')); loginWindow.loadURL(`file://${path.join(__dirname, '/static/login/login.html')}`);
ipcMain.once('login-message', function(event, usernameAndPassword) { ipcMain.once('login-message', (event, usernameAndPassword) => {
loginCallback(usernameAndPassword[0], usernameAndPassword[1]); loginCallback(usernameAndPassword[0], usernameAndPassword[1]);
loginWindow.close(); loginWindow.close();
}); });

View File

@ -10,17 +10,52 @@ const {isOSX, linkIsInternal, getCssToInject, shouldInjectCss} = helpers;
const ZOOM_INTERVAL = 0.1; const ZOOM_INTERVAL = 0.1;
function maybeHideWindow(window, event, fastQuit) {
if (isOSX() && !fastQuit) {
// this is called when exiting from clicking the cross button on the window
event.preventDefault();
window.hide();
}
// will close the window on other platforms
}
function maybeInjectCss(browserWindow) {
if (!shouldInjectCss()) {
return;
}
const cssToInject = getCssToInject();
const injectCss = () => {
browserWindow.webContents.insertCSS(cssToInject);
};
browserWindow.webContents.on('did-finish-load', () => {
// remove the injection of css the moment the page is loaded
browserWindow.webContents.removeListener('did-get-response-details', injectCss);
});
// on every page navigation inject the css
browserWindow.webContents.on('did-navigate', () => {
// we have to inject the css in did-get-response-details to prevent the fouc
// will run multiple times
browserWindow.webContents.on('did-get-response-details', injectCss);
});
}
/** /**
* *
* @param {{}} options AppArgs from nativefier.json * @param {{}} inpOptions AppArgs from nativefier.json
* @param {function} onAppQuit * @param {function} onAppQuit
* @param {function} setDockBadge * @param {function} setDockBadge
* @returns {electron.BrowserWindow} * @returns {electron.BrowserWindow}
*/ */
function createMainWindow(options, onAppQuit, setDockBadge) { function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
const options = Object.assign({}, inpOptions);
const mainWindowState = windowStateKeeper({ const mainWindowState = windowStateKeeper({
defaultWidth: options.width || 1280, defaultWidth: options.width || 1280,
defaultHeight: options.height || 800 defaultHeight: options.height || 800,
}); });
const mainWindow = new BrowserWindow({ const mainWindow = new BrowserWindow({
@ -43,12 +78,12 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
nodeIntegration: false, nodeIntegration: false,
webSecurity: !options.insecure, webSecurity: !options.insecure,
preload: path.join(__dirname, 'static', 'preload.js'), preload: path.join(__dirname, 'static', 'preload.js'),
zoomFactor: options.zoom zoomFactor: options.zoom,
}, },
// after webpack path here should reference `resources/app/` // after webpack path here should reference `resources/app/`
icon: path.join(__dirname, '../', '/icon.png'), icon: path.join(__dirname, '../', '/icon.png'),
// set to undefined and not false because explicitly setting to false will disable full screen // set to undefined and not false because explicitly setting to false will disable full screen
fullscreen: options.fullScreen || undefined fullscreen: options.fullScreen || undefined,
}); });
mainWindowState.manage(mainWindow); mainWindowState.manage(mainWindow);
@ -88,16 +123,17 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
buttons: ['Yes', 'Cancel'], buttons: ['Yes', 'Cancel'],
defaultId: 1, defaultId: 1,
title: 'Clear cache confirmation', title: 'Clear cache confirmation',
message: 'This will clear all data (cookies, local storage etc) from this app. Are you sure you wish to proceed?' message: 'This will clear all data (cookies, local storage etc) from this app. Are you sure you wish to proceed?',
}, response => { }, (response) => {
if (response === 0) { if (response !== 0) {
return;
}
const session = mainWindow.webContents.session; const session = mainWindow.webContents.session;
session.clearStorageData(() => { session.clearStorageData(() => {
session.clearCache(() => { session.clearCache(() => {
mainWindow.loadURL(options.targetUrl); mainWindow.loadURL(options.targetUrl);
}); });
}); });
}
}); });
}; };
@ -109,9 +145,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
mainWindow.webContents.goForward(); mainWindow.webContents.goForward();
}; };
const getCurrentUrl = () => { const getCurrentUrl = () => mainWindow.webContents.getURL();
return mainWindow.webContents.getURL();
};
const menuOptions = { const menuOptions = {
nativefierVersion: options.nativefierVersion, nativefierVersion: options.nativefierVersion,
@ -122,9 +156,9 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
zoomBuildTimeValue: options.zoom, zoomBuildTimeValue: options.zoom,
goBack: onGoBack, goBack: onGoBack,
goForward: onGoForward, goForward: onGoForward,
getCurrentUrl: getCurrentUrl, getCurrentUrl,
clearAppData: clearAppData, clearAppData,
disableDevTools: options.disableDevTools disableDevTools: options.disableDevTools,
}; };
createMenu(menuOptions); createMenu(menuOptions);
@ -143,7 +177,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
if (options.counter) { if (options.counter) {
mainWindow.on('page-title-updated', (e, title) => { mainWindow.on('page-title-updated', (e, title) => {
const itemCountRegex = /[\(\[{](\d*?)[}\]\)]/; const itemCountRegex = /[([{](\d*?)[}\])]/;
const match = itemCountRegex.exec(title); const match = itemCountRegex.exec(title);
if (match) { if (match) {
setDockBadge(match[1]); setDockBadge(match[1]);
@ -178,7 +212,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
mainWindow.loadURL(options.targetUrl); mainWindow.loadURL(options.targetUrl);
mainWindow.on('close', event => { mainWindow.on('close', (event) => {
if (mainWindow.isFullScreen()) { if (mainWindow.isFullScreen()) {
mainWindow.setFullScreen(false); mainWindow.setFullScreen(false);
mainWindow.once('leave-full-screen', maybeHideWindow.bind(this, mainWindow, event, options.fastQuit)); mainWindow.once('leave-full-screen', maybeHideWindow.bind(this, mainWindow, event, options.fastQuit));
@ -191,42 +225,10 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
ipcMain.on('cancelNewWindowOverride', () => { ipcMain.on('cancelNewWindowOverride', () => {
const allWindows = BrowserWindow.getAllWindows(); const allWindows = BrowserWindow.getAllWindows();
allWindows.forEach(window => { allWindows.forEach((window) => {
// eslint-disable-next-line no-param-reassign
window.useDefaultWindowBehaviour = false; window.useDefaultWindowBehaviour = false;
}); });
}); });
function maybeHideWindow(window, event, fastQuit) {
if (isOSX() && !fastQuit) {
// this is called when exiting from clicking the cross button on the window
event.preventDefault();
window.hide();
}
// will close the window on other platforms
}
function maybeInjectCss(browserWindow) {
if (!shouldInjectCss()) {
return;
}
const cssToInject = getCssToInject();
const injectCss = () => {
browserWindow.webContents.insertCSS(cssToInject);
};
browserWindow.webContents.on('did-finish-load', () => {
// remove the injection of css the moment the page is loaded
browserWindow.webContents.removeListener('did-get-response-details', injectCss);
});
// on every page navigation inject the css
browserWindow.webContents.on('did-navigate', () => {
// we have to inject the css in did-get-response-details to prevent the fouc
// will run multiple times
browserWindow.webContents.on('did-get-response-details', injectCss);
});
}
export default createMainWindow; export default createMainWindow;

View File

@ -13,7 +13,17 @@ import {Menu, shell, clipboard} from 'electron';
* @param clearAppData * @param clearAppData
* @param disableDevTools * @param disableDevTools
*/ */
function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoomBuildTimeValue, goBack, goForward, getCurrentUrl, clearAppData, disableDevTools}) { function createMenu({ nativefierVersion,
appQuit,
zoomIn,
zoomOut,
zoomReset,
zoomBuildTimeValue,
goBack,
goForward,
getCurrentUrl,
clearAppData,
disableDevTools }) {
if (Menu.getApplicationMenu()) { if (Menu.getApplicationMenu()) {
return; return;
} }
@ -28,25 +38,25 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
{ {
label: 'Undo', label: 'Undo',
accelerator: 'CmdOrCtrl+Z', accelerator: 'CmdOrCtrl+Z',
role: 'undo' role: 'undo',
}, },
{ {
label: 'Redo', label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z', accelerator: 'Shift+CmdOrCtrl+Z',
role: 'redo' role: 'redo',
}, },
{ {
type: 'separator' type: 'separator',
}, },
{ {
label: 'Cut', label: 'Cut',
accelerator: 'CmdOrCtrl+X', accelerator: 'CmdOrCtrl+X',
role: 'cut' role: 'cut',
}, },
{ {
label: 'Copy', label: 'Copy',
accelerator: 'CmdOrCtrl+C', accelerator: 'CmdOrCtrl+C',
role: 'copy' role: 'copy',
}, },
{ {
label: 'Copy Current URL', label: 'Copy Current URL',
@ -54,25 +64,25 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
click: () => { click: () => {
const currentURL = getCurrentUrl(); const currentURL = getCurrentUrl();
clipboard.writeText(currentURL); clipboard.writeText(currentURL);
} },
}, },
{ {
label: 'Paste', label: 'Paste',
accelerator: 'CmdOrCtrl+V', accelerator: 'CmdOrCtrl+V',
role: 'paste' role: 'paste',
}, },
{ {
label: 'Select All', label: 'Select All',
accelerator: 'CmdOrCtrl+A', accelerator: 'CmdOrCtrl+A',
role: 'selectall' role: 'selectall',
}, },
{ {
label: 'Clear App Data', label: 'Clear App Data',
click: () => { click: () => {
clearAppData(); clearAppData();
} },
} },
] ],
}, },
{ {
label: 'View', label: 'View',
@ -82,14 +92,14 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
accelerator: 'CmdOrCtrl+[', accelerator: 'CmdOrCtrl+[',
click: () => { click: () => {
goBack(); goBack();
} },
}, },
{ {
label: 'Forward', label: 'Forward',
accelerator: 'CmdOrCtrl+]', accelerator: 'CmdOrCtrl+]',
click: () => { click: () => {
goForward(); goForward();
} },
}, },
{ {
label: 'Reload', label: 'Reload',
@ -98,10 +108,10 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
if (focusedWindow) { if (focusedWindow) {
focusedWindow.reload(); focusedWindow.reload();
} }
} },
}, },
{ {
type: 'separator' type: 'separator',
}, },
{ {
label: 'Toggle Full Screen', label: 'Toggle Full Screen',
@ -115,7 +125,7 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
if (focusedWindow) { if (focusedWindow) {
focusedWindow.setFullScreen(!focusedWindow.isFullScreen()); focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
} }
} },
}, },
{ {
label: 'Zoom In', label: 'Zoom In',
@ -127,7 +137,7 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
})(), })(),
click: () => { click: () => {
zoomIn(); zoomIn();
} },
}, },
{ {
label: 'Zoom Out', label: 'Zoom Out',
@ -139,7 +149,7 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
})(), })(),
click: () => { click: () => {
zoomOut(); zoomOut();
} },
}, },
{ {
label: zoomResetLabel, label: zoomResetLabel,
@ -151,7 +161,7 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
})(), })(),
click: () => { click: () => {
zoomReset(); zoomReset();
} },
}, },
{ {
label: 'Toggle Developer Tools', label: 'Toggle Developer Tools',
@ -165,9 +175,9 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
if (focusedWindow) { if (focusedWindow) {
focusedWindow.toggleDevTools(); focusedWindow.toggleDevTools();
} }
} },
} },
] ],
}, },
{ {
label: 'Window', label: 'Window',
@ -176,14 +186,14 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
{ {
label: 'Minimize', label: 'Minimize',
accelerator: 'CmdOrCtrl+M', accelerator: 'CmdOrCtrl+M',
role: 'minimize' role: 'minimize',
}, },
{ {
label: 'Close', label: 'Close',
accelerator: 'CmdOrCtrl+W', accelerator: 'CmdOrCtrl+W',
role: 'close' role: 'close',
} },
] ],
}, },
{ {
label: 'Help', label: 'Help',
@ -193,16 +203,16 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
label: `Built with Nativefier v${nativefierVersion}`, label: `Built with Nativefier v${nativefierVersion}`,
click: () => { click: () => {
shell.openExternal('https://github.com/jiahaog/nativefier'); shell.openExternal('https://github.com/jiahaog/nativefier');
} },
}, },
{ {
label: 'Report an Issue', label: 'Report an Issue',
click: () => { click: () => {
shell.openExternal('https://github.com/jiahaog/nativefier/issues'); shell.openExternal('https://github.com/jiahaog/nativefier/issues');
} },
} },
] ],
} },
]; ];
if (disableDevTools) { if (disableDevTools) {
@ -218,45 +228,45 @@ function createMenu({nativefierVersion, appQuit, zoomIn, zoomOut, zoomReset, zoo
{ {
label: 'Services', label: 'Services',
role: 'services', role: 'services',
submenu: [] submenu: [],
}, },
{ {
type: 'separator' type: 'separator',
}, },
{ {
label: 'Hide App', label: 'Hide App',
accelerator: 'Command+H', accelerator: 'Command+H',
role: 'hide' role: 'hide',
}, },
{ {
label: 'Hide Others', label: 'Hide Others',
accelerator: 'Command+Shift+H', accelerator: 'Command+Shift+H',
role: 'hideothers' role: 'hideothers',
}, },
{ {
label: 'Show All', label: 'Show All',
role: 'unhide' role: 'unhide',
}, },
{ {
type: 'separator' type: 'separator',
}, },
{ {
label: 'Quit', label: 'Quit',
accelerator: 'Command+Q', accelerator: 'Command+Q',
click: () => { click: () => {
appQuit(); appQuit();
} },
} },
] ],
}); });
template[3].submenu.push( template[3].submenu.push(
{ {
type: 'separator' type: 'separator',
}, },
{ {
label: 'Bring All to Front', label: 'Bring All to Front',
role: 'front' role: 'front',
} },
); );
} }

View File

@ -19,12 +19,12 @@ function isWindows() {
function linkIsInternal(currentUrl, newUrl, internalUrlRegex) { function linkIsInternal(currentUrl, newUrl, internalUrlRegex) {
if (internalUrlRegex) { if (internalUrlRegex) {
var regex = RegExp(internalUrlRegex); const regex = RegExp(internalUrlRegex);
return regex.test(newUrl); return regex.test(newUrl);
} }
var currentDomain = wurl('domain', currentUrl); const currentDomain = wurl('domain', currentUrl);
var newDomain = wurl('domain', newUrl); const newDomain = wurl('domain', newUrl);
return currentDomain === newDomain; return currentDomain === newDomain;
} }
@ -51,6 +51,7 @@ function debugLog(browserWindow, message) {
setTimeout(() => { setTimeout(() => {
browserWindow.webContents.send('debug', message); browserWindow.webContents.send('debug', message);
}, 3000); }, 3000);
// eslint-disable-next-line no-console
console.log(message); console.log(message);
} }
@ -61,5 +62,5 @@ export default {
linkIsInternal, linkIsInternal,
getCssToInject, getCssToInject,
debugLog, debugLog,
shouldInjectCss shouldInjectCss,
}; };

View File

@ -4,22 +4,6 @@ import helpers from './helpers';
const { isOSX, isWindows, isLinux } = helpers; const { isOSX, isWindows, isLinux } = helpers;
function inferFlash() {
if (isOSX()) {
return darwinMatch();
}
if (isWindows()) {
return windowsMatch();
}
if (isLinux()) {
return linuxMatch();
}
console.warn('Unable to determine OS to infer flash player');
}
/** /**
* Synchronously find a file or directory * Synchronously find a file or directory
* @param {RegExp} pattern regex * @param {RegExp} pattern regex
@ -27,7 +11,7 @@ function inferFlash() {
* @param {boolean} [findDir] if true, search results will be limited to only directories * @param {boolean} [findDir] if true, search results will be limited to only directories
* @returns {Array} * @returns {Array}
*/ */
function findSync(pattern, base, findDir) { function findSync(pattern, basePath, findDir) {
const matches = []; const matches = [];
(function findSyncRecurse(base) { (function findSyncRecurse(base) {
@ -41,7 +25,7 @@ function findSync(pattern, base, findDir) {
throw exception; throw exception;
} }
children.forEach(child => { children.forEach((child) => {
const childPath = path.join(base, child); const childPath = path.join(base, child);
const childIsDirectory = fs.lstatSync(childPath).isDirectory(); const childIsDirectory = fs.lstatSync(childPath).isDirectory();
const patternMatches = pattern.test(childPath); const patternMatches = pattern.test(childPath);
@ -63,7 +47,7 @@ function findSync(pattern, base, findDir) {
matches.push(childPath); matches.push(childPath);
} }
}); });
})(base); }(basePath));
return matches; return matches;
} }
@ -79,4 +63,20 @@ function darwinMatch() {
return findSync(/PepperFlashPlayer.plugin/, '/Applications/Google Chrome.app/', true)[0]; return findSync(/PepperFlashPlayer.plugin/, '/Applications/Google Chrome.app/', true)[0];
} }
function inferFlash() {
if (isOSX()) {
return darwinMatch();
}
if (isWindows()) {
return windowsMatch();
}
if (isLinux()) {
return linuxMatch();
}
console.warn('Unable to determine OS to infer flash player');
return null;
}
export default inferFlash; export default inferFlash;

View File

@ -2,11 +2,12 @@ import 'source-map-support/register';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import { app, crashReporter } from 'electron'; import { app, crashReporter } from 'electron';
import electronDownload from 'electron-dl';
import createLoginWindow from './components/login/loginWindow'; import createLoginWindow from './components/login/loginWindow';
import createMainWindow from './components/mainWindow/mainWindow'; import createMainWindow from './components/mainWindow/mainWindow';
import helpers from './helpers/helpers'; import helpers from './helpers/helpers';
import inferFlash from './helpers/inferFlash'; import inferFlash from './helpers/inferFlash';
import electronDownload from 'electron-dl';
const { isOSX } = helpers; const { isOSX } = helpers;
@ -67,7 +68,7 @@ if (appArgs.crashReporter) {
crashReporter.start({ crashReporter.start({
productName: appArgs.name, productName: appArgs.name,
submitURL: appArgs.crashReporter, submitURL: appArgs.crashReporter,
autoSubmit: true autoSubmit: true,
}); });
}); });
} }
@ -90,7 +91,6 @@ if (appArgs.singleInstance) {
mainWindow.restore(); mainWindow.restore();
} }
mainWindow.focus(); mainWindow.focus();
} }
}); });

View File

@ -0,0 +1,2 @@
env:
browser: true

View File

@ -1,9 +1,10 @@
import electron from 'electron'; import electron from 'electron';
const { ipcRenderer } = electron; const { ipcRenderer } = electron;
const form = document.getElementById('login-form'); const form = document.getElementById('login-form');
form.addEventListener('submit', event => { form.addEventListener('submit', (event) => {
event.preventDefault(); event.preventDefault();
const username = document.getElementById('username-input').value; const username = document.getElementById('username-input').value;
const password = document.getElementById('password-input').value; const password = document.getElementById('password-input').value;

View File

@ -7,14 +7,45 @@ import fs from 'fs';
const INJECT_JS_PATH = path.join(__dirname, '../../', 'inject/inject.js'); const INJECT_JS_PATH = path.join(__dirname, '../../', 'inject/inject.js');
/**
* Patches window.Notification to set a callback on a new Notification
* @param callback
*/
function setNotificationCallback(callback) {
const OldNotify = window.Notification;
const newNotify = (title, opt) => {
callback(title, opt);
return new OldNotify(title, opt);
};
newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
Object.defineProperty(newNotify, 'permission', {
get: () => OldNotify.permission,
});
window.Notification = newNotify;
}
function clickSelector(element) {
const mouseEvent = new MouseEvent('click');
element.dispatchEvent(mouseEvent);
}
function injectScripts() {
const needToInject = fs.existsSync(INJECT_JS_PATH);
if (!needToInject) {
return;
}
// Dynamically require scripts
// eslint-disable-next-line global-require, import/no-dynamic-require
require(INJECT_JS_PATH);
}
setNotificationCallback((title, opt) => { setNotificationCallback((title, opt) => {
ipcRenderer.send('notification', title, opt); ipcRenderer.send('notification', title, opt);
}); });
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
// do things window.addEventListener('contextmenu', (event) => {
window.addEventListener('contextmenu', event => {
event.preventDefault(); event.preventDefault();
let targetElement = event.srcElement; let targetElement = event.srcElement;
@ -44,6 +75,7 @@ ipcRenderer.on('params', (event, message) => {
}); });
ipcRenderer.on('debug', (event, message) => { ipcRenderer.on('debug', (event, message) => {
// eslint-disable-next-line no-console
console.log('debug:', message); console.log('debug:', message);
}); });
@ -51,36 +83,3 @@ ipcRenderer.on('change-zoom', (event, message) => {
webFrame.setZoomFactor(message); webFrame.setZoomFactor(message);
}); });
/**
* Patches window.Notification to set a callback on a new Notification
* @param callback
*/
function setNotificationCallback(callback) {
const OldNotify = window.Notification;
const newNotify = (title, opt) => {
callback(title, opt);
return new OldNotify(title, opt);
};
newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
Object.defineProperty(newNotify, 'permission', {
get: () => {
return OldNotify.permission;
}
});
window.Notification = newNotify;
}
function clickSelector(element) {
const mouseEvent = new MouseEvent('click');
element.dispatchEvent(mouseEvent);
}
function injectScripts() {
const needToInject = fs.existsSync(INJECT_JS_PATH);
if (!needToInject) {
return;
}
require(INJECT_JS_PATH);
}

View File

@ -1,14 +1,13 @@
import gulp from 'gulp'; import gulp from 'gulp';
import PATHS from './helpers/src-paths';
import del from 'del'; import del from 'del';
import runSequence from 'run-sequence'; import runSequence from 'run-sequence';
import PATHS from './helpers/src-paths';
gulp.task('build', callback => { gulp.task('build', (callback) => {
runSequence('clean', ['build-cli', 'build-app', 'build-tests'], callback); runSequence('clean', ['build-cli', 'build-app', 'build-tests'], callback);
}); });
gulp.task('clean', callback => { gulp.task('clean', (callback) => {
del(PATHS.CLI_DEST).then(() => { del(PATHS.CLI_DEST).then(() => {
del(PATHS.APP_DEST).then(() => { del(PATHS.APP_DEST).then(() => {
del(PATHS.TEST_DEST).then(() => { del(PATHS.TEST_DEST).then(() => {

View File

@ -1,10 +1,9 @@
import gulp from 'gulp'; import gulp from 'gulp';
import webpack from 'webpack-stream';
import PATHS from './../helpers/src-paths'; import PATHS from './../helpers/src-paths';
import webpack from 'webpack-stream'; const webpackConfig = require('./../../webpack.config.js');
gulp.task('build-app', ['build-static'], () => { gulp.task('build-app', ['build-static'], () => gulp.src(PATHS.APP_MAIN_JS)
return gulp.src(PATHS.APP_MAIN_JS) .pipe(webpack(webpackConfig))
.pipe(webpack(require('./../../webpack.config.js'))) .pipe(gulp.dest(PATHS.APP_DEST)));
.pipe(gulp.dest(PATHS.APP_DEST));
});

View File

@ -4,6 +4,4 @@ import helpers from './../helpers/gulp-helpers';
const { buildES6 } = helpers; const { buildES6 } = helpers;
gulp.task('build-cli', done => { gulp.task('build-cli', done => buildES6(PATHS.CLI_SRC_JS, PATHS.CLI_DEST, done));
return buildES6(PATHS.CLI_SRC_JS, PATHS.CLI_DEST, done);
});

View File

@ -4,13 +4,9 @@ import helpers from './../helpers/gulp-helpers';
const { buildES6 } = helpers; const { buildES6 } = helpers;
gulp.task('build-static-not-js', () => { gulp.task('build-static-not-js', () => gulp.src([PATHS.APP_STATIC_ALL, '!**/*.js'])
return gulp.src([PATHS.APP_STATIC_ALL, '!**/*.js']) .pipe(gulp.dest(PATHS.APP_STATIC_DEST)));
.pipe(gulp.dest(PATHS.APP_STATIC_DEST));
});
gulp.task('build-static-js', done => { gulp.task('build-static-js', done => buildES6(PATHS.APP_STATIC_JS, PATHS.APP_STATIC_DEST, done));
return buildES6(PATHS.APP_STATIC_JS, PATHS.APP_STATIC_DEST, done);
});
gulp.task('build-static', ['build-static-js', 'build-static-not-js']); gulp.task('build-static', ['build-static-js', 'build-static-not-js']);

View File

@ -4,7 +4,7 @@ import sourcemaps from 'gulp-sourcemaps';
import babel from 'gulp-babel'; import babel from 'gulp-babel';
function shellExec(cmd, silent, callback) { function shellExec(cmd, silent, callback) {
shellJs.exec(cmd, {silent: silent}, (code, stdout, stderr) => { shellJs.exec(cmd, { silent }, (code, stdout, stderr) => {
if (code) { if (code) {
callback(JSON.stringify({ code, stdout, stderr })); callback(JSON.stringify({ code, stdout, stderr }));
return; return;
@ -24,5 +24,5 @@ function buildES6(src, dest, callback) {
export default { export default {
shellExec, shellExec,
buildES6 buildES6,
}; };

View File

@ -6,17 +6,17 @@ const paths = {
CLI_SRC: 'src', CLI_SRC: 'src',
CLI_DEST: 'lib', CLI_DEST: 'lib',
TEST_SRC: 'test', TEST_SRC: 'test',
TEST_DEST: 'built-tests' TEST_DEST: 'built-tests',
}; };
paths.APP_MAIN_JS = path.join(paths.APP_SRC, '/main.js'); paths.APP_MAIN_JS = path.join(paths.APP_SRC, '/main.js');
paths.APP_ALL = paths.APP_SRC + '/**/*'; paths.APP_ALL = `${paths.APP_SRC}/**/*`;
paths.APP_STATIC_ALL = path.join(paths.APP_SRC, 'static') + '/**/*'; paths.APP_STATIC_ALL = `${path.join(paths.APP_SRC, 'static')}/**/*`;
paths.APP_STATIC_JS = path.join(paths.APP_SRC, 'static') + '/**/*.js'; paths.APP_STATIC_JS = `${path.join(paths.APP_SRC, 'static')}/**/*.js`;
paths.APP_STATIC_DEST = path.join(paths.APP_DEST, 'static'); paths.APP_STATIC_DEST = path.join(paths.APP_DEST, 'static');
paths.CLI_SRC_JS = paths.CLI_SRC + '/**/*.js'; paths.CLI_SRC_JS = `${paths.CLI_SRC}/**/*.js`;
paths.CLI_DEST_JS = paths.CLI_DEST + '/**/*.js'; paths.CLI_DEST_JS = `${paths.CLI_DEST}/**/*.js`;
paths.TEST_SRC_JS = paths.TEST_SRC + '/**/*.js'; paths.TEST_SRC_JS = `${paths.TEST_SRC}/**/*.js`;
paths.TEST_DEST_JS = paths.TEST_DEST + '/**/*.js'; paths.TEST_DEST_JS = `${paths.TEST_DEST}/**/*.js`;
export default paths; export default paths;

View File

@ -4,10 +4,8 @@ import helpers from './helpers/gulp-helpers';
const { shellExec } = helpers; const { shellExec } = helpers;
gulp.task('publish', done => { gulp.task('publish', (done) => {
shellExec('npm publish', false, done); shellExec('npm publish', false, done);
}); });
gulp.task('release', callback => { gulp.task('release', callback => runSequence('build', 'publish', callback));
return runSequence('build', 'publish', callback);
});

View File

@ -4,10 +4,8 @@ import helpers from './helpers/gulp-helpers';
const { shellExec } = helpers; const { shellExec } = helpers;
gulp.task('prune', done => { gulp.task('prune', (done) => {
shellExec('npm prune', true, done); shellExec('npm prune', true, done);
}); });
gulp.task('test', callback => { gulp.task('test', callback => runSequence('prune', 'mocha', callback));
return runSequence('prune', 'mocha', callback);
});

View File

@ -4,6 +4,4 @@ import helpers from './../helpers/gulp-helpers';
const { buildES6 } = helpers; const { buildES6 } = helpers;
gulp.task('build-tests', done => { gulp.task('build-tests', done => buildES6(PATHS.TEST_SRC_JS, PATHS.TEST_DEST, done));
return buildES6(PATHS.TEST_SRC_JS, PATHS.TEST_DEST, done);
});

View File

@ -1,32 +1,27 @@
import gulp from 'gulp'; import gulp from 'gulp';
import PATHS from './../helpers/src-paths';
import istanbul from 'gulp-istanbul'; import istanbul from 'gulp-istanbul';
import { Instrumenter } from 'isparta'; import { Instrumenter } from 'isparta';
import mocha from 'gulp-mocha'; import mocha from 'gulp-mocha';
import PATHS from './../helpers/src-paths';
gulp.task('mocha', done => { gulp.task('mocha', (done) => {
gulp.src([PATHS.CLI_SRC_JS, '!src/cli.js']) gulp.src([PATHS.CLI_SRC_JS, '!src/cli.js'])
.pipe(istanbul({ .pipe(istanbul({
instrumenter: Instrumenter, instrumenter: Instrumenter,
includeUntested: true includeUntested: true,
})) }))
.pipe(istanbul.hookRequire()) // Force `require` to return covered files .pipe(istanbul.hookRequire()) // Force `require` to return covered files
.on('finish', () => { .on('finish', () => gulp.src(PATHS.TEST_SRC, { read: false })
return gulp.src(PATHS.TEST_SRC, {read: false})
.pipe(mocha({ .pipe(mocha({
compilers: 'js:babel-core/register', compilers: 'js:babel-core/register',
recursive: true recursive: true,
})) }))
.pipe(istanbul.writeReports({ .pipe(istanbul.writeReports({
dir: './coverage', dir: './coverage',
reporters: ['lcov'], reporters: ['lcov'],
reportOpts: {dir: './coverage'} reportOpts: { dir: './coverage' },
})) }))
.on('end', done); .on('end', done));
});
}); });
gulp.task('tdd', ['mocha'], () => { gulp.task('tdd', ['mocha'], () => gulp.watch(['src/**/*.js', 'test/**/*.js'], ['mocha']));
return gulp.watch(['src/**/*.js', 'test/**/*.js'], ['mocha']);
});

View File

@ -2,7 +2,7 @@ import gulp from 'gulp';
import PATHS from './helpers/src-paths'; import PATHS from './helpers/src-paths';
gulp.task('watch', ['build'], () => { gulp.task('watch', ['build'], () => {
var handleError = function(error) { const handleError = function (error) {
console.error(error); console.error(error);
}; };
gulp.watch(PATHS.APP_ALL, ['build-app']) gulp.watch(PATHS.APP_ALL, ['build-app'])

View File

@ -3,7 +3,7 @@ import requireDir from 'require-dir';
requireDir('./gulp', { requireDir('./gulp', {
recurse: true, recurse: true,
duplicates: true duplicates: true,
}); });
gulp.task('default', ['build']); gulp.task('default', ['build']);

View File

@ -15,6 +15,7 @@
"test": "gulp test", "test": "gulp test",
"tdd": "gulp tdd", "tdd": "gulp tdd",
"lint": "eslint .", "lint": "eslint .",
"lint:fix": "eslint . --fix",
"ci": "gulp build test && npm run lint", "ci": "gulp build test && npm run lint",
"clean": "gulp clean", "clean": "gulp clean",
"build": "gulp build", "build": "gulp build",
@ -68,8 +69,9 @@
"babel-register": "^6.6.0", "babel-register": "^6.6.0",
"chai": "^3.4.1", "chai": "^3.4.1",
"del": "^2.2.0", "del": "^2.2.0",
"eslint": "^2.10.2", "eslint": "^3.19.0",
"eslint-config-google": "^0.5.0", "eslint-config-airbnb-base": "^11.1.3",
"eslint-plugin-import": "^2.2.0",
"gulp": "^3.9.0", "gulp": "^3.9.0",
"gulp-babel": "^6.1.1", "gulp-babel": "^6.1.1",
"gulp-istanbul": "^1.1.1", "gulp-istanbul": "^1.1.1",

View File

@ -6,91 +6,9 @@ import ncp from 'ncp';
const copy = ncp.ncp; const copy = ncp.ncp;
/**
* Creates a temporary directory and copies the './app folder' inside, and adds a text file with the configuration
* for the single page app.
*
* @param {string} src
* @param {string} dest
* @param {{}} options
* @param callback
*/
function buildApp(src, dest, options, callback) {
const appArgs = selectAppArgs(options);
copy(src, dest, error => {
if (error) {
callback(`Error Copying temporary directory: ${error}`);
return;
}
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
maybeCopyScripts(options.inject, dest)
.catch(error => {
console.warn(error);
})
.then(() => {
changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl);
callback();
});
});
}
function maybeCopyScripts(srcs, dest) {
if (!srcs) {
return new Promise(resolve => {
resolve();
});
}
const promises = srcs.map(src => {
return new Promise((resolve, reject) => {
if (!fs.existsSync(src)) {
reject('Error copying injection files: file not found');
return;
}
let destFileName;
if (path.extname(src) === '.js') {
destFileName = 'inject.js';
} else if (path.extname(src) === '.css') {
destFileName = 'inject.css';
} else {
resolve();
return;
}
copy(src, path.join(dest, 'inject', destFileName), error => {
if (error) {
reject(`Error Copying injection files: ${error}`);
return;
}
resolve();
});
});
});
return new Promise((resolve, reject) => {
Promise.all(promises)
.then(() => {
resolve();
})
.catch(error => {
reject(error);
});
});
}
function changeAppPackageJsonName(appPath, name, url) {
const packageJsonPath = path.join(appPath, '/package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
packageJson.name = normalizeAppName(name, url);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
}
/** /**
* Only picks certain app args to pass to nativefier.json * Only picks certain app args to pass to nativefier.json
* @param options * @param options
* @returns {{name: (*|string), targetUrl: (string|*), counter: *, width: *, height: *, showMenuBar: *, userAgent: *, nativefierVersion: *, insecure: *, disableWebSecurity: *}}
*/ */
function selectAppArgs(options) { function selectAppArgs(options) {
return { return {
@ -118,17 +36,97 @@ function selectAppArgs(options) {
zoom: options.zoom, zoom: options.zoom,
internalUrls: options.internalUrls, internalUrls: options.internalUrls,
crashReporter: options.crashReporter, crashReporter: options.crashReporter,
singleInstance: options.singleInstance singleInstance: options.singleInstance,
}; };
} }
function maybeCopyScripts(srcs, dest) {
if (!srcs) {
return new Promise((resolve) => {
resolve();
});
}
const promises = srcs.map(src => new Promise((resolve, reject) => {
if (!fs.existsSync(src)) {
reject('Error copying injection files: file not found');
return;
}
let destFileName;
if (path.extname(src) === '.js') {
destFileName = 'inject.js';
} else if (path.extname(src) === '.css') {
destFileName = 'inject.css';
} else {
resolve();
return;
}
copy(src, path.join(dest, 'inject', destFileName), (error) => {
if (error) {
reject(`Error Copying injection files: ${error}`);
return;
}
resolve();
});
}));
return new Promise((resolve, reject) => {
Promise.all(promises)
.then(() => {
resolve();
})
.catch((error) => {
reject(error);
});
});
}
function normalizeAppName(appName, url) { function normalizeAppName(appName, url) {
// use a simple 3 byte random string to prevent collision // use a simple 3 byte random string to prevent collision
let hash = crypto.createHash('md5'); const hash = crypto.createHash('md5');
hash.update(url); hash.update(url);
const postFixHash = hash.digest('hex').substring(0, 6); const postFixHash = hash.digest('hex').substring(0, 6);
const normalized = _.kebabCase(appName.toLowerCase()); const normalized = _.kebabCase(appName.toLowerCase());
return `${normalized}-nativefier-${postFixHash}`; return `${normalized}-nativefier-${postFixHash}`;
} }
function changeAppPackageJsonName(appPath, name, url) {
const packageJsonPath = path.join(appPath, '/package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
packageJson.name = normalizeAppName(name, url);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
}
/**
* Creates a temporary directory and copies the './app folder' inside,
* and adds a text file with the configuration for the single page app.
*
* @param {string} src
* @param {string} dest
* @param {{}} options
* @param callback
*/
function buildApp(src, dest, options, callback) {
const appArgs = selectAppArgs(options);
copy(src, dest, (error) => {
if (error) {
callback(`Error Copying temporary directory: ${error}`);
return;
}
fs.writeFileSync(path.join(dest, '/nativefier.json'), JSON.stringify(appArgs));
maybeCopyScripts(options.inject, dest)
.catch((error) => {
console.warn(error);
})
.then(() => {
changeAppPackageJsonName(dest, appArgs.name, appArgs.targetUrl);
callback();
});
});
}
export default buildApp; export default buildApp;

View File

@ -15,86 +15,6 @@ import buildApp from './buildApp';
const copy = ncp.ncp; const copy = ncp.ncp;
const isWindows = helpers.isWindows; const isWindows = helpers.isWindows;
/**
* @callback buildAppCallback
* @param error
* @param {string} appPath
*/
/**
*
* @param {{}} options
* @param {buildAppCallback} callback
*/
function buildMain(options, callback) {
// pre process app
const tmpObj = tmp.dirSync({unsafeCleanup: true});
const tmpPath = tmpObj.name;
// todo check if this is still needed on later version of packager
const packagerConsole = new PackagerConsole();
const progress = new DishonestProgress(5);
async.waterfall([
callback => {
progress.tick('inferring');
optionsFactory(options, callback);
},
(options, callback) => {
progress.tick('copying');
buildApp(options.dir, tmpPath, options, error => {
if (error) {
callback(error);
return;
}
// dir now correctly references the app folder to package
options.dir = tmpPath;
callback(null, options);
});
},
(options, callback) => {
progress.tick('icons');
iconBuild(options, (error, optionsWithIcon) => {
callback(null, optionsWithIcon);
});
},
(options, callback) => {
progress.tick('packaging');
// maybe skip passing icon parameter to electron packager
const packageOptions = maybeNoIconOption(options);
packagerConsole.override();
packager(packageOptions, (error, appPathArray) => {
// restore console.error
packagerConsole.restore();
// pass options which still contains the icon to waterfall
callback(error, options, appPathArray);
});
},
(options, appPathArray, callback) => {
progress.tick('finalizing');
// somehow appPathArray is a 1 element array
const appPath = getAppPath(appPathArray);
if (!appPath) {
callback();
return;
}
maybeCopyIcons(options, appPath, error => {
callback(error, appPath);
});
}
], (error, appPath) => {
packagerConsole.playback();
callback(error, appPath);
});
}
/** /**
* Checks the app path array to determine if the packaging was completed successfully * Checks the app path array to determine if the packaging was completed successfully
* @param appPathArray Result from electron-packager * @param appPathArray Result from electron-packager
@ -115,7 +35,8 @@ function getAppPath(appPathArray) {
} }
/** /**
* Removes the `icon` parameter from options if building for Windows while not on Windows and Wine is not installed * Removes the `icon` parameter from options if building for Windows while not on Windows
* and Wine is not installed
* @param options * @param options
*/ */
function maybeNoIconOption(options) { function maybeNoIconOption(options) {
@ -151,9 +72,91 @@ function maybeCopyIcons(options, appPath, callback) {
// put the icon file into the app // put the icon file into the app
const destIconPath = path.join(appPath, 'resources/app'); const destIconPath = path.join(appPath, 'resources/app');
const destFileName = `icon${path.extname(options.icon)}`; const destFileName = `icon${path.extname(options.icon)}`;
copy(options.icon, path.join(destIconPath, destFileName), error => { copy(options.icon, path.join(destIconPath, destFileName), (error) => {
callback(error); callback(error);
}); });
} }
/**
* @callback buildAppCallback
* @param error
* @param {string} appPath
*/
/**
*
* @param {{}} inpOptions
* @param {buildAppCallback} callback
*/
function buildMain(inpOptions, callback) {
const options = Object.assign({}, inpOptions);
// pre process app
const tmpObj = tmp.dirSync({ unsafeCleanup: true });
const tmpPath = tmpObj.name;
// todo check if this is still needed on later version of packager
const packagerConsole = new PackagerConsole();
const progress = new DishonestProgress(5);
async.waterfall([
(callback) => {
progress.tick('inferring');
optionsFactory(options, callback);
},
(options, callback) => {
progress.tick('copying');
buildApp(options.dir, tmpPath, options, (error) => {
if (error) {
callback(error);
return;
}
// Change the reference file for the Electron app to be the temporary path
const newOptions = Object.assign({}, options, { dir: tmpPath });
callback(null, newOptions);
});
},
(options, callback) => {
progress.tick('icons');
iconBuild(options, (error, optionsWithIcon) => {
callback(null, optionsWithIcon);
});
},
(options, callback) => {
progress.tick('packaging');
// maybe skip passing icon parameter to electron packager
const packageOptions = maybeNoIconOption(options);
packagerConsole.override();
packager(packageOptions, (error, appPathArray) => {
// restore console.error
packagerConsole.restore();
// pass options which still contains the icon to waterfall
callback(error, options, appPathArray);
});
},
(options, appPathArray, callback) => {
progress.tick('finalizing');
// somehow appPathArray is a 1 element array
const appPath = getAppPath(appPathArray);
if (!appPath) {
callback();
return;
}
maybeCopyIcons(options, appPath, (error) => {
callback(error, appPath);
});
},
], (error, appPath) => {
packagerConsole.playback();
callback(error, appPath);
});
}
export default buildMain; export default buildMain;

View File

@ -6,6 +6,18 @@ import iconShellHelpers from './../helpers/iconShellHelpers';
const { isOSX } = helpers; const { isOSX } = helpers;
const { convertToPng, convertToIco, convertToIcns } = iconShellHelpers; const { convertToPng, convertToIco, convertToIcns } = iconShellHelpers;
function iconIsIco(iconPath) {
return path.extname(iconPath) === '.ico';
}
function iconIsPng(iconPath) {
return path.extname(iconPath) === '.png';
}
function iconIsIcns(iconPath) {
return path.extname(iconPath) === '.icns';
}
/** /**
* @callback augmentIconsCallback * @callback augmentIconsCallback
* @param error * @param error
@ -16,11 +28,11 @@ const {convertToPng, convertToIco, convertToIcns} = iconShellHelpers;
* Will check and convert a `.png` to `.icns` if necessary and augment * Will check and convert a `.png` to `.icns` if necessary and augment
* options.icon with the result * options.icon with the result
* *
* @param options will need options.platform and options.icon * @param inpOptions will need options.platform and options.icon
* @param {augmentIconsCallback} callback * @param {augmentIconsCallback} callback
*/ */
function iconBuild(options, callback) { function iconBuild(inpOptions, callback) {
const options = Object.assign({}, inpOptions);
const returnCallback = () => { const returnCallback = () => {
callback(null, options); callback(null, options);
}; };
@ -37,11 +49,11 @@ function iconBuild(options, callback) {
} }
convertToIco(options.icon) convertToIco(options.icon)
.then(outPath => { .then((outPath) => {
options.icon = outPath; options.icon = outPath;
returnCallback(); returnCallback();
}) })
.catch(error => { .catch((error) => {
log.warn('Skipping icon conversion to .ico', error); log.warn('Skipping icon conversion to .ico', error);
returnCallback(); returnCallback();
}); });
@ -55,11 +67,11 @@ function iconBuild(options, callback) {
} }
convertToPng(options.icon) convertToPng(options.icon)
.then(outPath => { .then((outPath) => {
options.icon = outPath; options.icon = outPath;
returnCallback(); returnCallback();
}) })
.catch(error => { .catch((error) => {
log.warn('Skipping icon conversion to .png', error); log.warn('Skipping icon conversion to .png', error);
returnCallback(); returnCallback();
}); });
@ -78,26 +90,14 @@ function iconBuild(options, callback) {
} }
convertToIcns(options.icon) convertToIcns(options.icon)
.then(outPath => { .then((outPath) => {
options.icon = outPath; options.icon = outPath;
returnCallback(); returnCallback();
}) })
.catch(error => { .catch((error) => {
log.warn('Skipping icon conversion to .icns', error); log.warn('Skipping icon conversion to .icns', error);
returnCallback(); returnCallback();
}); });
} }
function iconIsIco(iconPath) {
return path.extname(iconPath) === '.ico';
}
function iconIsPng(iconPath) {
return path.extname(iconPath) === '.png';
}
function iconIsIcns(iconPath) {
return path.extname(iconPath) === '.icns';
}
export default iconBuild; export default iconBuild;

View File

@ -1,11 +1,10 @@
#! /usr/bin/env node #! /usr/bin/env node
import 'source-map-support/register'; import 'source-map-support/register';
import path from 'path';
import program from 'commander'; import program from 'commander';
import nativefier from './index'; import nativefier from './index';
const packageJson = require(path.join('..', 'package'));
const packageJson = require('./../package');
function collect(val, memo) { function collect(val, memo) {
memo.push(val); memo.push(val);
@ -13,11 +12,10 @@ function collect(val, memo) {
} }
if (require.main === module) { if (require.main === module) {
program program
.version(packageJson.version) .version(packageJson.version)
.arguments('<targetUrl> [dest]') .arguments('<targetUrl> [dest]')
.action(function(targetUrl, appDir) { .action((targetUrl, appDir) => {
program.targetUrl = targetUrl; program.targetUrl = targetUrl;
program.out = appDir; program.out = appDir;
}) })

View File

@ -2,6 +2,7 @@ import shell from 'shelljs';
import path from 'path'; import path from 'path';
import tmp from 'tmp'; import tmp from 'tmp';
import helpers from './helpers'; import helpers from './helpers';
const isOSX = helpers.isOSX; const isOSX = helpers.isOSX;
tmp.setGracefulCleanup(); tmp.setGracefulCleanup();
@ -29,8 +30,8 @@ function convertToIcns(pngSrc, icnsDest, callback) {
if (stdOut.includes('icon.iconset:error') || exitCode) { if (stdOut.includes('icon.iconset:error') || exitCode) {
if (exitCode) { if (exitCode) {
callback({ callback({
stdOut: stdOut, stdOut,
stdError: stdError stdError,
}, pngSrc); }, pngSrc);
return; return;
} }

View File

@ -9,23 +9,26 @@ class DishonestProgress {
incomplete: ' ', incomplete: ' ',
total: total * this.tickParts, total: total * this.tickParts,
width: 50, width: 50,
clear: true clear: true,
}); });
this.tickingPrevious = { this.tickingPrevious = {
message: '', message: '',
remainder: 0, remainder: 0,
interval: null interval: null,
}; };
} }
tick(message) { tick(message) {
const {
const {remainder: prevRemainder, message: prevMessage, interval: prevInterval} = this.tickingPrevious; remainder: prevRemainder,
message: prevMessage,
interval: prevInterval,
} = this.tickingPrevious;
if (prevRemainder) { if (prevRemainder) {
this.bar.tick(prevRemainder, { this.bar.tick(prevRemainder, {
task: prevMessage task: prevMessage,
}); });
clearInterval(prevInterval); clearInterval(prevInterval);
} }
@ -33,19 +36,19 @@ class DishonestProgress {
const realRemainder = this.bar.total - this.bar.curr; const realRemainder = this.bar.total - this.bar.curr;
if (realRemainder === this.tickParts) { if (realRemainder === this.tickParts) {
this.bar.tick(this.tickParts, { this.bar.tick(this.tickParts, {
task: message task: message,
}); });
return; return;
} }
this.bar.tick({ this.bar.tick({
task: message task: message,
}); });
this.tickingPrevious = { this.tickingPrevious = {
message: message, message,
remainder: this.tickParts, remainder: this.tickParts,
interval: null interval: null,
}; };
this.tickingPrevious.remainder -= 1; this.tickingPrevious.remainder -= 1;
@ -57,11 +60,10 @@ class DishonestProgress {
} }
this.bar.tick({ this.bar.tick({
task: message task: message,
}); });
this.tickingPrevious.remainder -= 1; this.tickingPrevious.remainder -= 1;
}, 200); }, 200);
} }
} }

View File

@ -14,15 +14,15 @@ function isWindows() {
function downloadFile(fileUrl) { function downloadFile(fileUrl) {
return axios.get( return axios.get(
fileUrl, { fileUrl, {
responseType: 'arraybuffer' responseType: 'arraybuffer',
}) })
.then(function(response) { .then((response) => {
if (!response.data) { if (!response.data) {
return null; return null;
} }
return { return {
data: response.data, data: response.data,
ext: path.extname(fileUrl) ext: path.extname(fileUrl),
}; };
}); });
} }
@ -56,7 +56,7 @@ function allowedIconFormats(platform) {
formats.push('.ico'); formats.push('.ico');
break; break;
default: default:
throw `function allowedIconFormats error: Unknown platform ${platform}`; throw new Error(`function allowedIconFormats error: Unknown platform ${platform}`);
} }
return formats; return formats;
} }
@ -90,7 +90,7 @@ function allowedIconFormats(platform) {
} }
break; break;
default: default:
throw `function allowedIconFormats error: Unknown platform ${platform}`; throw new Error(`function allowedIconFormats error: Unknown platform ${platform}`);
} }
return formats; return formats;
} }
@ -99,5 +99,5 @@ export default {
isOSX, isOSX,
isWindows, isWindows,
downloadFile, downloadFile,
allowedIconFormats allowedIconFormats,
}; };

View File

@ -2,6 +2,7 @@ import shell from 'shelljs';
import path from 'path'; import path from 'path';
import tmp from 'tmp'; import tmp from 'tmp';
import helpers from './helpers'; import helpers from './helpers';
const { isWindows, isOSX } = helpers; const { isWindows, isOSX } = helpers;
tmp.setGracefulCleanup(); tmp.setGracefulCleanup();
@ -10,7 +11,7 @@ const SCRIPT_PATHS = {
singleIco: path.join(__dirname, '../..', 'bin/singleIco'), singleIco: path.join(__dirname, '../..', 'bin/singleIco'),
convertToPng: path.join(__dirname, '../..', 'bin/convertToPng'), convertToPng: path.join(__dirname, '../..', 'bin/convertToPng'),
convertToIco: path.join(__dirname, '../..', 'bin/convertToIco'), convertToIco: path.join(__dirname, '../..', 'bin/convertToIco'),
convertToIcns: path.join(__dirname, '../..', 'bin/convertToIcns') convertToIcns: path.join(__dirname, '../..', 'bin/convertToIcns'),
}; };
/** /**
@ -29,8 +30,8 @@ function iconShellHelper(shellScriptPath, icoSrc, dest) {
shell.exec(`${shellScriptPath} ${icoSrc} ${dest}`, { silent: true }, (exitCode, stdOut, stdError) => { shell.exec(`${shellScriptPath} ${icoSrc} ${dest}`, { silent: true }, (exitCode, stdOut, stdError) => {
if (exitCode) { if (exitCode) {
reject({ reject({
stdOut: stdOut, stdOut,
stdError: stdError stdError,
}); });
return; return;
} }
@ -74,5 +75,5 @@ export default {
singleIco, singleIco,
convertToPng, convertToPng,
convertToIco, convertToIco,
convertToIcns convertToIcns,
}; };

View File

@ -1,3 +1,4 @@
// TODO: remove this file and use quiet mode of new version of electron packager
class PackagerConsole { class PackagerConsole {
constructor() { constructor() {
@ -12,6 +13,7 @@ class PackagerConsole {
this.consoleError = console.error; this.consoleError = console.error;
// need to bind because somehow when _log() is called this refers to console // need to bind because somehow when _log() is called this refers to console
// eslint-disable-next-line no-underscore-dangle
console.error = this._log.bind(this); console.error = this._log.bind(this);
} }

View File

@ -10,56 +10,6 @@ tmp.setGracefulCleanup();
const GITCLOUD_SPACE_DELIMITER = '-'; const GITCLOUD_SPACE_DELIMITER = '-';
function inferIconFromStore(targetUrl, platform) {
const allowedFormats = allowedIconFormats(platform);
return gitCloud('http://jiahaog.com/nativefier-icons/')
.then(fileIndex => {
const iconWithScores = mapIconWithMatchScore(fileIndex, targetUrl);
const maxScore = getMaxMatchScore(iconWithScores);
if (maxScore === 0) {
return null;
}
const matchingIcons = getMatchingIcons(iconWithScores, maxScore);
let matchingUrl;
for (let format of allowedFormats) {
for (let icon of matchingIcons) {
if (icon.ext !== format) {
continue;
}
matchingUrl = icon.url;
}
}
if (!matchingUrl) {
return null;
}
return downloadFile(matchingUrl);
});
}
function mapIconWithMatchScore(fileIndex, targetUrl) {
const normalisedTargetUrl = targetUrl.toLowerCase();
return fileIndex
.map(item => {
const itemWords = item.name.split(GITCLOUD_SPACE_DELIMITER);
const score = itemWords.reduce((currentScore, word) => {
if (normalisedTargetUrl.includes(word)) {
return currentScore + 1;
}
return currentScore;
}, 0);
return Object.assign({},
item,
{score}
);
});
}
function getMaxMatchScore(iconWithScores) { function getMaxMatchScore(iconWithScores) {
return iconWithScores.reduce((maxScore, currentIcon) => { return iconWithScores.reduce((maxScore, currentIcon) => {
const currentScore = currentIcon.score; const currentScore = currentIcon.score;
@ -73,23 +23,55 @@ function getMaxMatchScore(iconWithScores) {
/** /**
* also maps ext to icon object * also maps ext to icon object
*/ */
function getMatchingIcons(iconWithScores, maxScore) { function getMatchingIcons(iconsWithScores, maxScore) {
return iconWithScores return iconsWithScores
.filter(item => { .filter(item => item.score === maxScore)
return item.score === maxScore; .map(item => Object.assign({}, item, { ext: path.extname(item.url) }));
}) }
.map(item => {
return Object.assign( function mapIconWithMatchScore(fileIndex, targetUrl) {
{}, const normalisedTargetUrl = targetUrl.toLowerCase();
item, return fileIndex
{ext: path.extname(item.url)} .map((item) => {
); const itemWords = item.name.split(GITCLOUD_SPACE_DELIMITER);
const score = itemWords.reduce((currentScore, word) => {
if (normalisedTargetUrl.includes(word)) {
return currentScore + 1;
}
return currentScore;
}, 0);
return Object.assign({}, item, { score });
});
}
function inferIconFromStore(targetUrl, platform) {
const allowedFormats = new Set(allowedIconFormats(platform));
return gitCloud('http://jiahaog.com/nativefier-icons/')
.then((fileIndex) => {
const iconWithScores = mapIconWithMatchScore(fileIndex, targetUrl);
const maxScore = getMaxMatchScore(iconWithScores);
if (maxScore === 0) {
return null;
}
const iconsMatchingScore = getMatchingIcons(iconWithScores, maxScore);
const iconsMatchingExt = iconsMatchingScore.filter(icon => allowedFormats.has(icon.ext));
const matchingIcon = iconsMatchingExt[0];
const iconUrl = matchingIcon && matchingIcon.url;
if (!iconUrl) {
return null;
}
return downloadFile(iconUrl);
}); });
} }
function writeFilePromise(outPath, data) { function writeFilePromise(outPath, data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.writeFile(outPath, data, error => { fs.writeFile(outPath, data, (error) => {
if (error) { if (error) {
reject(error); reject(error);
return; return;
@ -107,7 +89,7 @@ function inferFromPage(targetUrl, platform, outDir) {
// todo might want to pass list of preferences instead // todo might want to pass list of preferences instead
return pageIcon(targetUrl, { ext: preferredExt }) return pageIcon(targetUrl, { ext: preferredExt })
.then(icon => { .then((icon) => {
if (!icon) { if (!icon) {
return null; return null;
} }
@ -116,6 +98,7 @@ function inferFromPage(targetUrl, platform, outDir) {
return writeFilePromise(outfilePath, icon.data); return writeFilePromise(outfilePath, icon.data);
}); });
} }
/** /**
* *
* @param {string} targetUrl * @param {string} targetUrl
@ -123,9 +106,8 @@ function inferFromPage(targetUrl, platform, outDir) {
* @param {string} outDir * @param {string} outDir
*/ */
function inferIconFromUrlToPath(targetUrl, platform, outDir) { function inferIconFromUrlToPath(targetUrl, platform, outDir) {
return inferIconFromStore(targetUrl, platform) return inferIconFromStore(targetUrl, platform)
.then(icon => { .then((icon) => {
if (!icon) { if (!icon) {
return inferFromPage(targetUrl, platform, outDir); return inferFromPage(targetUrl, platform, outDir);
} }

View File

@ -6,18 +6,18 @@ function inferPlatform() {
return platform; return platform;
} }
throw `Untested platform ${platform} detected`; throw new Error(`Untested platform ${platform} detected`);
} }
function inferArch() { function inferArch() {
const arch = os.arch(); const arch = os.arch();
if (arch !== 'ia32' && arch !== 'x64') { if (arch !== 'ia32' && arch !== 'x64') {
throw `Incompatible architecture ${arch} detected`; throw new Error(`Incompatible architecture ${arch} detected`);
} }
return arch; return arch;
} }
export default { export default {
inferPlatform: inferPlatform, inferPlatform,
inferArch: inferArch inferArch,
}; };

View File

@ -9,8 +9,8 @@ function inferTitle(url) {
url, url,
headers: { headers: {
// fake a user agent because pages like http://messenger.com will throw 404 error // fake a user agent because pages like http://messenger.com will throw 404 error
'User-Agent': USER_AGENT 'User-Agent': USER_AGENT,
} },
}; };
return axios(options).then(({ data }) => { return axios(options).then(({ data }) => {

View File

@ -7,16 +7,17 @@ const DEFAULT_CHROME_VERSION = '56.0.2924.87';
function getChromeVersionForElectronVersion(electronVersion, url = ELECTRON_VERSIONS_URL) { function getChromeVersionForElectronVersion(electronVersion, url = ELECTRON_VERSIONS_URL) {
return axios.get(url, { timeout: 5000 }) return axios.get(url, { timeout: 5000 })
.then(response => { .then((response) => {
if (response.status !== 200) { if (response.status !== 200) {
throw `Bad request: Status code ${response.status}`; throw new Error(`Bad request: Status code ${response.status}`);
} }
const data = response.data; const data = response.data;
const electronVersionToChromeVersion = _.zipObject(data.map(d => d.version), data.map(d => d.chrome)); const electronVersionToChromeVersion = _.zipObject(data.map(d => d.version),
data.map(d => d.chrome));
if (!(electronVersion in electronVersionToChromeVersion)) { if (!(electronVersion in electronVersionToChromeVersion)) {
throw `Electron version '${electronVersion}' not found in retrieved version list!`; throw new Error(`Electron version '${electronVersion}' not found in retrieved version list!`);
} }
return electronVersionToChromeVersion[electronVersion]; return electronVersionToChromeVersion[electronVersion];
@ -36,16 +37,14 @@ export function getUserAgentString(chromeVersion, platform) {
userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`; userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion} Safari/537.36`;
break; break;
default: default:
throw 'Error invalid platform specified to getUserAgentString()'; throw new Error('Error invalid platform specified to getUserAgentString()');
} }
return userAgent; return userAgent;
} }
function inferUserAgent(electronVersion, platform, url = ELECTRON_VERSIONS_URL) { function inferUserAgent(electronVersion, platform, url = ELECTRON_VERSIONS_URL) {
return getChromeVersionForElectronVersion(electronVersion, url) return getChromeVersionForElectronVersion(electronVersion, url)
.then(chromeVersion => { .then(chromeVersion => getUserAgentString(chromeVersion, platform))
return getUserAgentString(chromeVersion, platform);
})
.catch(() => { .catch(() => {
log.warn(`Unable to infer chrome version for user agent, using ${DEFAULT_CHROME_VERSION}`); log.warn(`Unable to infer chrome version for user agent, using ${DEFAULT_CHROME_VERSION}`);
return getUserAgentString(DEFAULT_CHROME_VERSION, platform); return getUserAgentString(DEFAULT_CHROME_VERSION, platform);

View File

@ -15,10 +15,10 @@ function normalizeUrl(testUrl) {
const validatorOptions = { const validatorOptions = {
require_protocol: true, require_protocol: true,
require_tld: false, require_tld: false,
allow_trailing_dot: true // mDNS addresses, https://github.com/jiahaog/nativefier/issues/308 allow_trailing_dot: true, // mDNS addresses, https://github.com/jiahaog/nativefier/issues/308
}; };
if (!validator.isURL(urlWithProtocol, validatorOptions)) { if (!validator.isURL(urlWithProtocol, validatorOptions)) {
throw `Your Url: "${urlWithProtocol}" is invalid!`; throw new Error(`Your Url: "${urlWithProtocol}" is invalid!`);
} }
return urlWithProtocol; return urlWithProtocol;
} }

View File

@ -15,9 +15,27 @@ const {inferPlatform, inferArch} = inferOs;
const PLACEHOLDER_APP_DIR = path.join(__dirname, '../../', 'app'); const PLACEHOLDER_APP_DIR = path.join(__dirname, '../../', 'app');
const ELECTRON_VERSION = '1.6.6'; const ELECTRON_VERSION = '1.6.6';
const DEFAULT_APP_NAME = 'APP'; const DEFAULT_APP_NAME = 'APP';
function sanitizeFilename(platform, str) {
let result = sanitizeFilenameLib(str);
// remove all non ascii or use default app name
// eslint-disable-next-line no-control-regex
result = result.replace(/[^\x00-\x7F]/g, '') || DEFAULT_APP_NAME;
// spaces will cause problems with Ubuntu when pinned to the dock
if (platform === 'linux') {
return _.kebabCase(result);
}
return result;
}
function sanitizeOptions(options) {
const name = sanitizeFilename(options.platform, options.name);
return Object.assign({}, options, { name });
}
/** /**
* @callback optionsCallback * @callback optionsCallback
* @param error * @param error
@ -30,7 +48,6 @@ const DEFAULT_APP_NAME = 'APP';
* @param {optionsCallback} callback * @param {optionsCallback} callback
*/ */
function optionsFactory(inpOptions, callback) { function optionsFactory(inpOptions, callback) {
const options = { const options = {
dir: PLACEHOLDER_APP_DIR, dir: PLACEHOLDER_APP_DIR,
name: inpOptions.name, name: inpOptions.name,
@ -69,7 +86,7 @@ function optionsFactory(inpOptions, callback) {
tmpdir: false, tmpdir: false,
zoom: inpOptions.zoom || 1.0, zoom: inpOptions.zoom || 1.0,
internalUrls: inpOptions.internalUrls || null, internalUrls: inpOptions.internalUrls || null,
singleInstance: inpOptions.singleInstance || false singleInstance: inpOptions.singleInstance || false,
}; };
if (options.verbose) { if (options.verbose) {
@ -103,34 +120,34 @@ function optionsFactory(inpOptions, callback) {
} }
async.waterfall([ async.waterfall([
callback => { (callback) => {
if (options.userAgent) { if (options.userAgent) {
callback(); callback();
return; return;
} }
inferUserAgent(options.electronVersion, options.platform) inferUserAgent(options.electronVersion, options.platform)
.then(userAgent => { .then((userAgent) => {
options.userAgent = userAgent; options.userAgent = userAgent;
callback(); callback();
}) })
.catch(callback); .catch(callback);
}, },
callback => { (callback) => {
if (options.icon) { if (options.icon) {
callback(); callback();
return; return;
} }
inferIcon(options.targetUrl, options.platform) inferIcon(options.targetUrl, options.platform)
.then(pngPath => { .then((pngPath) => {
options.icon = pngPath; options.icon = pngPath;
callback(); callback();
}) })
.catch(error => { .catch((error) => {
log.warn('Cannot automatically retrieve the app icon:', error); log.warn('Cannot automatically retrieve the app icon:', error);
callback(); callback();
}); });
}, },
callback => { (callback) => {
// length also checks if its the commanderJS function or a string // length also checks if its the commanderJS function or a string
if (options.name && options.name.length > 0) { if (options.name && options.name.length > 0) {
callback(); callback();
@ -138,35 +155,21 @@ function optionsFactory(inpOptions, callback) {
} }
options.name = DEFAULT_APP_NAME; options.name = DEFAULT_APP_NAME;
inferTitle(options.targetUrl).then(pageTitle => { inferTitle(options.targetUrl).then((pageTitle) => {
options.name = pageTitle; options.name = pageTitle;
}).catch(error => { }).catch((error) => {
log.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'. Reason: ${error}`); log.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'. Reason: ${error}`);
}).then(() => { }).then(() => {
callback(); callback();
}); });
},
], (error) => {
if (error) {
callback(error);
return;
} }
], error => { callback(null, sanitizeOptions(options));
callback(error, sanitizeOptions(options));
}); });
} }
function sanitizeFilename(platform, str) {
let result = sanitizeFilenameLib(str);
// remove all non ascii or use default app name
result = result.replace(/[^\x00-\x7F]/g, '') || DEFAULT_APP_NAME;
// spaces will cause problems with Ubuntu when pinned to the dock
if (platform === 'linux') {
return _.kebabCase(result);
}
return result;
}
function sanitizeOptions(options) {
options.name = sanitizeFilename(options.platform, options.name);
return options;
}
export default optionsFactory; export default optionsFactory;

View File

@ -0,0 +1,2 @@
env:
mocha: true

View File

@ -7,7 +7,7 @@ import os from 'os';
import path from 'path'; import path from 'path';
import convertToIcns from './../../lib/helpers/convertToIcns'; import convertToIcns from './../../lib/helpers/convertToIcns';
let assert = chai.assert; const assert = chai.assert;
// Prerequisite for test: to use OSX with sips, iconutil and imagemagick convert // Prerequisite for test: to use OSX with sips, iconutil and imagemagick convert
@ -18,24 +18,24 @@ function testConvertPng(pngName, done) {
return; return;
} }
let stat = fs.statSync(icnsPath); const stat = fs.statSync(icnsPath);
assert.isTrue(stat.isFile(), 'Output icns file should be a path'); assert.isTrue(stat.isFile(), 'Output icns file should be a path');
done(); done();
}); });
} }
describe('Get Icon Module', function() { describe('Get Icon Module', () => {
it('Can convert icons', function() { it('Can convert icons', () => {
if (os.platform() !== 'darwin') { if (os.platform() !== 'darwin') {
console.warn('Skipping png conversion tests, OSX is required'); console.warn('Skipping png conversion tests, OSX is required');
return; return;
} }
it('Can convert a rgb png to icns', function(done) { it('Can convert a rgb png to icns', (done) => {
testConvertPng('iconSample.png', done); testConvertPng('iconSample.png', done);
}); });
it('Can convert a grey png to icns', function(done) { it('Can convert a grey png to icns', (done) => {
testConvertPng('iconSampleGrey.png', done); testConvertPng('iconSampleGrey.png', done);
}); });
}); });

View File

@ -25,7 +25,7 @@ function checkApp(appPath, inputOptions, callback) {
relPathToConfig = 'resources/app'; relPathToConfig = 'resources/app';
break; break;
default: default:
throw 'Unknown app platform'; throw new Error('Unknown app platform');
} }
const nativefierConfigPath = path.join(appPath, relPathToConfig, 'nativefier.json'); const nativefierConfigPath = path.join(appPath, relPathToConfig, 'nativefier.json');
@ -33,7 +33,8 @@ function checkApp(appPath, inputOptions, callback) {
assert.strictEqual(inputOptions.targetUrl, nativefierConfig.targetUrl, 'Packaged app must have the same targetUrl as the input parameters'); assert.strictEqual(inputOptions.targetUrl, nativefierConfig.targetUrl, 'Packaged app must have the same targetUrl as the input parameters');
// app name is not consistent for linux // app name is not consistent for linux
// assert.strictEqual(inputOptions.appName, nativefierConfig.name, 'Packaged app must have the same name as the input parameters'); // assert.strictEqual(inputOptions.appName, nativefierConfig.name,
// 'Packaged app must have the same name as the input parameters');
callback(); callback();
} catch (exception) { } catch (exception) {
callback(exception); callback(exception);
@ -42,9 +43,8 @@ function checkApp(appPath, inputOptions, callback) {
describe('Nativefier Module', function () { describe('Nativefier Module', function () {
this.timeout(240000); this.timeout(240000);
it('Can build an app from a target url', function(done) { it('Can build an app from a target url', (done) => {
async.eachSeries(PLATFORMS, (platform, callback) => { async.eachSeries(PLATFORMS, (platform, callback) => {
const tmpObj = tmp.dirSync({ unsafeCleanup: true }); const tmpObj = tmp.dirSync({ unsafeCleanup: true });
const tmpPath = tmpObj.name; const tmpPath = tmpObj.name;
@ -53,7 +53,7 @@ describe('Nativefier Module', function() {
targetUrl: 'http://google.com', targetUrl: 'http://google.com',
out: tmpPath, out: tmpPath,
overwrite: true, overwrite: true,
platform: null platform: null,
}; };
options.platform = platform; options.platform = platform;
@ -63,11 +63,11 @@ describe('Nativefier Module', function() {
return; return;
} }
checkApp(appPath, options, error => { checkApp(appPath, options, (error) => {
callback(error); callback(error);
}); });
}); });
}, error => { }, (error) => {
done(error); done(error);
}); });
}); });

View File

@ -1,46 +1,44 @@
import inferUserAgent from './../../lib/infer/inferUserAgent';
import chai from 'chai'; import chai from 'chai';
import _ from 'lodash'; import _ from 'lodash';
import inferUserAgent from './../../lib/infer/inferUserAgent';
const assert = chai.assert; const assert = chai.assert;
const TEST_RESULT = { const TEST_RESULT = {
darwin: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36', darwin: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
win32: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36', win32: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
linux: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36' linux: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36',
}; };
function testPlatform(platform) { function testPlatform(platform) {
return inferUserAgent('0.37.1', platform) return inferUserAgent('0.37.1', platform)
.then(userAgent => { .then((userAgent) => {
assert.equal(userAgent, TEST_RESULT[platform], 'Correct user agent should be inferred'); assert.equal(userAgent, TEST_RESULT[platform], 'Correct user agent should be inferred');
}); });
} }
describe('Infer User Agent', function () { describe('Infer User Agent', function () {
this.timeout(15000); this.timeout(15000);
it('Can infer userAgent for all platforms', function(done) { it('Can infer userAgent for all platforms', (done) => {
const testPromises = _.keys(TEST_RESULT).map(platform => { const testPromises = _.keys(TEST_RESULT).map(platform => testPlatform(platform));
return testPlatform(platform);
});
Promise Promise
.all(testPromises) .all(testPromises)
.then(() => { .then(() => {
done(); done();
}) })
.catch(error => { .catch((error) => {
done(error); done(error);
}); });
}); });
it('Connection error will still get a user agent', function(done) { it('Connection error will still get a user agent', (done) => {
const TIMEOUT_URL = 'http://www.google.com:81/'; const TIMEOUT_URL = 'http://www.google.com:81/';
inferUserAgent('1.6.7', 'darwin', TIMEOUT_URL) inferUserAgent('1.6.7', 'darwin', TIMEOUT_URL)
.then(userAgent => { .then((userAgent) => {
assert.equal( assert.equal(
userAgent, userAgent,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Expect default user agent on connection error' 'Expect default user agent on connection error',
); );
done(); done();
}) })

View File

@ -1,10 +1,10 @@
import normalizeUrl from '../../../src/options/normalizeUrl';
import chai from 'chai'; import chai from 'chai';
import normalizeUrl from '../../../src/options/normalizeUrl';
const assert = chai.assert; const assert = chai.assert;
const expect = chai.expect; const expect = chai.expect;
describe('Normalize URL', () => { describe('Normalize URL', () => {
describe('given a valid URL without a protocol', () => { describe('given a valid URL without a protocol', () => {
it('should allow the url', () => { it('should allow the url', () => {
assert.equal(normalizeUrl('http://www.google.com'), 'http://www.google.com'); assert.equal(normalizeUrl('http://www.google.com'), 'http://www.google.com');

View File

@ -1,24 +1,24 @@
var electronPublicApi = ['electron']; const electronPublicApi = ['electron'];
var nodeModules = {}; const nodeModules = {};
electronPublicApi.forEach(apiString => { electronPublicApi.forEach((apiString) => {
nodeModules[apiString] = 'commonjs ' + apiString; nodeModules[apiString] = `commonjs ${apiString}`;
}); });
module.exports = { module.exports = {
target: 'node', target: 'node',
output: { output: {
filename: 'main.js' filename: 'main.js',
}, },
node: { node: {
global: false, global: false,
__dirname: false __dirname: false,
}, },
externals: nodeModules, externals: nodeModules,
module: { module: {
loaders: [ loaders: [
{test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'} { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' },
] ],
}, },
devtool: 'source-map' devtool: 'source-map',
}; };