From 89e91939e4afb95d0c3026977fa34df4885e3970 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 18 May 2022 14:07:32 -0700 Subject: [PATCH] Write theme files atomically --- styles/src/buildThemes.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/styles/src/buildThemes.ts b/styles/src/buildThemes.ts index 520a37395d5f1775ac37c465ec4f81ac5121b0ef..dad56046a9fba08dae0fa5c2a6c5e30022479542 100644 --- a/styles/src/buildThemes.ts +++ b/styles/src/buildThemes.ts @@ -1,23 +1,27 @@ import * as fs from "fs"; import * as path from "path"; +import { tmpdir } from 'os'; import app from "./styleTree/app"; import themes from "./themes"; import snakeCase from "./utils/snakeCase"; const themeDirectory = `${__dirname}/../../assets/themes/`; +const tempDirectory = fs.mkdtempSync(path.join(tmpdir(), 'build-themes')); // Clear existing themes for (const file of fs.readdirSync(themeDirectory)) { - fs.unlinkSync(path.join(themeDirectory, file)); + if (file.endsWith('.json')) { + fs.unlinkSync(path.join(themeDirectory, file)); + } } // Write new themes to theme directory for (let theme of themes) { let styleTree = snakeCase(app(theme)); let styleTreeJSON = JSON.stringify(styleTree, null, 2); - let outPath = path.resolve( - `${__dirname}/../../assets/themes/${theme.name}.json` - ); - fs.writeFileSync(outPath, styleTreeJSON); + let tempPath = path.join(tempDirectory, `${theme.name}.json`); + let outPath = path.join(themeDirectory, `${theme.name}.json`); + fs.writeFileSync(tempPath, styleTreeJSON); + fs.renameSync(tempPath, outPath); console.log(`- ${outPath} created`); }