Skip to content

Commit

Permalink
WIP - Kind of working
Browse files Browse the repository at this point in the history
  • Loading branch information
pistachiobaby committed Jan 16, 2024
1 parent 162200b commit 9de75e0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
27 changes: 27 additions & 0 deletions spec/services/filesync/filesync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ describe("FileSync.init", () => {
});
});

it("loads state from .gadget/sync.json and the environment is set correctly", async () => {
const state = {
app: multiEnvironmentTestApp.slug,
filesVersion: "77",
currentEnvironment: "development",
environments: {
development: { filesVersion: "77" },
},
};
await fs.outputJSON(appDirPath(".gadget/sync.json"), state);

const filesync = await FileSync.init(
makeContext({
parse: args,
argv: ["sync", appDir, "--app", multiEnvironmentTestApp.slug, "--environment", "cool-environment-development"],
}),
);

const { environments, ...rest } = state;
// @ts-expect-error _syncJson is private
expect(filesync._syncJson).toEqual({
...rest,
currentEnvironment: "cool-environment-development",
environments: { ...environments, "cool-environment-development": { fileVersion: "0" } },
});
});

it("uses default state if .gadget/sync.json does not exist and `dir` is empty", async () => {
const filesync = await FileSync.init(makeContext({ parse: args, argv: ["sync", appDir, "--app", testApp.slug] }));

Expand Down
22 changes: 15 additions & 7 deletions src/services/filesync/filesync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export class FileSync {
environments: Record<string, { filesVersion: string }>;
},
) {
if (this._syncJson.environments[this.environment] === undefined) {
this._syncJson.environments[this.environment] = { filesVersion: "0" };
}

this.ctx = ctx.child({
fields: () => ({ filesync: { directory: this.directory.path, filesVersion: this.filesVersion } }),
});
Expand Down Expand Up @@ -179,8 +183,8 @@ export class FileSync {
.catch((e) => ctx.log.warn("failed to load .gadget/sync.json", { error: e }));

// Basically a hack for reverse compatibility with old sync.json files
const devEnvrionmentState = state?.environments["development"];
if (devEnvrionmentState && state.filesVersion && devEnvrionmentState.filesVersion !== state.filesVersion) {
const devEnvironmentState = state?.environments["development"];
if (devEnvironmentState && state.filesVersion && devEnvironmentState.filesVersion !== state.filesVersion) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
state.environments["development"]!.filesVersion = state.filesVersion;
}
Expand Down Expand Up @@ -819,11 +823,15 @@ export class FileSync {
* Updates {@linkcode _syncJson} and saves it to `.gadget/sync.json`.
*/
private async _save(filesVersion: string | bigint): Promise<void> {
this._syncJson = {
...this._syncJson,
filesVersion: String(filesVersion),
environments: { ...this._syncJson.environments, [this.environment]: { filesVersion: String(filesVersion) } },
};
assert(this._syncJson.environments[this.environment], "environment doesn't exist in _syncJson");

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._syncJson.environments[this.environment]!.filesVersion = String(filesVersion);

if (this.environment === "development") {
this._syncJson.filesVersion = String(filesVersion);
}

this.ctx.log.debug("saving .gadget/sync.json");
await fs.outputJSON(this.directory.absolute(".gadget/sync.json"), this._syncJson, { spaces: 2 });
}
Expand Down

0 comments on commit 9de75e0

Please sign in to comment.