2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2025-04-02 04:51:49 +00:00

Lint all files

This commit is contained in:
Jia Hao 2016-01-24 02:02:23 +08:00
parent b7433dfc3b
commit 82c29de231
12 changed files with 132 additions and 79 deletions

39
.eslintrc.js Normal file
View File

@ -0,0 +1,39 @@
module.exports = {
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-unused-vars': 1,
'no-path-concat': 1,
'quote-props': [2, 'as-needed']
},
env: {
es6: true,
browser: true,
node: true
},
ecmaFeatures: {
modules: true
},
extends: 'google'
};

View File

@ -35,18 +35,19 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
nodeIntegration: false, nodeIntegration: false,
preload: path.join(__dirname, 'static', 'preload.js') preload: path.join(__dirname, 'static', 'preload.js')
}, },
icon: options.icon || path.join(__dirname, '/icon.png') // hardcoded by default until you decide how to pass in an icon // hardcoded by default until you decide how to pass in an icon
icon: options.icon || path.join(__dirname, '/icon.png')
} }
); );
var currentZoom = 1; var currentZoom = 1;
var onZoomIn = function () { var onZoomIn = function() {
currentZoom += ZOOM_INTERVAL; currentZoom += ZOOM_INTERVAL;
mainWindow.webContents.send('change-zoom', currentZoom); mainWindow.webContents.send('change-zoom', currentZoom);
}; };
var onZoomOut = function () { var onZoomOut = function() {
currentZoom -= ZOOM_INTERVAL; currentZoom -= ZOOM_INTERVAL;
mainWindow.webContents.send('change-zoom', currentZoom); mainWindow.webContents.send('change-zoom', currentZoom);
}; };
@ -57,12 +58,12 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
mainWindow.webContents.setUserAgent(options.userAgent); mainWindow.webContents.setUserAgent(options.userAgent);
} }
mainWindow.webContents.on('did-finish-load', function () { mainWindow.webContents.on('did-finish-load', function() {
mainWindow.webContents.send('params', JSON.stringify(options)); mainWindow.webContents.send('params', JSON.stringify(options));
}); });
if (options.counter) { if (options.counter) {
mainWindow.on('page-title-updated', function () { mainWindow.on('page-title-updated', function() {
if (mainWindow.isFocused()) { if (mainWindow.isFocused()) {
return; return;
@ -81,7 +82,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
}); });
} }
mainWindow.webContents.on('new-window', function (event, urlToGo) { mainWindow.webContents.on('new-window', function(event, urlToGo) {
if (linkIsInternal(options.targetUrl, urlToGo)) { if (linkIsInternal(options.targetUrl, urlToGo)) {
return; return;
} }
@ -91,7 +92,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
mainWindow.loadURL(options.targetUrl); mainWindow.loadURL(options.targetUrl);
mainWindow.on('focus', function () { mainWindow.on('focus', function() {
setDockBadge(''); setDockBadge('');
}); });
@ -100,7 +101,7 @@ function createMainWindow(options, onAppQuit, setDockBadge) {
mainWindow.setFullScreen(false); mainWindow.setFullScreen(false);
mainWindow.once('leave-full-screen', maybeHideWindow.bind(this, mainWindow, event)); mainWindow.once('leave-full-screen', maybeHideWindow.bind(this, mainWindow, event));
} }
maybeHideWindow(mainWindow, event) maybeHideWindow(mainWindow, event);
}); });
mainWindowState.manage(mainWindow); mainWindowState.manage(mainWindow);

View File

@ -12,8 +12,9 @@ var shell = electron.shell;
* @param {function} onZoomOut * @param {function} onZoomOut
*/ */
function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn, onZoomOut) { function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn, onZoomOut) {
if (Menu.getApplicationMenu()) if (Menu.getApplicationMenu()) {
return; return;
}
var template = [ var template = [
{ {
@ -51,7 +52,7 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
label: 'Select All', label: 'Select All',
accelerator: 'CmdOrCtrl+A', accelerator: 'CmdOrCtrl+A',
role: 'selectall' role: 'selectall'
}, }
] ]
}, },
{ {
@ -59,22 +60,23 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
submenu: [ submenu: [
{ {
label: 'Back', label: 'Back',
click: function () { click: function() {
onGoBack(); onGoBack();
} }
}, },
{ {
label: 'Forward', label: 'Forward',
click: function () { click: function() {
onGoForward(); onGoForward();
} }
}, },
{ {
label: 'Reload', label: 'Reload',
accelerator: 'CmdOrCtrl+R', accelerator: 'CmdOrCtrl+R',
click: function (item, focusedWindow) { click: function(item, focusedWindow) {
if (focusedWindow) if (focusedWindow) {
focusedWindow.reload(); focusedWindow.reload();
}
} }
}, },
{ {
@ -82,52 +84,54 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
}, },
{ {
label: 'Toggle Full Screen', label: 'Toggle Full Screen',
accelerator: (function () { accelerator: (function() {
if (process.platform == 'darwin') if (process.platform === 'darwin') {
return 'Ctrl+Command+F'; return 'Ctrl+Command+F';
else }
return 'F11'; return 'F11';
})(), })(),
click: function (item, focusedWindow) { click: function(item, focusedWindow) {
if (focusedWindow) if (focusedWindow) {
focusedWindow.setFullScreen(!focusedWindow.isFullScreen()); focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
}
} }
}, },
{ {
label: 'Zoom In', label: 'Zoom In',
accelerator: (function () { accelerator: (function() {
if (process.platform == 'darwin') if (process.platform === 'darwin') {
return 'Command+='; return 'Command+=';
else }
return 'Ctrl+='; return 'Ctrl+=';
})(), })(),
click: function () { click: function() {
onZoomIn(); onZoomIn();
} }
}, },
{ {
label: 'Zoom Out', label: 'Zoom Out',
accelerator: (function () { accelerator: (function() {
if (process.platform == 'darwin') if (process.platform === 'darwin') {
return 'Command+-'; return 'Command+-';
else }
return 'Ctrl+-'; return 'Ctrl+-';
})(), })(),
click: function () { click: function() {
onZoomOut(); onZoomOut();
} }
}, },
{ {
label: 'Toggle Window Developer Tools', label: 'Toggle Window Developer Tools',
accelerator: (function () { accelerator: (function() {
if (process.platform == 'darwin') if (process.platform === 'darwin') {
return 'Alt+Command+I'; return 'Alt+Command+I';
else }
return 'Ctrl+Shift+I'; return 'Ctrl+Shift+I';
})(), })(),
click: function (item, focusedWindow) { click: function(item, focusedWindow) {
if (focusedWindow) if (focusedWindow) {
focusedWindow.toggleDevTools(); focusedWindow.toggleDevTools();
}
} }
} }
] ]
@ -145,7 +149,7 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
label: 'Close', label: 'Close',
accelerator: 'CmdOrCtrl+W', accelerator: 'CmdOrCtrl+W',
role: 'close' role: 'close'
}, }
] ]
}, },
{ {
@ -154,21 +158,21 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
submenu: [ submenu: [
{ {
label: `Built with Nativefier v${nativefierVersion}`, label: `Built with Nativefier v${nativefierVersion}`,
click: function () { click: function() {
shell.openExternal('https://github.com/jiahaog/nativefier') shell.openExternal('https://github.com/jiahaog/nativefier');
} }
}, },
{ {
label: 'Report an Issue', label: 'Report an Issue',
click: function () { click: function() {
shell.openExternal('https://github.com/jiahaog/nativefier/issues') shell.openExternal('https://github.com/jiahaog/nativefier/issues');
} }
} }
] ]
} }
]; ];
if (process.platform == 'darwin') { if (process.platform === 'darwin') {
template.unshift({ template.unshift({
label: 'Electron', label: 'Electron',
submenu: [ submenu: [
@ -200,10 +204,10 @@ function createMenu(nativefierVersion, onQuit, onGoBack, onGoForward, onZoomIn,
{ {
label: 'Quit', label: 'Quit',
accelerator: 'Command+Q', accelerator: 'Command+Q',
click: function () { click: function() {
onQuit(); onQuit();
} }
}, }
] ]
}); });
template[3].submenu.push( template[3].submenu.push(

View File

@ -20,17 +20,18 @@ var mainWindow;
// do nothing for setDockBadge if not OSX // do nothing for setDockBadge if not OSX
let setDockBadge = () => {}; let setDockBadge = () => {};
if (isOSX()) { if (isOSX()) {
setDockBadge = app.dock.setBadge; setDockBadge = app.dock.setBadge;
} }
app.on('window-all-closed', function () { app.on('window-all-closed', function() {
if (!isOSX()) { if (!isOSX()) {
app.quit(); app.quit();
} }
}); });
app.on('activate', function (event, hasVisibleWindows) { app.on('activate', function(event, hasVisibleWindows) {
if (isOSX()) { if (isOSX()) {
// this is called when the dock is clicked // this is called when the dock is clicked
if (!hasVisibleWindows) { if (!hasVisibleWindows) {
@ -39,7 +40,7 @@ app.on('activate', function (event, hasVisibleWindows) {
} }
}); });
app.on('before-quit', function () { app.on('before-quit', function() {
// not fired when the close button on the window is clicked // not fired when the close button on the window is clicked
if (isOSX()) { if (isOSX()) {
// need to force a quit as a workaround here to simulate the osx app hiding behaviour // need to force a quit as a workaround here to simulate the osx app hiding behaviour
@ -51,7 +52,7 @@ app.on('before-quit', function () {
} }
}); });
app.on('ready', function () { app.on('ready', function() {
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge); mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
}); });

View File

@ -2,7 +2,7 @@ var ipcRenderer = require('electron').ipcRenderer;
var form = document.getElementById('login-form'); var form = document.getElementById('login-form');
form.addEventListener('submit', function (event) { form.addEventListener('submit', function(event) {
event.preventDefault(); event.preventDefault();
var username = document.getElementById('username-input').value; var username = document.getElementById('username-input').value;
var password = document.getElementById('password-input').value; var password = document.getElementById('password-input').value;

View File

@ -6,39 +6,38 @@ var electron = require('electron');
var ipc = electron.ipcRenderer; var ipc = electron.ipcRenderer;
var webFrame = electron.webFrame; var webFrame = electron.webFrame;
setNotificationCallback(function (title, opt) { setNotificationCallback(function(title, opt) {
ipc.send('notification', title, opt); ipc.send('notification', title, opt);
}); });
document.addEventListener("DOMContentLoaded", function (event) { document.addEventListener('DOMContentLoaded', function(event) {
// do things // do things
}); });
ipc.on('params', function (event, message) { ipc.on('params', function(event, message) {
var appArgs = JSON.parse(message); var appArgs = JSON.parse(message);
console.log('nativefier.json', appArgs); console.log('nativefier.json', appArgs);
}); });
ipc.on('change-zoom', function (event, message) { ipc.on('change-zoom', function(event, message) {
webFrame.setZoomFactor(message); webFrame.setZoomFactor(message);
}); });
/** /**
* Patches window.Notification to set a callback on a new Notification * Patches window.Notification to set a callback on a new Notification
* @param callback * @param callback
*/ */
function setNotificationCallback(callback) { function setNotificationCallback(callback) {
var oldNotify = window.Notification; var OldNotify = window.Notification;
var newNotify = function (title, opt) { var newNotify = function(title, opt) {
callback(title, opt); callback(title, opt);
return new oldNotify(title, opt); return new OldNotify(title, opt);
}; };
newNotify.requestPermission = oldNotify.requestPermission.bind(oldNotify); newNotify.requestPermission = OldNotify.requestPermission.bind(OldNotify);
Object.defineProperty(newNotify, 'permission', { Object.defineProperty(newNotify, 'permission', {
get: function () { get: function() {
return oldNotify.permission; return OldNotify.permission;
} }
}); });

View File

@ -6,6 +6,7 @@ import babel from 'gulp-babel';
import runSequence from 'run-sequence'; import runSequence from 'run-sequence';
import path from 'path'; import path from 'path';
import childProcess from 'child_process'; import childProcess from 'child_process';
import eslint from 'gulp-eslint';
const PATHS = setUpPaths(); const PATHS = setUpPaths();
@ -45,7 +46,7 @@ gulp.task('build-static', () => {
}); });
gulp.task('watch', ['build'], () => { gulp.task('watch', ['build'], () => {
var handleError = function (error) { var handleError = function(error) {
console.error(error); console.error(error);
}; };
gulp.watch(PATHS.APP_ALL, ['build-app']) gulp.watch(PATHS.APP_ALL, ['build-app'])
@ -61,9 +62,18 @@ gulp.task('publish', done => {
}); });
gulp.task('release', callback => { gulp.task('release', callback => {
return runSequence('build', 'publish', callback); return runSequence('test', 'build', 'publish', callback);
}); });
gulp.task('lint', function() {
return gulp.src(['**/*.js', '!node_modules/**', '!app/node_modules/**', '!app/lib/**', '!lib/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('test', ['lint']);
function setUpPaths() { function setUpPaths() {
const paths = { const paths = {
WEBPACK_CONFIG: './webpack.config.js', WEBPACK_CONFIG: './webpack.config.js',

View File

@ -12,11 +12,11 @@
"main": "lib/buildApp.js", "main": "lib/buildApp.js",
"scripts": { "scripts": {
"dev-up": "npm install && (cd app && npm install)", "dev-up": "npm install && (cd app && npm install)",
"test": "echo \"Error: no test specified\" && exit 1", "test": "gulp test",
"clean": "gulp clean", "clean": "gulp clean",
"build": "gulp build", "build": "gulp build",
"watch": "while true ; do gulp watch ; done", "watch": "while true ; do gulp watch ; done",
"package-placeholder": "npm run build && node lib/cli.js http://www.medium.com ~/Desktop --overwrite && open ~/Desktop/Medium-darwin-x64/Medium.app", "package-placeholder": "npm run build && node lib/cli.js http://www.bennish.net/web-notifications.html ~/Desktop --overwrite --app-name notification-test && open ~/Desktop/notification-test-darwin-x64/notification-test.app",
"start-placeholder": "npm run build && electron app", "start-placeholder": "npm run build && electron app",
"release": "gulp release" "release": "gulp release"
}, },
@ -55,8 +55,10 @@
"babel-preset-es2015": "^6.3.13", "babel-preset-es2015": "^6.3.13",
"del": "^2.2.0", "del": "^2.2.0",
"electron-prebuilt": "^0.36.5", "electron-prebuilt": "^0.36.5",
"eslint-config-google": "^0.3.0",
"gulp": "^3.9.0", "gulp": "^3.9.0",
"gulp-babel": "^6.1.1", "gulp-babel": "^6.1.1",
"gulp-eslint": "^1.1.1",
"gulp-sourcemaps": "^1.6.0", "gulp-sourcemaps": "^1.6.0",
"run-sequence": "^1.1.5", "run-sequence": "^1.1.5",
"webpack-stream": "^3.1.0" "webpack-stream": "^3.1.0"

View File

@ -20,7 +20,7 @@ const copy = ncp.ncp;
/** /**
* *
* @param options * @param {{}} options
* @param {buildAppCallback} callback * @param {buildAppCallback} callback
*/ */
function buildApp(options, callback) { function buildApp(options, callback) {
@ -45,7 +45,6 @@ function buildApp(options, callback) {
], callback); ], callback);
} }
/** /**
* @callback tempDirCallback * @callback tempDirCallback
* @param error * @param error

View File

@ -10,7 +10,6 @@ import buildApp from './buildApp';
const packageJson = require(path.join('..', 'package')); const packageJson = require(path.join('..', 'package'));
function main(program) { function main(program) {
async.waterfall([ async.waterfall([
callback => { callback => {
optionsFactory( optionsFactory(
@ -48,7 +47,7 @@ if (require.main === module) {
program program
.version(packageJson.version) .version(packageJson.version)
.arguments('<targetUrl> [dest]') .arguments('<targetUrl> [dest]')
.action(function (targetUrl, appDir) { .action(function(targetUrl, appDir) {
program.targetUrl = targetUrl; program.targetUrl = targetUrl;
program.outDir = appDir; program.outDir = appDir;
}) })

View File

@ -25,7 +25,6 @@ function optionsFactory(name,
userAgent, userAgent,
honest = false, honest = false,
callback) { callback) {
targetUrl = normalizeUrl(targetUrl); targetUrl = normalizeUrl(targetUrl);
if (!width) { if (!width) {
@ -70,7 +69,7 @@ function optionsFactory(name,
return; return;
} }
getTitle(options.targetUrl, function (error, pageTitle) { getTitle(options.targetUrl, function(error, pageTitle) {
if (error) { if (error) {
console.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`); console.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`);
options.name = DEFAULT_APP_NAME; options.name = DEFAULT_APP_NAME;
@ -116,7 +115,7 @@ function getTitle(url, callback) {
} }
const $ = cheerio.load(body); const $ = cheerio.load(body);
const pageTitle = $("title").text().replace(/\//g, ""); const pageTitle = $('title').text().replace(/\//g, '');
callback(null, pageTitle); callback(null, pageTitle);
}); });
} }
@ -146,6 +145,8 @@ function getFakeUserAgent() {
case 'linux': case 'linux':
userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36'; userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36';
break; break;
default:
break;
} }
return userAgent; return userAgent;
} }

View File

@ -1,20 +1,18 @@
var path = require('path');
var fs = require('fs'); var fs = require('fs');
//http://jlongster.com/Backend-Apps-with-Webpack--Part-I // http://jlongster.com/Backend-Apps-with-Webpack--Part-I
// set all modules in node_modules as external // set all modules in node_modules as external
var nodeModules = {}; var nodeModules = {};
fs fs.readdirSync('./app/node_modules')
.readdirSync('./app/node_modules') .filter(function(x) {
.filter(function (x) {
return ['.bin'].indexOf(x) === -1; return ['.bin'].indexOf(x) === -1;
}) })
.forEach(function (mod) { .forEach(function(mod) {
nodeModules[mod] = 'commonjs ' + mod; nodeModules[mod] = 'commonjs ' + mod;
}); });
// add electron to external module // add electron to external module
nodeModules['electron'] = 'commonjs electron'; nodeModules.electron = 'commonjs electron';
module.exports = { module.exports = {
target: 'node', target: 'node',
@ -28,7 +26,7 @@ module.exports = {
externals: nodeModules, externals: nodeModules,
module: { module: {
loaders: [ loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"} {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'}
] ]
} }
}; };