Skip to content

Commit dd5139e

Browse files
committed
chore: modified error handling and fixed flag conflicts with yargs
1 parent 6a487e3 commit dd5139e

File tree

2 files changed

+13
-38
lines changed

2 files changed

+13
-38
lines changed

packages/aws-cdk/lib/cli/cli-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ export async function makeConfig(): Promise<CliConfig> {
395395
'list': { type: 'boolean', desc: 'List the available templates' },
396396
'generate-only': { type: 'boolean', default: false, desc: 'If true, only generates project files, without executing additional operations such as setting up a git repo, installing dependencies or compiling the project' },
397397
'lib-version': { type: 'string', alias: 'V', default: undefined, desc: 'The version of the CDK library (aws-cdk-lib) to initialize the project with. Defaults to the version that was current when this CLI was built.' },
398-
'from-path': { type: 'string', desc: 'Path to a local custom template directory', requiresArg: true },
398+
'from-path': { type: 'string', desc: 'Path to a local custom template directory', requiresArg: true, conflicts: ['lib-version'] },
399399
},
400400
},
401401
'migrate': {

packages/aws-cdk/lib/commands/init/init.ts

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ const decamelize = require('decamelize');
1717

1818
export interface CliInitOptions {
1919
/**
20-
* @default - Throws error requiring template specification
20+
* Template name to initialize
21+
* @default undefined
2122
*/
2223
readonly type?: string;
2324

2425
/**
25-
* @default - Auto-detects for single-language templates or throws error
26+
* Programming language for the project
27+
* @default undefined
2628
*/
2729
readonly language?: string;
2830

@@ -37,7 +39,7 @@ export interface CliInitOptions {
3739
readonly generateOnly?: boolean;
3840

3941
/**
40-
* @default - Process.cwd()
42+
* @default process.cwd()
4143
*/
4244
readonly workDir?: string;
4345

@@ -53,13 +55,13 @@ export interface CliInitOptions {
5355

5456
/**
5557
* Override the built-in CDK version
56-
* @default - Uses built-in CDK version
58+
* @default undefined
5759
*/
5860
readonly libVersion?: string;
5961

6062
/**
6163
* Path to a local custom template directory
62-
* @default - Uses built-in templates
64+
* @default undefined
6365
*/
6466
readonly fromPath?: string;
6567

@@ -75,19 +77,6 @@ export async function cliInit(options: CliInitOptions) {
7577
const generateOnly = options.generateOnly ?? false;
7678
const workDir = options.workDir ?? process.cwd();
7779

78-
// Validate conflicting options
79-
if (options.fromPath && options.type) {
80-
throw new ToolkitError('Cannot specify both --from-path and template name. Use either --from-path for custom templates or specify a built-in template name.');
81-
}
82-
83-
if (options.fromPath && options.libVersion) {
84-
throw new ToolkitError('Cannot specify --lib-version with --from-path. Custom templates do not process version placeholders.');
85-
}
86-
87-
if (options.fromPath && options.stackName) {
88-
throw new ToolkitError('Cannot specify --stack-name with --from-path. Custom templates do not process stack name placeholders.');
89-
}
90-
9180
// Step 1: Load template
9281
let template: InitTemplate;
9382
if (options.fromPath) {
@@ -136,17 +125,12 @@ async function loadLocalTemplate(templatePath: string): Promise<InitTemplate> {
136125
* Load a built-in template by name
137126
* @param ioHelper - IO helper for user interaction
138127
* @param type - Template type name
139-
* @default - Throws error requiring template specification
128+
* @default undefined
140129
* @param language - Programming language filter
141-
* @default - Uses all available languages for template filtering
130+
* @default undefined
142131
* @returns Promise resolving to the loaded InitTemplate
143132
*/
144133
async function loadBuiltinTemplate(ioHelper: IoHelper, type?: string, language?: string): Promise<InitTemplate> {
145-
if (!type && !language) {
146-
await printAvailableTemplates(ioHelper, language);
147-
throw new ToolkitError('No template specified. Please specify a template name.');
148-
}
149-
150134
if (!type) {
151135
await printAvailableTemplates(ioHelper, language);
152136
throw new ToolkitError('No template specified. Please specify a template name.');
@@ -167,7 +151,7 @@ async function loadBuiltinTemplate(ioHelper: IoHelper, type?: string, language?:
167151
* @param ioHelper - IO helper for user interaction
168152
* @param template - The template to resolve language for
169153
* @param requestedLanguage - User-requested language (optional)
170-
* @default - Auto-detects for single-language templates or throws error
154+
* @default undefined
171155
* @returns Promise resolving to the selected language
172156
*/
173157
async function resolveLanguage(ioHelper: IoHelper, template: InitTemplate, requestedLanguage?: string): Promise<string> {
@@ -197,7 +181,7 @@ async function resolveLanguage(ioHelper: IoHelper, template: InitTemplate, reque
197181
async function getLanguageDirectories(templatePath: string): Promise<string[]> {
198182
const cdkSupportedLanguages = ['typescript', 'javascript', 'python', 'java', 'csharp', 'fsharp', 'go'];
199183
const languageExtensions: Record<string, string[]> = {
200-
typescript: ['.ts'],
184+
typescript: ['.ts', '.js'],
201185
javascript: ['.js'],
202186
python: ['.py'],
203187
java: ['.java'],
@@ -225,9 +209,6 @@ async function getLanguageDirectories(templatePath: string): Promise<string[]> {
225209
const validationResults = await Promise.all(languageValidationPromises);
226210
return validationResults.filter((languageName): languageName is string => languageName !== null);
227211
} catch (error: any) {
228-
if (error instanceof ToolkitError) {
229-
throw error;
230-
}
231212
throw new ToolkitError(`Cannot read template directory '${templatePath}': ${error.message}`);
232213
}
233214
}
@@ -255,12 +236,6 @@ async function hasLanguageFiles(directoryPath: string, extensions: string[]): Pr
255236
}
256237
}
257238
} catch (error: any) {
258-
// Skip directories that can't be read (permissions, broken symlinks, etc.)
259-
// but continue searching other directories
260-
if (error.code === 'ENOENT' || error.code === 'EACCES' || error.code === 'ENOTDIR') {
261-
continue;
262-
}
263-
// Re-throw unexpected errors
264239
throw error;
265240
}
266241
}
@@ -644,7 +619,7 @@ async function assertIsEmptyDirectory(workDir: string) {
644619
}
645620
} catch (e: any) {
646621
if (e.code === 'ENOENT') {
647-
await fs.mkdirp(workDir);
622+
throw new ToolkitError(`Directory does not exist: ${workDir}. Please create the directory first.`);
648623
} else {
649624
throw e;
650625
}

0 commit comments

Comments
 (0)