Skip to content
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
118 changes: 115 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"body-parser": "^1.18.3",
"boxen": "^1.3.0",
"chalk": "^2.4.2",
"chokidar": "^3.2.3",
"columnify": "^1.5.4",
"common-tags": "^1.8.0",
"conf": "^5.0.0",
Expand All @@ -61,6 +62,7 @@
"is-ci": "^2.0.0",
"listr": "^0.14.3",
"lodash.camelcase": "^4.3.0",
"lodash.debounce": "^4.0.8",
"lodash.kebabcase": "^4.1.1",
"lodash.startcase": "^4.4.0",
"log-symbols": "^2.2.0",
Expand Down Expand Up @@ -88,6 +90,7 @@
"@types/got": "^9.6.0",
"@types/jest": "^24.0.15",
"@types/listr": "^0.14.0",
"@types/lodash.debounce": "^4.0.6",
"@types/lodash.kebabcase": "^4.1.6",
"@types/lodash.startcase": "^4.4.6",
"@types/prompts": "^2.0.1",
Expand Down
44 changes: 42 additions & 2 deletions src/runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import express, {
} from 'express';
import userAgentMiddleware from 'express-useragent';
import nocache from 'nocache';
import { printRouteInfo } from '../printers/start';
import chokidar from 'chokidar';
import debounce from 'lodash.debounce';
import path from 'path';
import { StartCliConfig } from '../config/start';
import { wrapErrorInHtml } from '../utils/error-html';
import { getDebugFunction } from '../utils/logger';
Expand All @@ -18,6 +22,7 @@ import { constructGlobalScope, functionToRoute } from './route';

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

function requireUncached(module: string): any {
Expand Down Expand Up @@ -80,8 +85,43 @@ export async function createServer(
});
}

const routes = await getFunctionsAndAssets(config.baseDir);
const routeMap = setRoutes(routes);
let routes = await getFunctionsAndAssets(config.baseDir);
let routeMap = setRoutes(routes);

if (config.live) {
const watcher = chokidar.watch(
[
path.join(config.baseDir, '/(functions|src)/**/*.js'),
path.join(config.baseDir, '/(assets|static)/**/*'),
],
{
ignoreInitial: true
}
);

const reloadRoutes = async () => {
routes = await getFunctionsAndAssets(config.baseDir);
routeMap = setRoutes(routes);

await printRouteInfo(config);
};

// Debounce so we don't needlessly reload when multiple files are changed
const debouncedReloadRoutes = debounce(reloadRoutes, RELOAD_DEBOUNCE_MS);

watcher
.on('add', path => {
debug(`Reloading Routes: add @ ${path}`);
debouncedReloadRoutes();
})
.on('unlink', path => {
debug(`Reloading Routes: unlink @ ${path}`);
debouncedReloadRoutes();
});

// Clean the watcher up when exiting.
process.on('exit', () => watcher.close());
}

constructGlobalScope(config);

Expand Down