Skip to content

Commit

Permalink
Merge pull request #43 from catdad/#30-watchboy
Browse files Browse the repository at this point in the history
using watchboy to watch for changes
  • Loading branch information
catdad authored Jan 9, 2020
2 parents d873864 + ae00414 commit 0b2a2a3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
},
"dependencies": {
"chalk": "^2.4.2",
"chokidar": "^2.1.8",
"import-from": "^3.0.0",
"runtime-required": "^1.0.2"
"runtime-required": "^1.0.2",
"watchboy": "^0.4.0"
},
"keywords": [
"electron",
Expand Down
6 changes: 4 additions & 2 deletions src/electronmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ function restartApp() {
function startWatcher(done) {
const watcher = watch();

watcher.on('change', relpath => {
watcher.on('change', ({ path: fullpath }) => {
const relpath = path.relative(root, fullpath);
const filepath = path.resolve(root, relpath);
const type = 'change';

Expand All @@ -121,7 +122,8 @@ function startWatcher(done) {
}
});

watcher.on('add', relpath => {
watcher.on('add', ({ path: fullpath }) => {
const relpath = path.relative(root, fullpath);
log.verbose('watching new file:', relpath);
});

Expand Down
12 changes: 3 additions & 9 deletions src/watch.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
const fs = require('fs');
const path = require('path');

const chokidar = require('chokidar');
const watchboy = require('watchboy');

const root = fs.realpathSync(path.resolve('.'));

module.exports = () => {
const watcher = chokidar.watch('.', {
cwd: root,
ignored: [
/(^|[/\\])\../, // Dotfiles
'node_modules',
'**/*.map'
]
const watcher = watchboy(['**/*', '!node_modules', '!.*', '!**/*.map'], {
cwd: root
});

return watcher;
Expand Down
28 changes: 23 additions & 5 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ describe('integration', () => {
};

const waitFor = (stream, regex) => {
return new Promise(resolve => {
const err = new Error(`did not find ${regex.toString()}`);

return new Promise((resolve, reject) => {
const onReadable = () => {
stream.resume();
};

const timer = setTimeout(() => {
stream.pause();
stream.removeListener('readable', onReadable);
stream.removeListener('data', onLine);
reject(err);
}, 5000);

const onLine = line => {
stream.pause();

if (regex.test(line)) {
clearTimeout(timer);
stream.removeListener('readable', onReadable);
stream.removeListener('data', onLine);
return resolve();
Expand All @@ -62,16 +72,24 @@ describe('integration', () => {

function runTests(realRoot, cwd) {
let app;
let stdout;

const file = fixturename => {
return path.resolve(realRoot, fixturename);
};

afterEach(async () => {
afterEach(async function () {
if (!app) {
return;
}

if (this.currentTest.state === 'failed' && stdout) {
// eslint-disable-next-line no-console
console.log(stdout._getLines());
}

stdout = null;

const tmp = app;
app = null;

Expand All @@ -98,7 +116,7 @@ describe('integration', () => {
stdio: ['ignore', 'pipe', 'pipe']
});

const stdout = collect(wrap(app.stdout));
stdout = collect(wrap(app.stdout));

await ready(stdout);

Expand Down Expand Up @@ -134,7 +152,7 @@ describe('integration', () => {
stdio: ['ignore', 'pipe', 'pipe']
});

const stdout = collect(wrap(app.stdout));
stdout = collect(wrap(app.stdout));

await waitFor(stdout, /pineapples/);
await waitFor(stdout, /waiting for any change to restart the app/);
Expand Down Expand Up @@ -167,7 +185,7 @@ describe('integration', () => {
stdio: ['ignore', 'pipe', 'pipe']
});

const stdout = collect(wrap(app.stdout));
stdout = collect(wrap(app.stdout));

await waitFor(stdout, /uncaught exception occured/),
await waitFor(stdout, /waiting for any change to restart the app/);
Expand Down

0 comments on commit 0b2a2a3

Please sign in to comment.