From 0e1edb281eb3e77e73c8dbbd79ee283e0e3f8cd1 Mon Sep 17 00:00:00 2001 From: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com> Date: Fri, 12 Jul 2024 07:47:54 +0530 Subject: [PATCH 1/4] feat(workers): set compatibility date to current date --- src/hooks/after-create.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/hooks/after-create.ts b/src/hooks/after-create.ts index 5efc32a..67d4c60 100644 --- a/src/hooks/after-create.ts +++ b/src/hooks/after-create.ts @@ -17,4 +17,19 @@ afterCreateHook.addHook( }, ) +const regex = /compatibility_date\s*=\s*"\d{4}-\d{2}-\d{2}"/ +afterCreateHook.addHook(['cloudflare-workers'], ({ directoryPath }) => { + const wranglerPath = path.join(directoryPath, 'wrangler.toml') + const wrangler = readFileSync(wranglerPath, 'utf-8') + + const currentDate = new Date().toISOString().split('T')[0] // Get current date in YYYY-MM-DD format + + const rewritten = wrangler.replace( + regex, + `compatibility_date = "${currentDate}"`, + ) + + writeFileSync(wranglerPath, rewritten) +}) + export { afterCreateHook } From 9b4e0d397dce2161a59034532b9b293b279ccbb1 Mon Sep 17 00:00:00 2001 From: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com> Date: Fri, 12 Jul 2024 08:01:31 +0530 Subject: [PATCH 2/4] chore: renamed regex variable --- src/hooks/after-create.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/hooks/after-create.ts b/src/hooks/after-create.ts index 67d4c60..28d20f7 100644 --- a/src/hooks/after-create.ts +++ b/src/hooks/after-create.ts @@ -17,18 +17,16 @@ afterCreateHook.addHook( }, ) -const regex = /compatibility_date\s*=\s*"\d{4}-\d{2}-\d{2}"/ +const compatibilityDateRegex = /compatibility_date\s*=\s*"\d{4}-\d{2}-\d{2}"/ afterCreateHook.addHook(['cloudflare-workers'], ({ directoryPath }) => { const wranglerPath = path.join(directoryPath, 'wrangler.toml') const wrangler = readFileSync(wranglerPath, 'utf-8') - - const currentDate = new Date().toISOString().split('T')[0] // Get current date in YYYY-MM-DD format - + // Get current date in YYYY-MM-DD format + const currentDate = new Date().toISOString().split('T')[0] const rewritten = wrangler.replace( - regex, + compatibilityDateRegex, `compatibility_date = "${currentDate}"`, ) - writeFileSync(wranglerPath, rewritten) }) From 4b240ffc95658cb7d44955145b963f3ada5172ae Mon Sep 17 00:00:00 2001 From: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com> Date: Fri, 12 Jul 2024 08:47:03 +0530 Subject: [PATCH 3/4] tests(hooks): added check for compatibility_date updation --- src/hooks/after-create.test.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/hooks/after-create.test.ts b/src/hooks/after-create.test.ts index 8747716..7037f45 100644 --- a/src/hooks/after-create.test.ts +++ b/src/hooks/after-create.test.ts @@ -9,6 +9,7 @@ describe('afterCreateHook', () => { vi.mock('fs', () => { const wrangler = ` name = "%%PROJECT_NAME%%" +compatibility_date = "2023-12-01" [env.staging] name = "%%PROJECT_NAME%%-staging" @@ -24,18 +25,32 @@ name = "%%PROJECT_NAME%%-staging" const projectName = 'test-projectNAME+123' const directoryPath = './tmp' const wranglerPath = join(directoryPath, 'wrangler.toml') - const replaced = ` + + const firstHookContent = ` name = "test-projectname-123" +compatibility_date = "2023-12-01" [env.staging] name = "test-projectname-123-staging" `.trim() + + // Get current date in YYYY-MM-DD format + const currentDate = new Date().toISOString().split('T')[0] + const secondHookContent = ` +name = "%%PROJECT_NAME%%" +compatibility_date = "${currentDate}" + +[env.staging] +name = "%%PROJECT_NAME%%-staging" + `.trim() + afterCreateHook.applyHook('cloudflare-workers', { projectName, directoryPath, }) expect(readFileSync).toHaveBeenCalledWith(wranglerPath, 'utf-8') - expect(writeFileSync).toHaveBeenCalledWith(wranglerPath, replaced) + expect(writeFileSync).nthCalledWith(1, wranglerPath, firstHookContent) + expect(writeFileSync).nthCalledWith(2, wranglerPath, secondHookContent) }) }) }) From 9f95fbb7e0f34bc56713f8235be04bc78b230e27 Mon Sep 17 00:00:00 2001 From: Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com> Date: Fri, 12 Jul 2024 08:47:14 +0530 Subject: [PATCH 4/4] chore: updated variable name --- src/hooks/after-create.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/after-create.ts b/src/hooks/after-create.ts index 28d20f7..a18bb42 100644 --- a/src/hooks/after-create.ts +++ b/src/hooks/after-create.ts @@ -17,14 +17,14 @@ afterCreateHook.addHook( }, ) -const compatibilityDateRegex = /compatibility_date\s*=\s*"\d{4}-\d{2}-\d{2}"/ +const COMPATIBILITY_DATE = /compatibility_date\s*=\s*"\d{4}-\d{2}-\d{2}"/ afterCreateHook.addHook(['cloudflare-workers'], ({ directoryPath }) => { const wranglerPath = path.join(directoryPath, 'wrangler.toml') const wrangler = readFileSync(wranglerPath, 'utf-8') // Get current date in YYYY-MM-DD format const currentDate = new Date().toISOString().split('T')[0] const rewritten = wrangler.replace( - compatibilityDateRegex, + COMPATIBILITY_DATE, `compatibility_date = "${currentDate}"`, ) writeFileSync(wranglerPath, rewritten)