Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hooks/after-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ name = "%%PROJECT_NAME%%-staging"
})
expect(readFileSync).toHaveBeenCalledWith(wranglerPath, 'utf-8')
expect(writeFileSync).nthCalledWith(1, wranglerPath, firstHookContent)
expect(writeFileSync).nthCalledWith(2, wranglerPath, secondHookContent)
expect(writeFileSync).nthCalledWith(4, wranglerPath, secondHookContent)
})
})
})
Expand Down
42 changes: 26 additions & 16 deletions src/hooks/after-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ import { afterCreateHook } from '../hook'

const PROJECT_NAME = new RegExp(/%%PROJECT_NAME.*%%/g)

const WRANGLER_FILES = ['wrangler.toml', 'wrangler.json', 'wrangler.jsonc']

afterCreateHook.addHook(
['cloudflare-workers', 'cloudflare-pages', 'x-basic'],
({ projectName, directoryPath }) => {
const wranglerPath = path.join(directoryPath, 'wrangler.toml')
const wrangler = readFileSync(wranglerPath, 'utf-8')
const convertProjectName = projectName
.toLowerCase()
.replaceAll(/[^a-z0-9\-_]/gm, '-')
const rewritten = wrangler.replaceAll(PROJECT_NAME, convertProjectName)
writeFileSync(wranglerPath, rewritten)
for (const filename of WRANGLER_FILES) {
try {
const wranglerPath = path.join(directoryPath, filename)
const wrangler = readFileSync(wranglerPath, 'utf-8')
const convertProjectName = projectName
.toLowerCase()
.replaceAll(/[^a-z0-9\-_]/gm, '-')
const rewritten = wrangler.replaceAll(PROJECT_NAME, convertProjectName)
writeFileSync(wranglerPath, rewritten)
} catch {}
}
},
)

Expand All @@ -33,15 +39,19 @@ const COMPATIBILITY_DATE = /compatibility_date\s*=\s*"\d{4}-\d{2}-\d{2}"/
afterCreateHook.addHook(
['cloudflare-workers', 'cloudflare-pages'],
({ 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)
for (const filename of WRANGLER_FILES) {
try {
const wranglerPath = path.join(directoryPath, filename)
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)
} catch {}
}
},
)

Expand Down