Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/validate-positional-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-solana-dapp': minor
---

Validate the positional `name` CLI argument with the same rules as the interactive prompt. Previously `npx create-solana-dapp My_App` bypassed validation entirely and produced an invalid npm package name and a degenerate rename search key; it now fails with the validation message.
14 changes: 14 additions & 0 deletions src/utils/get-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { listTemplates } from './list-templates'
import { listVersions } from './list-versions'
import { runVersionCheck } from './run-version-check'
import { Template } from './template'
import { validateProjectName } from './validate-project-name'
import { PackageManager } from './vendor/package-manager'

const minimalTemplateName = 'nextjs-anchor'
Expand Down Expand Up @@ -61,6 +62,19 @@ Examples:
const result = input.opts()
const verbose = result.verbose ?? false

// The positional name flows into the generated package.json and the rename search key, so it
// must pass the same validation as the interactive prompt. Validate before fetching template
// data so a network failure can't mask the name error, but not for the informational list
// commands, which only use the name to build a hint.
const isListCommand = result.listTemplateIds || result.listTemplates || result.listVersions
if (name && !isListCommand) {
const nameError = validateProjectName(name)
if (nameError) {
log.error(nameError)
throw new Error(nameError)
}
}

// Fetch the templates url, parse the template data and create menu items following our menu config
const { items, templates } = await fetchTemplateData({ config: getMenuConfig(), url: result.templatesUrl, verbose })

Expand Down
47 changes: 47 additions & 0 deletions test/get-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ vi.mock('../src/utils/get-prompts', () => ({
getPrompts: vi.fn(),
}))

vi.mock('node:process', async (importOriginal) => {
const actual = await importOriginal<typeof import('node:process')>()
return {
...actual,
exit: vi.fn((code?: number) => {
throw new Error(`process.exit(${code})`)
}),
}
})

vi.mock('../src/utils/run-version-check', () => ({
runVersionCheck: vi.fn(),
}))
Expand Down Expand Up @@ -130,6 +140,43 @@ describe('getArgs', () => {
expect(args.template).toBe(template)
})

it('rejects an invalid positional project name', async () => {
const message =
'Please enter a valid project name (lowercase letters, numbers, and single dashes, starting with a letter)'

await expect(
getArgs(['node', 'create-solana-dapp', 'My_App', '--template', 'basic', '--skip-version-check'], app),
).rejects.toThrow(message)

expect(log.error).toHaveBeenCalledWith(message)
expect(fetchTemplateData).not.toHaveBeenCalled()
expect(getPrompts).not.toHaveBeenCalled()
})

it('rejects a positional project name whose directory already exists', async () => {
// The 'test' directory exists in the repo root, which is the cwd when running vitest
await expect(
getArgs(['node', 'create-solana-dapp', 'test', '--template', 'basic', '--skip-version-check'], app),
).rejects.toThrow('Directory already exists')

expect(log.error).toHaveBeenCalledWith('Directory already exists')
expect(fetchTemplateData).not.toHaveBeenCalled()
expect(getPrompts).not.toHaveBeenCalled()
})

it('skips project name validation for informational list commands', async () => {
const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => {})

await expect(
getArgs(['node', 'create-solana-dapp', 'My_App', '--list-template-ids', '--skip-version-check'], app),
).rejects.toThrow('process.exit(0)')

expect(log.error).not.toHaveBeenCalled()
expect(consoleLog).toHaveBeenCalledWith(JSON.stringify([template.id]))

consoleLog.mockRestore()
})

it('parses positional and registered arguments after a template-defined flag', async () => {
const args = await getArgs(
[
Expand Down
Loading