-
Notifications
You must be signed in to change notification settings - Fork 77
feat(cli): auto-detect language for single-language custom templates #819
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
Changes from 1 commit
bc5eca1
21c505f
0bf2c03
0d194f9
6975eae
9766049
9def72c
2be5f7a
14c188a
1c4d691
30be191
2a428d7
0eaefff
17bd208
30b99b4
421cff6
f5c0f15
e6e8dbb
1808f42
96f6f0b
b5cb197
c091f72
70b3dea
325a749
3406ff7
07895ef
7801fb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -84,7 +84,7 @@ export async function cliInit(options: CliInitOptions) { | |||
| const generateOnly = options.generateOnly ?? false; | ||||
| const workDir = options.workDir ?? process.cwd(); | ||||
|
|
||||
| // Show available templates if no type and no language provided (main branch logic) | ||||
| // Show available templates only if no fromPath, type, or language provided | ||||
| if (!options.fromPath && !options.type && !options.language) { | ||||
| await printAvailableTemplates(ioHelper); | ||||
| return; | ||||
|
|
@@ -298,6 +298,24 @@ async function hasLanguageFiles(directoryPath: string, extensions: string[]): Pr | |||
| return false; | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Get file extensions for a specific language | ||||
| * @param language - The programming language | ||||
| * @returns Array of file extensions for the language | ||||
| */ | ||||
| function getLanguageExtensions(language: string): string[] { | ||||
| const languageExtensions: Record<string, string[]> = { | ||||
| typescript: ['.ts', '.js'], | ||||
| javascript: ['.js'], | ||||
| python: ['.py'], | ||||
| java: ['.java'], | ||||
| csharp: ['.cs'], | ||||
| fsharp: ['.fs'], | ||||
| go: ['.go'], | ||||
| }; | ||||
| return languageExtensions[language] || []; | ||||
| } | ||||
|
|
||||
| /** | ||||
| * Returns the name of the Python executable for this OS | ||||
| * @returns The Python executable name for the current platform | ||||
|
|
@@ -336,10 +354,33 @@ export class InitTemplate { | |||
| throw new ToolkitError(`Template path does not exist: ${basePath}`); | ||||
| } | ||||
|
|
||||
| const languages = await getLanguageDirectories(basePath); | ||||
| let actualBasePath = basePath; | ||||
| let languages = await getLanguageDirectories(basePath); | ||||
|
|
||||
| // Auto-detect single language templates | ||||
| if (languages.length === 0) { | ||||
| const entries = await fs.readdir(basePath, { withFileTypes: true }); | ||||
|
||||
| async function getLanguageDirectories(templatePath: string): Promise<string[]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the return signature of getLanguageDirectories to include entries, so that we can reuse it here and speed up the code.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to name these, iterate the keys from languageExtensions earlier in the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed this by implementing my suggestion. Moved language extensions to a new interface LanguageInfo.
iankhou marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two variables here have the same value—as long as hasLanguageFiles is a pure function (which it should be, if it's not, it should be corrected).
langDirPath and actualBasePath.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed this by consolidating the two variables.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a silent failure if hasValidFiles is falsy. Shouldn't we fail or at least log a warning in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed dead code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have languageExtensions in this file... https://github.com/aws/aws-cdk-cli/pull/819/files#diff-8a5cd4c3a707b56e2e21dd1333dd816bcc07a507effa6c3ad91d0d04381a39d6R238-R245
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved language extensions to new interface
LanguageInfo, which is the new, consolidated source of truth.