diff --git a/spec/__support__/filesync.ts b/spec/__support__/filesync.ts index 358b9ac68..1ba12badc 100644 --- a/spec/__support__/filesync.ts +++ b/spec/__support__/filesync.ts @@ -206,7 +206,12 @@ export const makeSyncScenario = async ({ await localDir.loadIgnoreFile(); if (!localFiles[".gadget/sync.json"]) { - const syncJson: SyncJson = { app: testApp.slug, environment: "development", filesVersion: "1", mtime: Date.now() + 1 }; + const syncJson: SyncJson = { + app: testApp.slug, + filesVersion: "1", + currentEnvironment: "development", + environments: { development: { filesVersion: "1" } }, + }; await fs.outputJSON(localDir.absolute(".gadget/sync.json"), syncJson, { spaces: 2 }); } } diff --git a/spec/commands/sync.spec.ts b/spec/commands/sync.spec.ts index 82c1b33f5..a3235934e 100644 --- a/spec/commands/sync.spec.ts +++ b/spec/commands/sync.spec.ts @@ -77,7 +77,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", "file.txt": "file v2", }, } @@ -113,7 +113,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "file.txt": "file v3", }, } @@ -153,7 +153,7 @@ describe("sync", () => { ".gadget/": "", ".gadget/backup/": "", ".gadget/backup/file.txt": "file v3", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"4\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"4\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"4\\"}}}", }, } `); @@ -197,7 +197,7 @@ describe("sync", () => { ".gadget/": "", ".gadget/backup/": "", ".gadget/backup/file.txt": "file v3", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"5\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"5\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"5\\"}}}", "directory/": "", }, } @@ -245,7 +245,7 @@ describe("sync", () => { ".gadget/backup/": "", ".gadget/backup/directory/": "", ".gadget/backup/file.txt": "file v3", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"6\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"6\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"6\\"}}}", }, } `); @@ -316,7 +316,7 @@ describe("sync", () => { ".gadget/backup/": "", ".gadget/backup/directory/": "", ".gadget/backup/file.txt": "file v3", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"7\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"7\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"7\\"}}}", "file1.txt": "file1.txt", "file10.txt": "file10.txt", "file2.txt": "file2.txt", @@ -440,7 +440,7 @@ describe("sync", () => { ".gadget/backup/": "", ".gadget/backup/directory/": "", ".gadget/backup/file.txt": "file v3", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"7\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"7\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"7\\"}}}", "file1.txt": "file1.txt", "file10.txt": "file10.txt", "file2.txt": "file2.txt", @@ -576,7 +576,7 @@ describe("sync", () => { ".gadget/backup/": "", ".gadget/backup/directory/": "", ".gadget/backup/file.txt": "file v3", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"7\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"7\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"7\\"}}}", "file.js": "file v2", "file1.txt": "file1.txt", "file10.txt": "file10.txt", @@ -639,7 +639,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", ".ignore": "**/tmp", }, } @@ -680,7 +680,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", ".ignore": "**/tmp", }, } @@ -712,7 +712,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", "file.txt": "file v2", }, } @@ -742,7 +742,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "file.txt": "file v3", }, } @@ -776,7 +776,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"4\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"4\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"4\\"}}}", "renamed-file.txt": "file v3", }, } @@ -812,7 +812,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"5\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"5\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"5\\"}}}", }, } `); @@ -852,7 +852,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"6\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"6\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"6\\"}}}", "directory/": "", }, } @@ -897,7 +897,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"7\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"7\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"7\\"}}}", "renamed-directory/": "", }, } @@ -944,7 +944,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"8\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"8\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"8\\"}}}", }, } `); @@ -1020,7 +1020,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"9\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"9\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"9\\"}}}", "file1.txt": "file1.txt", "file10.txt": "file10.txt", "file2.txt": "file2.txt", @@ -1065,7 +1065,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", "file.txt": "v10", }, } @@ -1131,7 +1131,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"1\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"1\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"1\\"}}}", ".ignore": "**/tmp", "tmp/": "", "tmp/file1.txt": "file1.txt", @@ -1176,7 +1176,7 @@ describe("sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", ".ignore": "# watch it all", }, } diff --git a/spec/services/filesync/filesync.spec.ts b/spec/services/filesync/filesync.spec.ts index 279fc5f5b..917b26241 100644 --- a/spec/services/filesync/filesync.spec.ts +++ b/spec/services/filesync/filesync.spec.ts @@ -48,7 +48,14 @@ describe("FileSync.init", () => { }); it("loads state from .gadget/sync.json", async () => { - const state = { app: testApp.slug, environment: "development", filesVersion: "77", mtime: 1658153625236 }; + const state = { + app: testApp.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", testApp.slug] })); @@ -58,20 +65,45 @@ describe("FileSync.init", () => { }); it("loads state from .gadget/sync.json and sets environment to `development` if it's missing", async () => { - const state = { app: testApp.slug, filesVersion: "77", mtime: 1658153625236 }; + const state = { app: testApp.slug, filesVersion: "77" }; await fs.outputJSON(appDirPath(".gadget/sync.json"), state); const filesync = await FileSync.init(makeContext({ parse: args, argv: ["sync", appDir, "--app", testApp.slug] })); // @ts-expect-error _syncJson is private - expect(filesync._syncJson).toEqual({ ...state, environment: "development" }); + expect(filesync._syncJson).toEqual({ + ...state, + currentEnvironment: "development", + environments: { development: { filesVersion: "77" } }, + }); + }); + + it("loads state from .gadget/sync.json and ignores mtime if it exists", async () => { + const state = { app: testApp.slug, filesVersion: "77" }; + await fs.outputJSON(appDirPath(".gadget/sync.json"), { ...state, mtime: "8888584595" }); + + const filesync = await FileSync.init(makeContext({ parse: args, argv: ["sync", appDir, "--app", testApp.slug] })); + + // @ts-expect-error _syncJson is private + expect(filesync._syncJson).toEqual({ + ...state, + currentEnvironment: "development", + environments: { development: { filesVersion: "77" } }, + }); }); 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] })); // @ts-expect-error _syncJson is private - expect(filesync._syncJson).toEqual({ app: "test", environment: "development", filesVersion: "0", mtime: 0 }); + expect(filesync._syncJson).toEqual({ + app: "test", + filesVersion: "0", + currentEnvironment: "development", + environments: { + development: { filesVersion: "0" }, + }, + }); }); it("throws InvalidSyncFileError if .gadget/sync.json does not exist and `dir` is not empty", async () => { @@ -93,12 +125,22 @@ describe("FileSync.init", () => { it("does not throw InvalidSyncFileError if .gadget/sync.json is invalid and `--force` is passed", async () => { // has trailing comma - await fs.outputFile(appDirPath(".gadget/sync.json"), '{"app":"test","filesVersion":"77","mtime":1658153625236,}'); + await fs.outputFile( + appDirPath(".gadget/sync.json"), + '{"app":"test","filesVersion":"77", "currentEnvironment": "development", "environments": {"development": {"filesVersion": "77"}},}', + ); const filesync = await FileSync.init(makeContext({ parse: args, argv: ["sync", appDir, "--app", testApp.slug, "--force"] })); // @ts-expect-error _syncJson is private - expect(filesync._syncJson).toEqual({ app: testApp.slug, environment: "development", filesVersion: "0", mtime: 0 }); + expect(filesync._syncJson).toEqual({ + app: testApp.slug, + currentEnvironment: "development", + filesVersion: "0", + environments: { + development: { filesVersion: "0" }, + }, + }); }); it("throws ArgError if the `--app` arg is passed a slug that does not exist within the user's available apps", async () => { @@ -143,9 +185,11 @@ describe("FileSync.init", () => { it("does not throw ArgError if the `--environment` flag is passed a different environment than the one in .gadget/sync.json", async () => { await fs.outputJson(appDirPath(".gadget/sync.json"), { app: multiEnvironmentTestApp.slug, - environment: "development", + currentEnvironment: "development", filesVersion: "0", - mtime: 0, + environments: { + development: { filesVersion: "0" }, + }, }); const filesync = await FileSync.init( @@ -158,27 +202,45 @@ describe("FileSync.init", () => { // @ts-expect-error _syncJson is private expect(filesync._syncJson).toEqual({ app: multiEnvironmentTestApp.slug, - environment: "other-environment-development", + currentEnvironment: "other-environment-development", filesVersion: "0", - mtime: 0, + environments: { + development: { filesVersion: "0" }, + }, }); }); it("does not throw ArgError if the `--app` flag is passed a different app name than the one in .gadget/sync.json and `--force` is passed", async () => { - await fs.outputJson(appDirPath(".gadget/sync.json"), { app: "not-test", filesVersion: "77", mtime: 1658153625236 }); + await fs.outputJson(appDirPath(".gadget/sync.json"), { + app: "not-test", + currentEnvironment: "development", + filesVersion: "77", + environments: { + development: { filesVersion: "77" }, + }, + }); const filesync = await FileSync.init(makeContext({ parse: args, argv: ["sync", appDir, "--app", testApp.slug, "--force"] })); // @ts-expect-error _syncJson is private - expect(filesync._syncJson).toEqual({ app: testApp.slug, environment: "development", filesVersion: "0", mtime: 0 }); + expect(filesync._syncJson).toEqual({ + app: testApp.slug, + filesVersion: "0", + currentEnvironment: "development", + environments: { development: { filesVersion: "0" } }, + }); }); it("does not throw ArgError if the `--environment` flag is passed a different environment than the one in .gadget/sync.json and `--force` is passed", async () => { await fs.outputJson(appDirPath(".gadget/sync.json"), { - app: "test", - environment: "development", - filesVersion: "0", - mtime: 0, + app: multiEnvironmentTestApp.slug, + filesVersion: "77", + currentEnvironment: "development", + environments: { + development: { + filesVersion: "77", + }, + }, }); const filesync = await FileSync.init( @@ -191,9 +253,13 @@ describe("FileSync.init", () => { // @ts-expect-error _syncJson is private expect(filesync._syncJson).toEqual({ app: multiEnvironmentTestApp.slug, - environment: "cool-environment-development", - filesVersion: "0", - mtime: 0, + currentEnvironment: "cool-environment-development", + filesVersion: "77", + environments: { + development: { + filesVersion: "77", + }, + }, }); }); }); @@ -676,7 +742,7 @@ describe("FileSync.sendChangesToGadget", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "gadget.txt": "// gadget", "local.txt": "// local", }, @@ -742,7 +808,7 @@ describe("FileSync.sendChangesToGadget", () => { "localDir": { ".gadget/": "", ".gadget/client.js": "// client", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"4\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"4\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"4\\"}}}", "gadget.txt": "// gadget", "local.txt": "// local", }, @@ -784,7 +850,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"1\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"1\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"1\\"}}}", "foo.js": "// foo", }, } @@ -827,7 +893,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"1\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"1\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"1\\"}}}", ".ignore": "foo.js", "foo.js": "// foo (local)", }, @@ -881,7 +947,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo", "gadget-file.js": "// gadget", "local-file.js": "// local", @@ -921,7 +987,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"1\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"1\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"1\\"}}}", "foo.js": "foo (local)", }, } @@ -967,7 +1033,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo (local)", }, } @@ -1014,7 +1080,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo (local)", }, } @@ -1069,7 +1135,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo (local)", "gadget-file.js": "// gadget", "local-file.js": "// local", @@ -1125,7 +1191,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo (local)", "gadget-file.js": "// gadget", "local-file.js": "// local", @@ -1171,7 +1237,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", "foo.js": "// foo (gadget)", }, } @@ -1214,7 +1280,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", "foo.js": "// foo (gadget)", }, } @@ -1269,7 +1335,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo (gadget)", "gadget-file.js": "// gadget", "local-file.js": "// local", @@ -1326,7 +1392,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo (gadget)", "gadget-file.js": "// gadget", "local-file.js": "// local", @@ -1382,7 +1448,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo (gadget)", "gadget-file.js": "// gadget", "local-file.js": "// local", @@ -1427,7 +1493,7 @@ describe("FileSync.sync", () => { "localDir": { ".gadget/": "", ".gadget/client.js": "// client (gadget)", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", }, } `); @@ -1482,7 +1548,7 @@ describe("FileSync.sync", () => { "localDir": { ".gadget/": "", ".gadget/client.js": "// client (gadget)", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "foo.js": "// foo (local)", }, } @@ -1519,7 +1585,7 @@ describe("FileSync.sync", () => { "localDir": { ".gadget/": "", ".gadget/client.js": "// client", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"1\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"1\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"1\\"}}}", }, } `); @@ -1576,7 +1642,7 @@ describe("FileSync.sync", () => { ".gadget/": "", ".gadget/client.js": "// client", ".gadget/server.js": "// server", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"2\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"2\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"2\\"}}}", "gadget-file.js": "// gadget", "local-file.js": "// local", }, @@ -1630,7 +1696,7 @@ describe("FileSync.sync", () => { }, "localDir": { ".gadget/": "", - ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"environment\\":\\"development\\",\\"filesVersion\\":\\"3\\"}", + ".gadget/sync.json": "{\\"app\\":\\"test\\",\\"filesVersion\\":\\"3\\",\\"currentEnvironment\\":\\"development\\",\\"environments\\":{\\"development\\":{\\"filesVersion\\":\\"3\\"}}}", "gadget.txt": "// gadget", "local.txt": "// local", }, diff --git a/src/services/filesync/filesync.ts b/src/services/filesync/filesync.ts index d0d8ce687..33c36958f 100644 --- a/src/services/filesync/filesync.ts +++ b/src/services/filesync/filesync.ts @@ -84,7 +84,12 @@ export class FileSync { * * This is persisted to `.gadget/sync.json` within the {@linkcode directory}. */ - private _syncJson: { app: string; environment: string; environments: Record }, + private _syncJson: { + app: string; + filesVersion: string; + currentEnvironment: string; + environments: Record; + }, ) { this.ctx = ctx.child({ fields: () => ({ filesync: { directory: this.directory.path, filesVersion: this.filesVersion } }), @@ -156,16 +161,18 @@ export class FileSync { return z .object({ app: z.string(), - environment: z + filesVersion: z .string() .nullish() - .transform((x) => x?.toLowerCase() ?? "development"), // old sync.json environment's may be null/undefined so default to development - filesVersion: z.string(), - mtime: z.number(), + .transform((x) => x ?? "0"), // Legacy support for old sync.json files + currentEnvironment: z + .string() + .nullish() + .transform((x) => x ?? "development"), // New support for multi environment, but old sync.json files don't have this environments: z .record(z.object({ filesVersion: z.string() })) .nullish() - .transform((value, ctx) => value ?? { development: { filesVersion: "0" } }), + .transform((value, _ctx) => value ?? { development: { filesVersion: "0" } }), }) .parse(json); }) @@ -173,7 +180,7 @@ export class FileSync { // Basically a hack for reverse compatibility with old sync.json files const devEnvrionmentState = state?.environments["development"]; - if (devEnvrionmentState && devEnvrionmentState.filesVersion !== state.filesVersion) { + if (devEnvrionmentState && state.filesVersion && devEnvrionmentState.filesVersion !== state.filesVersion) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion state.environments["development"]!.filesVersion = state.filesVersion; } @@ -212,7 +219,7 @@ export class FileSync { ); } - let environment = ctx.args["--environment"]?.toLowerCase() || state?.environment?.toLowerCase(); + let environment = ctx.args["--environment"]?.toLowerCase() || state?.currentEnvironment?.toLowerCase(); const filteredSelectableEnvironments = app.environments.filter((x) => x.type.toLowerCase() !== "production"); if (app.multiEnvironmentEnabled) { @@ -244,7 +251,7 @@ export class FileSync { `.concat(` • ${similarEnvironments.join("\n • ")}`), ); } - } else if (environment) { + } else if (ctx.args["--environment"]?.toLowerCase()) { // multi environment is not enabled but the user specified an environment throw new ArgError( sprint` @@ -269,7 +276,8 @@ export class FileSync { // either way, create a fresh .gadget/sync.json file return new FileSync(ctx, directory, app, environment, { app: app.slug, - environment, + filesVersion: "0", + currentEnvironment: environment, environments: { [environment]: { filesVersion: "0" }, }, @@ -283,14 +291,20 @@ export class FileSync { // the .gadget/sync.json file exists if (state.app === app.slug) { // the .gadget/sync.json file is for the same app - return new FileSync(ctx, directory, app, environment, { ...state, environment }); + return new FileSync(ctx, directory, app, environment, { ...state, currentEnvironment: environment }); } // the .gadget/sync.json file is for a different app or environment if (ctx.args["--force"]) { // the user passed --force, so use the app they specified and overwrite everything - const { environments } = state; - return new FileSync(ctx, directory, app, environment, { app: app.slug, environment, environments }); + return new FileSync(ctx, directory, app, environment, { + app: app.slug, + filesVersion: "0", + currentEnvironment: environment, + environments: { + [environment]: { filesVersion: "0" }, + }, + }); } // the user didn't pass --force, so throw an error @@ -301,7 +315,7 @@ export class FileSync { However, that directory has already been synced with this app: - {dim ${state.app}} {dim ({red ${state.environment}})} + {dim ${state.app}} {dim ({red ${state.currentEnvironment}})} If you're sure that you want to sync: @@ -785,7 +799,11 @@ export class FileSync { * Updates {@linkcode _syncJson} and saves it to `.gadget/sync.json`. */ private async _save(filesVersion: string | bigint): Promise { - this._syncJson = { ...this._syncJson, mtime: Date.now() + 1, filesVersion: String(filesVersion) }; + this._syncJson = { + ...this._syncJson, + filesVersion: String(filesVersion), + environments: { ...this._syncJson.environments, [this.environment]: { filesVersion: String(filesVersion) } }, + }; this.ctx.log.debug("saving .gadget/sync.json"); await fs.outputJSON(this.directory.absolute(".gadget/sync.json"), this._syncJson, { spaces: 2 }); }