Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 4, 2018
0 parents commit 5b4223c
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
@@ -0,0 +1,2 @@
* text=auto
*.js text eol=lf
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
os: osx
language: node_js
node_js:
- 'node'
71 changes: 71 additions & 0 deletions index.js
@@ -0,0 +1,71 @@
'use strict';
const path = require('path');
const electron = require('electron');
const chokidar = require('chokidar');
const isDev = require('electron-is-dev');

function getMainProcessPaths(topModuleObj) {
const cwd = path.dirname(topModuleObj.filename);
const paths = new Set([topModuleObj.filename]);

const getPaths = moduleObj => {
for (const child of moduleObj.children) {
if (path.relative(cwd, child.filename).includes('node_modules')) {
continue;
}

paths.add(child.filename);
getPaths(child);
}
};

getPaths(topModuleObj);

return paths;
}

module.exports = (moduleObj, options = {}) => {
// This module should be a dev dependency, but guard
// this in case the user included it as a dependency
if (!isDev) {
return;
}

if (!moduleObj) {
throw new Error('You have to pass the `module` object');
}

const cwd = path.dirname(moduleObj.filename);
const mainProcessPaths = getMainProcessPaths(moduleObj);

const watcher = chokidar.watch(cwd, {
cwd,
disableGlobbing: true,
ignored: [
/(^|[/\\])\../, // Dotfiles
'node_modules',
'**/*.map'
].concat(options.ignore)
});

if (options.debug) {
watcher.on('ready', () => {
console.log('Watched paths:', watcher.getWatched());
});
}

watcher.on('change', filePath => {
if (options.debug) {
console.log('File changed:', filePath);
}

if (mainProcessPaths.has(path.join(cwd, filePath))) {
electron.app.relaunch();
electron.app.exit(0);
} else {
for (const win of electron.BrowserWindow.getAllWindows()) {
win.webContents.reloadIgnoringCache();
}
}
});
};
9 changes: 9 additions & 0 deletions license
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 changes: 52 additions & 0 deletions package.json
@@ -0,0 +1,52 @@
{
"name": "electron-reloader",
"version": "0.0.0",
"description": "Simple auto-reloading for Electron apps during development",
"license": "MIT",
"repository": "sindresorhus/electron-reloader",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"scripts": {
"test": "xo",
"start": "electron test"
},
"files": [
"index.js"
],
"keywords": [
"electron",
"reload",
"reloader",
"livereload",
"auto-reload",
"live-reload",
"refresh",
"restart",
"watch",
"watcher",
"watching",
"monitor",
"hot",
"files",
"fs",
"dev",
"development"
],
"dependencies": {
"chokidar": "^2.0.0",
"electron-is-dev": "^0.3.0"
},
"devDependencies": {
"electron": "^1.7.10",
"xo": "*"
},
"xo": {
"envs": [
"node",
"browser"
]
}
}
76 changes: 76 additions & 0 deletions readme.md
@@ -0,0 +1,76 @@
# electron-reloader [![Build Status](https://travis-ci.org/sindresorhus/electron-reloader.svg?branch=master)](https://travis-ci.org/sindresorhus/electron-reloader)

> Simple auto-reloading for Electron apps during development
It *just works*. When files used in the main process are changed, the app is restarted, and when files used in the browser window are changed, the page is reloaded.


## Install

```
$ npm install --save-dev electron-reloader
```


## Usage

The following must be included in the app entry file, usually named `index.js`:

```js
try {
require('electron-reloader')(module);
} catch (err) {}
```

You have to pass the `module` object so we can read the module graph and figure out which files belong to the main process.

The `try/catch` is needed so it doesn't throw `Cannot find module 'electron-reloader'` in production.


## API

### reloader(module, [options])

#### module

Type: `Object`

The global `module` object.

#### options

Type: `Object`

##### debug

Type: `boolean`<br>
Default: `false`

Prints watched paths and when files change. Can be useful to make sure you set it up correctly.

##### ignore

Type: `Array<string|RegExp>`

Ignore patterns passed to [`chokidar`](https://github.com/paulmillr/chokidar#path-filtering). By default, files/directories starting with a `.`, `.map` files, and `node_modules` directories are ignored. This option is additive to those.


## Tip

### Using it with webpack watch mode

Just add the source directory to the `ignore` option. The dist directory is already watched, so when a source file changes, webpack will build it and output it to the dist directory, which this module will detect.


## Related

- [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules
- [electron-debug](https://github.com/sindresorhus/electron-debug) - Adds useful debug features to your Electron app
- [electron-context-menu](https://github.com/sindresorhus/electron-context-menu) - Context menu for your Electron app
- [electron-dl](https://github.com/sindresorhus/electron-dl) - Simplified file downloads for your Electron app
- [electron-unhandled](https://github.com/sindresorhus/electron-unhandled) - Catch unhandled errors and promise rejections in your Electron app


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
9 changes: 9 additions & 0 deletions test/index.html
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Test</h1>
</body>
</html>
11 changes: 11 additions & 0 deletions test/index.js
@@ -0,0 +1,11 @@
'use strict';
const electron = require('electron');

require('..')(module, {
debug: true
});

electron.app.on('ready', () => {
const win = new electron.BrowserWindow();
win.loadURL(`file://${__dirname}/index.html`);
});
5 changes: 5 additions & 0 deletions test/readme.md
@@ -0,0 +1,5 @@
# Testing

The tests are manual as I have no idea how to automate this.

From the root directory, run `npm start`, and then edit `index.html` to see the window refresh and `index.js` to see the app restart.

0 comments on commit 5b4223c

Please sign in to comment.