-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use basepath name for project name and add on-create hook (#4)
* fix: add target directory name question * feat: add on create hook for dynamic section * refactor: move to hooks folder * feat: add cloudflare-workers' on-create hook * feat: add log when path is unspecified * feat: use basepath for project name * refactor(on-create): rename REPLACE_PROJECT_NAME_KEY to PROJECT_NAME_REPLACE_KEY * refactor: move Hook type to types.ts * refactor: rename on-create to after-create * refactor: rename on-create.ts to after-create.ts * refactor: make more readable for variable * fix: rename replacing key to `%%PROJECT_NAME%%` * refactor: rename forgotten after-create-hook * chore: add vitest * fix: prepare for testing about after-create.ts * test: add after-create test case * make it as a class * fix: add return type to applied hook results * fix: expand and pass arguments * test: add applyHook test for Hook class * test: fix test for new hook style * fix: use replaceAll for multiple target * test: update test case for multiple replacing --------- Co-authored-by: Yusuke Wada <[email protected]>
- Loading branch information
Showing
7 changed files
with
642 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { Hook } from './hook' | ||
|
||
describe('Hook', () => { | ||
it('`Hook.applyHook()` runs all hooks for a template', () => { | ||
const hook = new Hook() | ||
const fn1 = vi.fn() | ||
const fn2 = vi.fn() | ||
hook.addHook('test-template', fn1) | ||
hook.addHook('test-template', fn2) | ||
hook.applyHook('test-template', {}) | ||
expect(fn1).toHaveBeenCalled() | ||
expect(fn2).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export class Hook<HookFunction extends (...args: any[]) => any> { | ||
#hookMap: Map<string, HookFunction[]> | ||
constructor() { | ||
this.#hookMap = new Map<string, HookFunction[]>() | ||
} | ||
|
||
addHook(templateName: string, hook: HookFunction) { | ||
const hooks = this.#hookMap.get(templateName) || [] | ||
hooks.push(hook) | ||
this.#hookMap.set(templateName, hooks) | ||
} | ||
|
||
applyHook( | ||
templateName: string, | ||
...hookOptions: Parameters<HookFunction> | ||
): ReturnType<HookFunction>[] { | ||
const hooks = this.#hookMap.get(templateName) | ||
const results: ReturnType<HookFunction>[] = [] | ||
if (hooks) { | ||
hooks.forEach((hook) => { | ||
results.push(hook(...hookOptions)) | ||
}) | ||
} | ||
return results | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { join } from 'path' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { afterCreateHook } from './after-create' | ||
|
||
describe('afterCreateHook', () => { | ||
describe('cloudflare-workers', () => { | ||
describe('rewriteWranglerHook', () => { | ||
it('rewrites the wrangler.toml file with the project name', async () => { | ||
vi.mock('fs', () => { | ||
const wrangler = ` | ||
name = "%%PROJECT_NAME%%" | ||
[env.staging] | ||
name = "%%PROJECT_NAME%%-staging" | ||
`.trim() | ||
|
||
return { | ||
readFileSync: vi.fn().mockReturnValue(wrangler), | ||
writeFileSync: vi.fn(), | ||
} | ||
}) | ||
const { readFileSync, writeFileSync } = await import('fs') | ||
|
||
const projectName = 'test-project' | ||
const directoryPath = './tmp' | ||
const wranglerPath = join(directoryPath, 'wrangler.toml') | ||
const replaced = ` | ||
name = "${projectName}" | ||
[env.staging] | ||
name = "${projectName}-staging" | ||
`.trim() | ||
afterCreateHook.applyHook('cloudflare-workers', { | ||
projectName, | ||
directoryPath, | ||
}) | ||
expect(readFileSync).toHaveBeenCalledWith(wranglerPath, 'utf-8') | ||
expect(writeFileSync).toHaveBeenCalledWith(wranglerPath, replaced) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { readFileSync, writeFileSync } from 'fs' | ||
import * as path from 'path' | ||
import { Hook } from '../hook' | ||
|
||
type AfterHookOptions = { | ||
projectName: string | ||
directoryPath: string | ||
} | ||
|
||
type AfterHookFunction = (options: AfterHookOptions) => void | ||
|
||
const afterCreateHook = new Hook<AfterHookFunction>() | ||
|
||
afterCreateHook.addHook( | ||
'cloudflare-workers', | ||
({ projectName, directoryPath }) => { | ||
const wranglerPath = path.join(directoryPath, 'wrangler.toml') | ||
const wrangler = readFileSync(wranglerPath, 'utf-8') | ||
const rewritten = wrangler.replaceAll('%%PROJECT_NAME%%', projectName) | ||
writeFileSync(wranglerPath, rewritten) | ||
} | ||
) | ||
|
||
export { afterCreateHook } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.