Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use basepath name for project name and add on-create hook #4

Merged
merged 24 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
eb23145
fix: add target directory name question
sor4chi Oct 12, 2023
1b474e6
feat: add on create hook for dynamic section
sor4chi Oct 12, 2023
1cf4e08
refactor: move to hooks folder
sor4chi Oct 12, 2023
d9ae2dc
feat: add cloudflare-workers' on-create hook
sor4chi Oct 12, 2023
16d7523
feat: add log when path is unspecified
sor4chi Oct 12, 2023
bb69964
feat: use basepath for project name
sor4chi Oct 12, 2023
ac59543
refactor(on-create): rename REPLACE_PROJECT_NAME_KEY to PROJECT_NAME_…
sor4chi Oct 12, 2023
ceedd17
refactor: move Hook type to types.ts
sor4chi Oct 13, 2023
d79eb6c
refactor: rename on-create to after-create
sor4chi Oct 13, 2023
b18d5ad
refactor: rename on-create.ts to after-create.ts
sor4chi Oct 13, 2023
9ec2f27
refactor: make more readable for variable
sor4chi Oct 13, 2023
7f38546
fix: rename replacing key to `%%PROJECT_NAME%%`
sor4chi Oct 13, 2023
31f7f90
refactor: rename forgotten after-create-hook
sor4chi Oct 13, 2023
233fcc1
chore: add vitest
sor4chi Oct 13, 2023
8ca7ece
fix: prepare for testing about after-create.ts
sor4chi Oct 13, 2023
37dd246
test: add after-create test case
sor4chi Oct 13, 2023
285ff6c
make it as a class
yusukebe Oct 14, 2023
ac529b5
Merge pull request #1 from yusukebe/current-directory-name-class
sor4chi Oct 14, 2023
c0f9333
fix: add return type to applied hook results
sor4chi Oct 14, 2023
1e3022a
fix: expand and pass arguments
sor4chi Oct 14, 2023
861fdde
test: add applyHook test for Hook class
sor4chi Oct 14, 2023
9f36a5d
test: fix test for new hook style
sor4chi Oct 14, 2023
343fd53
fix: use replaceAll for multiple target
sor4chi Oct 14, 2023
af7fae8
test: update test case for multiple replacing
sor4chi Oct 14, 2023
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
24 changes: 24 additions & 0 deletions src/hooks/on-create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { readFileSync, writeFileSync } from 'fs'
import path from 'path'

type Hook = (args: { projectName: string; directoryPath: string }) => void

// <templateName, Hook[]>
export const ON_CREATE_HOOKS = new Map<string, Hook[]>()

export const addOnCreateHook = (templateName: string, hook: Hook) => {
const hooks = ON_CREATE_HOOKS.get(templateName) || []
hooks.push(hook)
ON_CREATE_HOOKS.set(templateName, hooks)
}

const PROJECT_NAME_REPLACE_KEY = '[DYNAMIC_PROJECT_NAME]'

const rewriteWranglerHook: Hook = ({ projectName, directoryPath }) => {
const wranglerPath = path.join(directoryPath, 'wrangler.toml')
const wrangler = readFileSync(wranglerPath, 'utf-8')
const rewritten = wrangler.replace(PROJECT_NAME_REPLACE_KEY, projectName)
writeFileSync(wranglerPath, rewritten)
}

addOnCreateHook('cloudflare-workers', rewriteWranglerHook)
35 changes: 34 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import prompts from 'prompts'
import yargsParser from 'yargs-parser'
import { version } from '../package.json'
import { viaContentsApi } from './github.js'
import { ON_CREATE_HOOKS } from './hooks/on-create'

const directoryName = 'templates'
const config = {
Expand All @@ -29,7 +30,6 @@ async function main() {

let args = yargsParser(process.argv.slice(2))

const target = (args._[0] && String(args._[0])) || '.'
const templateArg = args.template

const templateDirs = await viaContentsApi(config)
Expand All @@ -45,6 +45,27 @@ async function main() {
})
let templateNames = [...Object.values(templates)] as { name: string }[]

let target = ''
let projectName = ''
if (args._[0]) {
target = String(args._[0])
projectName = target
console.log(`${bold(`${green(`✔`)} Using target directory`)} … ${target}`)
} else {
const ans = await prompts({
type: 'text',
name: 'target',
message: 'Target directory',
initial: 'my-app',
})
target = ans.target
if (ans.target === '.') {
projectName = path.basename(process.cwd())
} else {
projectName = path.basename(ans.target)
}
}

const templateName =
templateArg ||
(
Expand Down Expand Up @@ -102,6 +123,18 @@ async function main() {
})
})

try {
const hooks = ON_CREATE_HOOKS.get(templateName) || []
hooks.forEach((hook) => {
hook({
projectName,
directoryPath: path.join(process.cwd(), target),
})
})
} catch (e) {
throw new Error(`Error running hook for ${templateName}: ${e.message}`)
}

console.log(bold(green('✔ Copied project files')))
}

Expand Down