From 09da9c82b8bd0836dfb13cba8d64a9040974efa1 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Wed, 22 Oct 2014 15:05:39 -0600 Subject: [PATCH] #3 Template writing to disk should be atomic --- runtime/loader.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/runtime/loader.js b/runtime/loader.js index 4450d9b71..f2c2e78eb 100644 --- a/runtime/loader.js +++ b/runtime/loader.js @@ -21,6 +21,7 @@ function loadSource(templatePath, compiledSrc) { module.exports = function load(templatePath) { templatePath = nodePath.resolve(cwd, templatePath); + var targetDir = nodePath.dirname(templatePath); var targetFile = templatePath + '.js'; var compiler = markoCompiler.createCompiler(templatePath); @@ -34,7 +35,10 @@ module.exports = function load(templatePath) { // console.log('Compiled code for "' + templatePath + '":\n' + compiledSrc); - fs.writeFileSync(targetFile, compiledSrc, {encoding: 'utf8'}); + var filename = nodePath.basename(targetFile); + var tempFile = nodePath.join(targetDir, '.' + process.pid + '.' + Date.now() + '.' + filename); + fs.writeFileSync(tempFile, compiledSrc, {encoding: 'utf8'}); + fs.renameSync(tempFile, targetFile); return require(targetFile); };