Skip to content

Commit 2071cf0

Browse files
committed
unite template name validation
1 parent 05a7403 commit 2071cf0

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

packages/cli/src/commands/template/init-v2.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
} from './generators'
1616
import { generateReadmeContent } from './generators/handlebars'
1717

18+
const DEFAULT_TEMPLATE_NAME = 'my-template'
19+
1820
/**
1921
* Generate template files using shared template generation logic
2022
*/
@@ -98,39 +100,47 @@ async function addPackageJsonScripts(
98100
}
99101

100102
/**
101-
* Validate template name format
103+
* Check template name format
102104
*/
103-
function validateTemplateName(name: string): boolean {
105+
function templateNameRegex(name: string): boolean {
104106
return /^[a-z0-9][a-z0-9_-]*[a-z0-9]$|^[a-z0-9]$/.test(name)
105107
}
106108

109+
function validateTemplateName(name: string) {
110+
if (!name || name.trim().length === 0) {
111+
throw new Error('Template name cannot be empty')
112+
}
113+
if (!templateNameRegex(name.trim())) {
114+
throw new Error(
115+
'Template name must contain only lowercase letters, numbers, hyphens, and underscores, and cannot start or end with a hyphen or underscore'
116+
)
117+
}
118+
}
119+
107120
export const initV2Command = new commander.Command('init-v2')
108121
.description('initialize a new sandbox template using the SDK')
109122
.addOption(pathOption)
110123
.option('-n, --name <name>', 'template name (alias)', (value) => {
111-
if (!value || value.trim().length === 0) {
112-
console.error('Template name cannot be empty')
113-
process.exit(1)
114-
}
115-
if (!validateTemplateName(value.trim())) {
116-
console.error(
117-
'Template name must contain only lowercase letters, numbers, hyphens, and underscores, and cannot start or end with a hyphen or underscore'
124+
try {
125+
validateTemplateName(value)
126+
} catch (err) {
127+
throw new commander.InvalidArgumentError(
128+
err instanceof Error ? err.message : String(err)
118129
)
119-
process.exit(1)
120130
}
131+
121132
return value
122133
})
123134
.option(
124135
'-l, --language <language>',
125136
`target language: ${Object.values(Language).join(', ')}`,
126137
(value) => {
127138
if (!Object.values(Language).includes(value as Language)) {
128-
console.error(
139+
throw new commander.InvalidArgumentError(
129140
`Invalid language. Must be one of: ${Object.values(Language).join(
130141
', '
131142
)}`
132143
)
133-
process.exit(1)
134144
}
135145
return value as Language
136146
}
@@ -147,31 +157,24 @@ export const initV2Command = new commander.Command('init-v2')
147157
console.log('🚀 Initializing Sandbox Template...\n')
148158

149159
// Step 1: Get template name (from CLI or prompt)
150-
let templateName: string
151-
if (opts.name) {
152-
if (!validateTemplateName(opts.name)) {
153-
throw new Error(
154-
'Template name must contain only lowercase letters, numbers, hyphens, and underscores, and cannot start or end with a hyphen or underscore'
155-
)
156-
}
157-
templateName = opts.name
158-
console.log(`Using template name: ${templateName}`)
159-
} else {
160+
let templateName: string = opts.name ?? DEFAULT_TEMPLATE_NAME
161+
if (opts.name === undefined) {
160162
templateName = await input({
161163
message: 'Enter template name (alias):',
162-
default: 'my-template',
164+
default: DEFAULT_TEMPLATE_NAME,
163165
validate: (input: string) => {
164-
if (!input || input.trim().length === 0) {
165-
return 'Template name cannot be empty'
166-
}
167-
if (!validateTemplateName(input.trim())) {
168-
return 'Template name must contain only lowercase letters, numbers, hyphens, and underscores, and cannot start or end with a hyphen or underscore'
166+
try {
167+
validateTemplateName(input)
168+
} catch (err) {
169+
return err instanceof Error ? err.message : err
169170
}
171+
170172
return true
171173
},
172174
})
173175
}
174176
templateName = templateName.trim()
177+
console.log(`Using template name: ${templateName}`)
175178

176179
// Step 2: Get language (from CLI or prompt)
177180
let language: Language

0 commit comments

Comments
 (0)