Avoid writing CLI app's elm.json to disk if the contents are exactly what would be written. Helps the Elm compiler speed up by around 1 second.

This commit is contained in:
Dillon Kearns 2021-07-10 13:57:31 -07:00
parent 0dbb2138db
commit 335ff5f0b1

View File

@ -1,10 +1,13 @@
const fs = require("fs");
module.exports = function () {
var elmJson = JSON.parse(fs.readFileSync("./elm.json").toString());
module.exports = async function () {
var elmJson = JSON.parse(
(await fs.promises.readFile("./elm.json")).toString()
);
// write new elm.json
fs.writeFileSync(
await writeFileIfChanged(
"./elm-stuff/elm-pages/elm.json",
JSON.stringify(rewriteElmJson(elmJson))
);
@ -28,3 +31,18 @@ function rewriteElmJson(elmJson) {
elmJson["source-directories"].push(".elm-pages");
return elmJson;
}
async function writeFileIfChanged(filePath, content) {
if (
!(await fileExists(filePath)) ||
(await fs.promises.readFile(filePath, "utf8")) !== content
) {
await fs.promises.writeFile(filePath, content);
}
}
function fileExists(file) {
return fs.promises
.access(file, fs.constants.F_OK)
.then(() => true)
.catch(() => false);
}