@@ -15,6 +15,8 @@ import {
15
15
} from './generators'
16
16
import { generateReadmeContent } from './generators/handlebars'
17
17
18
+ const DEFAULT_TEMPLATE_NAME = 'my-template'
19
+
18
20
/**
19
21
* Generate template files using shared template generation logic
20
22
*/
@@ -98,39 +100,47 @@ async function addPackageJsonScripts(
98
100
}
99
101
100
102
/**
101
- * Validate template name format
103
+ * Check template name format
102
104
*/
103
- function validateTemplateName ( name : string ) : boolean {
105
+ function templateNameRegex ( name : string ) : boolean {
104
106
return / ^ [ a - z 0 - 9 ] [ a - z 0 - 9 _ - ] * [ a - z 0 - 9 ] $ | ^ [ a - z 0 - 9 ] $ / . test ( name )
105
107
}
106
108
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
+
107
120
export const initV2Command = new commander . Command ( 'init-v2' )
108
121
. description ( 'initialize a new sandbox template using the SDK' )
109
122
. addOption ( pathOption )
110
123
. 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 )
118
129
)
119
- process . exit ( 1 )
120
130
}
131
+
121
132
return value
122
133
} )
123
134
. option (
124
135
'-l, --language <language>' ,
125
136
`target language: ${ Object . values ( Language ) . join ( ', ' ) } ` ,
126
137
( value ) => {
127
138
if ( ! Object . values ( Language ) . includes ( value as Language ) ) {
128
- console . error (
139
+ throw new commander . InvalidArgumentError (
129
140
`Invalid language. Must be one of: ${ Object . values ( Language ) . join (
130
141
', '
131
142
) } `
132
143
)
133
- process . exit ( 1 )
134
144
}
135
145
return value as Language
136
146
}
@@ -147,31 +157,24 @@ export const initV2Command = new commander.Command('init-v2')
147
157
console . log ( '🚀 Initializing Sandbox Template...\n' )
148
158
149
159
// 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 ) {
160
162
templateName = await input ( {
161
163
message : 'Enter template name (alias):' ,
162
- default : 'my-template' ,
164
+ default : DEFAULT_TEMPLATE_NAME ,
163
165
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
169
170
}
171
+
170
172
return true
171
173
} ,
172
174
} )
173
175
}
174
176
templateName = templateName . trim ( )
177
+ console . log ( `Using template name: ${ templateName } ` )
175
178
176
179
// Step 2: Get language (from CLI or prompt)
177
180
let language : Language
0 commit comments