Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Fix/207 linux hot reloading #223

Merged
merged 8 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"adm-zip": "^0.4.11",
"ajv": "^6.5.2",
"body-parser": "^1.18.3",
"chokidar": "^2.0.4",
"cli-table2": "0.2.0",
"colors": "1.1.2",
"configstore": "^3.1.2",
Expand Down
37 changes: 20 additions & 17 deletions src/supervisor/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
const _ = require('lodash');
const bodyParser = require('body-parser');
const express = require('express');
const fs = require('fs');
const chokidar = require('chokidar');
const path = require('path');
const querystring = require('querystring');
const serializerr = require('serializerr');
Expand All @@ -40,13 +40,13 @@ const loadHandler = {
_originalLoader = Module._load;
Module._load = function (...args) {
const override = handler.onRequire(process.env['FUNCTION_NAME'], args[0]);
return (override || _originalLoader.apply(this, args));
return override || _originalLoader.apply(this, args);
};
}
};

function main () {
process.on('message', (message) => {
process.on('message', message => {
const name = message.name;
const cloudfunction = message.cloudfunction;
const localdir = getLocaldir(cloudfunction);
Expand All @@ -67,7 +67,9 @@ function main () {
console.log('Mock handler found. Require calls will be intercepted');
}
} catch (e) {
console.error('Mocks enabled but no mock handler found. Require calls will NOT be intercepted');
console.error(
'Mocks enabled but no mock handler found. Require calls will NOT be intercepted'
);
console.error(e);
}
}
Expand Down Expand Up @@ -155,7 +157,7 @@ function main () {
} else {
return Promise.resolve()
.then(() => handler(req.body))
.then((result) => {
.then(result => {
errback(null, result);
})
.catch(errback);
Expand All @@ -178,26 +180,27 @@ function main () {

// Only start watching for file changes if the funciton is not in debug mode
if (localdir && !message.debug && message.watch) {
fs.watch(localdir, {
recursive: true
}, (event, filename) => {
// Ignore node_modules
if (Array.isArray(message.watchIgnore)) {
for (let i = 0; i < message.watchIgnore.length; i++) {
if ((new RegExp(message.watchIgnore[i])).test(filename)) {
return;
}
}
}
const watcher = chokidar.watch(localdir, {
ignoreInitial: true,
persistent: false,
ignored: message.watchIgnore
});

const reloadServer = () => {
process.send({
close: true
});
server.close(() => {
console.log(`Worker for ${name} closed due to file changes.`);
process.exit();
});
});
};

watcher
.on('change', reloadServer)
.on('add', reloadServer)
.on('unlink', reloadServer)
.on('error', err => console.error(err));
}
});

Expand Down