Skip to content

Commit ddd734e

Browse files
committed
fix: fix duplicate process in reset and resetDataFileSync file name
1 parent 8a2c172 commit ddd734e

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"@types/lodash.isempty": "^4.4.3",
4545
"@types/lodash.isequal": "^4.5.2",
4646
"@types/lodash.set": "^4.3.3",
47+
"@types/lodash.uniq": "^4.5.3",
4748
"@types/verror": "^1.10.3",
4849
"moe-scripts": "0.0.61"
4950
},
@@ -59,6 +60,7 @@
5960
"lodash.isempty": "^4.4.0",
6061
"lodash.isequal": "^4.5.0",
6162
"lodash.set": "^4.3.2",
63+
"lodash.uniq": "^4.5.0",
6264
"resettable": "^0.1.18",
6365
"sort-keys": "^2.0.0",
6466
"verror": "^1.10.0",

src/resettable-file.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as childProcess from "child_process";
22
import path from "path";
33
import fs from "fs-extra";
44
import { promisify } from "util";
5+
import has from "lodash.has";
56

6-
import ResettableFile from "./resettable-file";
7+
import ResettableFile from "./index";
78
import { stubLogger } from "./logger";
89
import { BasicLogger } from "./@types";
910

@@ -60,7 +61,6 @@ beforeAll(async () => {
6061
afterAll(async () => {
6162
try {
6263
await Promise.all([clear(), clear(2)]);
63-
// await exec(`rm -rf ${path.join(paths.project, "/")}auto-test-qx-*`);
6464
} catch (e) {
6565
console.error(e);
6666
}
@@ -255,6 +255,15 @@ describe("ResettableFile", () => {
255255
expect(dataObject.has("x")).toBe(false);
256256
});
257257

258+
// it("should reset data file", () => {
259+
// const dataObject = resettableFile.getDataObjectSync("data.json");
260+
// dataObject.set("x", 1);
261+
// resettableFile.saveSync();
262+
// resettableFile.resetFileSync("data.json");
263+
// const data = fs.readJsonSync(path.join(paths.project, "data.json"));
264+
// expect(has(data, "x")).toBe(false);
265+
// });
266+
258267
it("should throw if non-tracked file tried to be reset", () => {
259268
expect(() => resettableFile.resetFileSync("text.txt")).toThrow("Cannot reset file");
260269
});
@@ -708,7 +717,6 @@ describe("ResettableFile", () => {
708717
rf.saveSync();
709718
fs.writeFileSync(rf.fromRoot("alt-x"), "other");
710719
rf.resetSync();
711-
712720
expect([rf.hasFileSync("registry.json"), rf.hasFileSync("alt-x.json"), rf.hasFileSync("alt-x")]).toEqual([true, false, true]);
713721
});
714722

src/resettable-file.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import VError from "verror";
66
import arrify from "arrify";
77
import crypto from "crypto";
88
import isEqual from "lodash.isEqual";
9+
import uniq from "lodash.uniq";
910
import emptyDir from "empty-dir";
1011
import { clone, Operation } from "resettable";
1112
import { InternalDataInterface } from "internal-data";
@@ -234,10 +235,9 @@ export default class ResettableFile {
234235
*/
235236
resetSync() {
236237
const internal = internalData.get(this);
237-
238238
try {
239239
this.logger.info("Reset begins.");
240-
const allFiles = Object.keys(internal.registry.files).concat(Object.keys(internal.dataFiles));
240+
const allFiles = uniq(Object.keys(internal.registry.files).concat(Object.keys(internal.dataFiles)));
241241
allFiles.forEach(projectFile => this.resetFileSync(projectFile));
242242

243243
// Delete created directories if they are empty.
@@ -434,14 +434,15 @@ export default class ResettableFile {
434434
const unapplied = dataObject.reset();
435435
const emptyData = isEqual(dataObject.data, {}) || !internal.registry.files[projectFile];
436436
const createdIndex = internal.registry.createdDataFiles.indexOf(projectFile);
437+
437438
internal.registry.createdDataFiles.splice(createdIndex, 1);
438439

439440
if (emptyData && createdIndex > -1) {
440441
fs.removeSync(file);
441442
this.logger.info(`Deleted data file after reset (empty object created by ResettableFile): ${file}`);
442443
} else {
443444
const { format, sortKeys } = dataObject;
444-
this.writeFileSync(file, dataObject.data, { format, sortKeys, serialize: true, track: false, force: true }); // Track is false, because tracked data files are tracked key level and written partially.
445+
this.writeFileSync(projectFile, dataObject.data, { format, sortKeys, serialize: true, track: false, force: true }); // Track is false, because tracked data files are tracked key level and written partially.
445446
}
446447

447448
/* istanbul ignore next */
@@ -610,7 +611,6 @@ export default class ResettableFile {
610611
try {
611612
if (isSafe) {
612613
const content = serialize ? serializeData(data as object, serializeFormat, sortKeys).data : data;
613-
614614
fs.outputFileSync(filePath, content);
615615

616616
if (track) {

0 commit comments

Comments
 (0)