Skip to content

Commit 955efbe

Browse files
imnotjamesdkundel
andcommitted
feat: support loading of new files automatically with --live (#94)
* add chokidar and debouncing depdendencies * use chokidar to watch for changes and reload this utilizes the chokidar watcher to watch all JS files under the functions directory and for all items within the assets directories. this also uses the lodash debouncer to prevent multiple files changing in quick succession (250ms) from causing too many reloads. * fix(server): adjust file watcher paths * style(server): fix spacing of imports Co-authored-by: Dominik Kundel <dkundel@twilio.com>
1 parent 8689f00 commit 955efbe

File tree

3 files changed

+160
-5
lines changed

3 files changed

+160
-5
lines changed

package-lock.json

Lines changed: 115 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"body-parser": "^1.18.3",
4949
"boxen": "^1.3.0",
5050
"chalk": "^2.4.2",
51+
"chokidar": "^3.2.3",
5152
"columnify": "^1.5.4",
5253
"common-tags": "^1.8.0",
5354
"conf": "^5.0.0",
@@ -61,6 +62,7 @@
6162
"is-ci": "^2.0.0",
6263
"listr": "^0.14.3",
6364
"lodash.camelcase": "^4.3.0",
65+
"lodash.debounce": "^4.0.8",
6466
"lodash.kebabcase": "^4.1.1",
6567
"lodash.startcase": "^4.4.0",
6668
"log-symbols": "^2.2.0",
@@ -88,6 +90,7 @@
8890
"@types/got": "^9.6.0",
8991
"@types/jest": "^24.0.15",
9092
"@types/listr": "^0.14.0",
93+
"@types/lodash.debounce": "^4.0.6",
9194
"@types/lodash.kebabcase": "^4.1.6",
9295
"@types/lodash.startcase": "^4.4.6",
9396
"@types/prompts": "^2.0.1",

src/runtime/server.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import express, {
88
} from 'express';
99
import userAgentMiddleware from 'express-useragent';
1010
import nocache from 'nocache';
11+
import { printRouteInfo } from '../printers/start';
12+
import chokidar from 'chokidar';
13+
import debounce from 'lodash.debounce';
14+
import path from 'path';
1115
import { StartCliConfig } from '../config/start';
1216
import { wrapErrorInHtml } from '../utils/error-html';
1317
import { getDebugFunction } from '../utils/logger';
@@ -18,6 +22,7 @@ import { constructGlobalScope, functionToRoute } from './route';
1822

1923
const debug = getDebugFunction('twilio-run:server');
2024
const DEFAULT_PORT = process.env.PORT || 3000;
25+
const RELOAD_DEBOUNCE_MS = 250;
2126
const DEFAULT_BODY_SIZE_LAMBDA = '6mb';
2227

2328
function requireUncached(module: string): any {
@@ -80,8 +85,43 @@ export async function createServer(
8085
});
8186
}
8287

83-
const routes = await getFunctionsAndAssets(config.baseDir);
84-
const routeMap = setRoutes(routes);
88+
let routes = await getFunctionsAndAssets(config.baseDir);
89+
let routeMap = setRoutes(routes);
90+
91+
if (config.live) {
92+
const watcher = chokidar.watch(
93+
[
94+
path.join(config.baseDir, '/(functions|src)/**/*.js'),
95+
path.join(config.baseDir, '/(assets|static)/**/*'),
96+
],
97+
{
98+
ignoreInitial: true
99+
}
100+
);
101+
102+
const reloadRoutes = async () => {
103+
routes = await getFunctionsAndAssets(config.baseDir);
104+
routeMap = setRoutes(routes);
105+
106+
await printRouteInfo(config);
107+
};
108+
109+
// Debounce so we don't needlessly reload when multiple files are changed
110+
const debouncedReloadRoutes = debounce(reloadRoutes, RELOAD_DEBOUNCE_MS);
111+
112+
watcher
113+
.on('add', path => {
114+
debug(`Reloading Routes: add @ ${path}`);
115+
debouncedReloadRoutes();
116+
})
117+
.on('unlink', path => {
118+
debug(`Reloading Routes: unlink @ ${path}`);
119+
debouncedReloadRoutes();
120+
});
121+
122+
// Clean the watcher up when exiting.
123+
process.on('exit', () => watcher.close());
124+
}
85125

86126
constructGlobalScope(config);
87127

0 commit comments

Comments
 (0)