Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function sendModifiedFile(filePath, ws) {
const fileContents = fs.readFileSync(filePath, {
encoding: 'utf8',
});
if (fileContents.endsWith('(this));')) {
// If we got the complete file, send the modified contents.
const modifiedContents = elmHot.inject(fileContents);
ws.send(
JSON.stringify({
type: 'fileChanged',
contents: modifiedContents,
})
);
} else {
// Else, try again after a few milliseconds.
setTimeout(() => {
sendModifiedFile(filePath, ws);
}, 100);
}
}
app.get('/build', (req, res) => {
const filePath = untildify(decodeURI(req.query.path));
let fileContents = null;
try {
fileContents = fs.readFileSync(filePath);
} catch (err) {}
if (fileContents) {
const hotReloadingHost = atom.config.get('elmjutsu.hotReloadingHost');
res.send(
hotReloadingCode
.replace('HOST', hotReloadingHost)
.replace('PORT', this.server.address().port)
.replace('FILE_PATH', filePath) +
'\n' +
elmHot.inject(fileContents)
);
} else {
res.sendStatus(404);
}
});