|
1 | 1 | // From jonschlinkert/data-store |
2 | 2 | // Extracted to replace debounce with throttle |
3 | 3 |
|
4 | | -import { writeFile as _writeFile, readFileSync, existsSync, mkdirSync, unlinkSync } from "fs"; |
5 | | -import { homedir } from "os"; |
6 | | -import { join, dirname as _dirname, relative, sep } from "path"; |
7 | | -import { strictEqual } from "assert"; |
| 4 | +import { |
| 5 | + writeFile as _writeFile, |
| 6 | + readFileSync, |
| 7 | + readdirSync, |
| 8 | + existsSync, |
| 9 | + mkdirSync, |
| 10 | + unlinkSync, |
| 11 | +} from "fs"; |
| 12 | +import {homedir} from "os"; |
| 13 | +import path, {join, dirname as _dirname, relative, sep} from "path"; |
| 14 | +import {strictEqual} from "assert"; |
8 | 15 | const XDG_CONFIG_HOME = process.env.XDG_CONFIG_HOME; |
9 | 16 | import throttle from "./throttle"; |
10 | 17 | const flatten = (...args) => [].concat.apply([], args); |
@@ -252,7 +259,7 @@ class Store { |
252 | 259 | * @api public |
253 | 260 | */ |
254 | 261 |
|
255 | | - json(replacer = null, space = this.indent) { |
| 262 | + json(data, replacer = null, space = this.indent) { |
256 | 263 | function stringify(obj, replacer, spaces, cycleReplacer) { |
257 | 264 | return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces); |
258 | 265 | } |
@@ -281,7 +288,7 @@ class Store { |
281 | 288 | return replacer == null ? value : replacer.call(this, key, value); |
282 | 289 | }; |
283 | 290 | } |
284 | | - return stringify(this.data, replacer, space); |
| 291 | + return stringify(data, replacer, space); |
285 | 292 | } |
286 | 293 |
|
287 | 294 | /** |
@@ -317,21 +324,46 @@ class Store { |
317 | 324 |
|
318 | 325 | writeFile() { |
319 | 326 | mkdir(_dirname(this.path), this.options.mkdir); |
320 | | - const jsonData = this.json(); |
| 327 | + const jsonData = this.json(this.data); |
| 328 | + |
| 329 | + _writeFile(this.path, jsonData, {mode: 0o0600}, err => { |
| 330 | + if (err) { |
| 331 | + console.error(err); |
| 332 | + } |
| 333 | + }); |
| 334 | + } |
| 335 | + |
| 336 | + writeRestore(data) { |
| 337 | + mkdir(_dirname(this.path), this.options.mkdir); |
| 338 | + const jsonData = this.json(data); |
| 339 | + |
| 340 | + // Get all of the restore files and keep the oldest 10 |
| 341 | + const filePath = _dirname(this.path); |
| 342 | + readdirSync(filePath) |
| 343 | + .filter(f => f.includes("-restore-")) |
| 344 | + .sort((a, b) => { |
| 345 | + const timestampA = Number( |
| 346 | + a.slice(a.indexOf("-restore-") + 9), |
| 347 | + a.indexOf(".json"), |
| 348 | + ); |
| 349 | + const timestampB = Number( |
| 350 | + b.slice(b.indexOf("-restore-") + 9), |
| 351 | + b.indexOf(".json"), |
| 352 | + ); |
| 353 | + |
| 354 | + return timestampB - timestampA; |
| 355 | + }) |
| 356 | + .slice(10) |
| 357 | + .forEach(filename => unlinkSync(join(filePath, filename))); |
| 358 | + |
321 | 359 | _writeFile( |
322 | | - this.path.replace(".json", "-restore.json"), |
| 360 | + this.path.replace(".json", `-restore-${Date.now()}.json`), |
323 | 361 | jsonData, |
324 | 362 | {mode: 0o0600}, |
325 | 363 | err => { |
326 | 364 | if (err) { |
327 | 365 | console.error(err); |
328 | 366 | } |
329 | | - |
330 | | - _writeFile(this.path, jsonData, {mode: 0o0600}, err => { |
331 | | - if (err) { |
332 | | - console.error(err); |
333 | | - } |
334 | | - }); |
335 | 367 | }, |
336 | 368 | ); |
337 | 369 | } |
|
0 commit comments