Add watchers when file reads and globs are encountered in the renderer.

This commit is contained in:
Dillon Kearns 2021-04-12 21:44:57 -07:00
parent fcada0839e
commit e8f0ffe7b6
2 changed files with 22 additions and 4 deletions

View File

@ -167,7 +167,15 @@ async function handleNavigationRequest(req, res) {
} else {
try {
await pendingCliCompile;
const renderResult = await renderer(compiledElmPath, req.url, req);
const renderResult = await renderer(
compiledElmPath,
req.url,
req,
function (pattern) {
console.log(`Watching data source ${pattern}`);
watcher.add(pattern);
}
);
if (renderResult.kind === "json") {
res.writeHead(200, {
"Content-Type": "application/json",

View File

@ -19,11 +19,17 @@ module.exports =
* @param {string} compiledElmPath
* @param {string} path
* @param {import('aws-lambda').APIGatewayProxyEvent} request
* @param {(pattern: string) => void} addDataSourceWatcher
* @returns
*/
async function run(compiledElmPath, path, request) {
async function run(compiledElmPath, path, request, addDataSourceWatcher) {
XMLHttpRequest = require("xhr2");
const result = await runElmApp(compiledElmPath, path, request);
const result = await runElmApp(
compiledElmPath,
path,
request,
addDataSourceWatcher
);
return result;
};
@ -31,9 +37,10 @@ module.exports =
* @param {string} compiledElmPath
* @param {string} pagePath
* @param {import('aws-lambda').APIGatewayProxyEvent} request
* @param {(pattern: string) => void} addDataSourceWatcher
* @returns {Promise<({ kind: 'json'; contentJson: string} | { kind: 'html'; htmlString: string })>}
*/
function runElmApp(compiledElmPath, pagePath, request) {
function runElmApp(compiledElmPath, pagePath, request, addDataSourceWatcher) {
return new Promise((resolve, reject) => {
console.time(`renderer-${pagePath}`);
const isJson = pagePath.match(/content\.json\/?$/);
@ -82,6 +89,8 @@ function runElmApp(compiledElmPath, pagePath, request) {
} else if (fromElm.tag === "ReadFile") {
const filePath = fromElm.args[0];
addDataSourceWatcher(filePath);
const fileContents = (
await fsPromises.readFile(path.join(process.cwd(), filePath))
).toString();
@ -97,6 +106,7 @@ function runElmApp(compiledElmPath, pagePath, request) {
});
} else if (fromElm.tag === "Glob") {
const globPattern = fromElm.args[0];
addDataSourceWatcher(globPattern);
const globResult = await globby(globPattern);
const captures = globResult.map((result) => {
return {