Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private async doWithPreferenceFile(withPreferenceFile: WithPreferenceFile): Promise {
await lock(this.filePath, { retries: 5 });
const prefs = await this.read();
let result;
try {
result = await withPreferenceFile(prefs);
if (result.save) {
await fs.writeJson(this.filePath, prefs);
}
} catch (e) {
logger.error(`Operation on preference file failed: ${e.message}`);
}
await unlock(this.filePath);
return result.value as V;
}
.catch(err => {
locker.unlock(filepath, { realpath: false }).catch(unlockErr => {
logger.error(`Failed to unlock ${filepath}: ${unlockErr}`);
});
return Q.reject(err);
});
}
await new Promise((resolve, reject) => lockfile.unlock(resolved, {
realpath: false
}, err => err ? reject(err) : resolve()));
}