Don’t write to configuration.yaml when it didn’t change. https://github.com/Koenkk/zigbee2mqtt/issues/2071

This commit is contained in:
Koen Kanters committed 2019-10-03 20:06:31 +02:00
1 parent e7bf5ee648
commit 732a97ba0c
6 files changed
+31 -8

No files matched your search

+3 -3
View File
@@ -244,16 +244,16 @@ function write() {
// Read settings to check if we have to split devices/groups into separate file.
const actual = yaml.read(file);
if (typeof actual.devices === 'string') {
yaml.write(data.joinPath(actual.devices), settings.devices);
yaml.writeIfChanged(data.joinPath(actual.devices), settings.devices);
toWrite.devices = actual.devices;
}
if (typeof actual.groups === 'string') {
yaml.write(data.joinPath(actual.groups), settings.groups);
yaml.writeIfChanged(data.joinPath(actual.groups), settings.groups);
toWrite.groups = actual.groups;
}
yaml.write(file, toWrite);
yaml.writeIfChanged(file, toWrite);
_settings = read();
_settingsWithDefaults = objectAssignDeep.noMutate(defaults, get());
+7 -3
View File
@@ -1,5 +1,6 @@
const yaml = require('js-yaml');
const fs = require('fs');
const equals = require('fast-deep-equal');
function read(file) {
try {
@@ -25,8 +26,11 @@ function readIfExists(file) {
return fs.existsSync(file) ? read(file) : null;
}
function write(file, content) {
fs.writeFileSync(file, yaml.safeDump(content));
function writeIfChanged(file, content) {
const before = readIfExists(file);
if (!equals(before, content)) {
fs.writeFileSync(file, yaml.safeDump(content));
}
}
module.exports = {read, readIfExists, write};
module.exports = {read, readIfExists, writeIfChanged};
+1
View File
@@ -35,6 +35,7 @@
"dependencies": {
"ajv": "*",
"debounce": "*",
"fast-deep-equal": "*",
"git-last-commit": "*",
"js-yaml": "*",
"mkdir-recursive": "*",
+7
View File
@@ -380,4 +380,11 @@ describe('Controller', () => {
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(0);
});
it('Should start when state is corrupted', async () => {
fs.writeFileSync(path.join(data.mockDir, 'state.json'), 'corrupted');
await controller.start();
await flushPromises();
expect(controller.state.state).toStrictEqual({});
});
});
+11
View File
@@ -463,4 +463,15 @@ describe('Settings', () => {
settings.changeFriendlyName('myname1', 'myname');
}).toThrowError(`friendly_name 'myname' is already in use`);
});
it('Shouldnt write to configuration.yaml when there are no changes in it', () => {
const contentConfiguration = {devices: 'devices.yaml'};
const contentDevices = {};
write(configurationFile, contentConfiguration);
const before = fs.statSync(configurationFile).mtimeMs;
write(devicesFile, contentDevices);
settings.addDevice('0x1234');
const after = fs.statSync(configurationFile).mtimeMs;
expect(before).toBe(after);
});
});
+2 -2
View File
@@ -132,11 +132,11 @@ function writeDefaultConfiguration() {
}
};
yaml.write(path.join(mockDir, 'configuration.yaml'), config);
yaml.writeIfChanged(path.join(mockDir, 'configuration.yaml'), config);
}
function writeEmptyState() {
yaml.write(path.join(mockDir, 'state.json'), JSON.stringify({}));
fs.writeFileSync(path.join(mockDir, 'state.json'), JSON.stringify({}));
}
function writeDefaultState() {