Skip to content

Commit

Permalink
feat(workers): set compatibility date to current date (#59)
Browse files Browse the repository at this point in the history
* feat(workers): set compatibility date to current date

* chore: renamed regex variable

* tests(hooks): added check for compatibility_date updation

* chore: updated variable name
  • Loading branch information
MathurAditya724 authored Jul 12, 2024
1 parent 90e18f2 commit efb096a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/hooks/after-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
})
})
})
Expand Down
13 changes: 13 additions & 0 deletions src/hooks/after-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ afterCreateHook.addHook(
},
)

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(
COMPATIBILITY_DATE,
`compatibility_date = "${currentDate}"`,
)
writeFileSync(wranglerPath, rewritten)
})

export { afterCreateHook }

0 comments on commit efb096a

Please sign in to comment.