Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/aws-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ $ # List the available template types & languages
$ cdk init --list
Available templates:
* app: Template for a CDK Application
└─ cdk init app --language=[csharp|fsharp|java|javascript|python|typescript]
└─ cdk init app --language=[csharp|cs|fsharp|fs|java|javascript|js|python|py|typescript|ts]
* lib: Template for a CDK Construct Library
└─ cdk init lib --language=typescript
* sample-app: Example CDK Application with some constructs
└─ cdk init sample-app --language=[csharp|fsharp|java|javascript|python|typescript]
└─ cdk init sample-app --language=[csharp|cs|fsharp|fs|java|javascript|js|python|py|typescript|ts]

$ # Create a new library application in typescript
$ cdk init lib --language=typescript
Expand Down
9 changes: 8 additions & 1 deletion packages/aws-cdk/lib/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CliHelpers, type CliConfig } from '@aws-cdk/user-input-gen';
import * as cdk_from_cfn from 'cdk-from-cfn';
import { StackActivityProgress } from '../commands/deploy';
import { availableInitLanguages } from '../commands/init';
import { getLanguageAlias } from '../commands/language';

export const YARGS_HELPERS = new CliHelpers('./util/yargs-helpers');

Expand Down Expand Up @@ -410,7 +411,13 @@ export async function makeConfig(): Promise<CliConfig> {
description: 'Migrate existing AWS resources into a CDK app',
options: {
'stack-name': { type: 'string', alias: 'n', desc: 'The name assigned to the stack created in the new project. The name of the app will be based off this name as well.', requiresArg: true },
'language': { type: 'string', default: 'typescript', alias: 'l', desc: 'The language to be used for the new project', choices: cdk_from_cfn.supported_languages() },
'language': {
type: 'string',
default: 'typescript',
alias: 'l',
desc: 'The language to be used for the new project',
choices: [...new Set(cdk_from_cfn.supported_languages().flatMap((lang) => [lang, getLanguageAlias(lang)]))],
},
'account': { type: 'string', desc: 'The account to retrieve the CloudFormation stack template from' },
'region': { type: 'string', desc: 'The region to retrieve the CloudFormation stack template from' },
'from-path': { type: 'string', desc: 'The path to the CloudFormation template to migrate. Use this for locally stored templates' },
Expand Down
12 changes: 10 additions & 2 deletions packages/aws-cdk/lib/cli/cli-type-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -845,12 +845,17 @@
"desc": "The language to be used for the new project (default can be configured in ~/.cdk.json)",
"choices": [
"csharp",
"cs",
"fsharp",
"fs",
"go",
"java",
"javascript",
"js",
"python",
"typescript"
"py",
"typescript",
"ts"
]
},
"list": {
Expand Down Expand Up @@ -901,10 +906,13 @@
"desc": "The language to be used for the new project",
"choices": [
"typescript",
"ts",
"go",
"java",
"python",
"csharp"
"py",
"csharp",
"cs"
]
},
"account": {
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-cdk/lib/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { ProxyAgentProvider } from './proxy-agent';
import { cdkCliErrorName } from './telemetry/error';
import type { ErrorDetails } from './telemetry/schema';
import { isDeveloperBuildVersion, versionWithBuild, versionNumber } from './version';
import { getLanguageFromAlias } from '../commands/language';

if (!process.stdout.isTTY) {
// Disable chalk color highlighting
Expand All @@ -42,6 +43,8 @@ if (!process.stdout.isTTY) {

export async function exec(args: string[], synthesizer?: Synthesizer): Promise<number | void> {
const argv = await parseCommandLineArguments(args);
argv.language = getLanguageFromAlias(argv.language) ?? argv.language;

const cmd = argv._[0];

// if one -v, log at a DEBUG level
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk/lib/cli/parse-command-line-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ export function parseCommandLineArguments(args: Array<string>): any {
type: 'string',
alias: 'l',
desc: 'The language to be used for the new project (default can be configured in ~/.cdk.json)',
choices: ['csharp', 'fsharp', 'go', 'java', 'javascript', 'python', 'typescript'],
choices: ['csharp', 'cs', 'fsharp', 'fs', 'go', 'java', 'javascript', 'js', 'python', 'py', 'typescript', 'ts'],
})
.option('list', {
default: undefined,
Expand Down Expand Up @@ -892,7 +892,7 @@ export function parseCommandLineArguments(args: Array<string>): any {
type: 'string',
alias: 'l',
desc: 'The language to be used for the new project',
choices: ['typescript', 'go', 'java', 'python', 'csharp'],
choices: ['typescript', 'ts', 'go', 'java', 'python', 'py', 'csharp', 'cs'],
})
.option('account', {
default: undefined,
Expand Down
3 changes: 3 additions & 0 deletions packages/aws-cdk/lib/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { IoHelper } from '../../api-private';
import { cliRootDir } from '../../cli/root-dir';
import { versionNumber } from '../../cli/version';
import { cdkHomeDir, formatErrorMessage, rangeFromSemver } from '../../util';
import { getLanguageAlias } from '../language';

/* eslint-disable @typescript-eslint/no-var-requires */ // Packages don't have @types module
// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand Down Expand Up @@ -569,7 +570,9 @@ export async function availableInitLanguages(): Promise<string[]> {
const result = new Set<string>();
for (const template of templates) {
for (const language of template.languages) {
const alias = getLanguageAlias(language);
result.add(language);
alias && result.add(alias);
}
}
return [...result];
Expand Down
31 changes: 31 additions & 0 deletions packages/aws-cdk/lib/commands/language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const SUPPORTED_LANGUAGES: { name: string; alias: string }[] = [
{ name: 'csharp', alias: 'cs' },
{ name: 'fsharp', alias: 'fs' },
{ name: 'go', alias: 'go' },
{ name: 'java', alias: 'java' },
{ name: 'javascript', alias: 'js' },
{ name: 'python', alias: 'py' },
{ name: 'typescript', alias: 'ts' },
];

/**
* get the language alias from the language name or alias
*
* @example
* getLanguageAlias('typescript') // returns 'ts'
* getLanguageAlias('python') // returns 'py'
*/
export function getLanguageAlias(language: string): string | undefined {
return SUPPORTED_LANGUAGES.find((l) => l.name === language || l.alias === language)?.alias;
}

/**
* get the language name from the language alias or name
*
* @example
* getLanguageFromAlias('ts') // returns 'typescript'
* getLanguageFromAlias('py') // returns 'python'
*/
export function getLanguageFromAlias(alias: string): string | undefined {
return SUPPORTED_LANGUAGES.find((l) => l.alias === alias || l.name === alias)?.name;
}
41 changes: 33 additions & 8 deletions packages/aws-cdk/test/cli/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as cdkToolkitModule from '../../lib/cli/cdk-toolkit';
import { exec } from '../../lib/cli/cli';
import { CliIoHost } from '../../lib/cli/io-host';
import { Configuration } from '../../lib/cli/user-configuration';
Expand Down Expand Up @@ -40,14 +41,26 @@ jest.mock('../../lib/api/notices', () => ({
}));

jest.mock('../../lib/cli/parse-command-line-arguments', () => ({
parseCommandLineArguments: jest.fn().mockImplementation((args) => Promise.resolve({
_: ['version'],
verbose: args.includes('-v') ? (
args.filter((arg: string) => arg === '-v').length
) : args.includes('--verbose') ? (
parseInt(args[args.indexOf('--verbose') + 1]) || true
) : undefined,
})),
parseCommandLineArguments: jest.fn().mockImplementation((args) => {
if (args.includes('version')) {
return Promise.resolve({
_: ['version'],
verbose: args.includes('-v')
? args.filter((arg: string) => arg === '-v').length
: args.includes('--verbose')
? parseInt(args[args.indexOf('--verbose') + 1]) || true
: undefined,
});
}
if (args.includes('migrate')) {
return Promise.resolve({
'_': ['migrate'],
'language': 'typescript',
'stack-name': 'sampleStack',
});
}
return Promise.resolve({ _: [] });
}),
}));

describe('exec verbose flag tests', () => {
Expand Down Expand Up @@ -97,3 +110,15 @@ describe('exec verbose flag tests', () => {
expect(CliIoHost.instance().logLevel).toBe('trace');
});
});

test('should convert language alias to full language name', async () => {
const migrateSpy = jest.spyOn(cdkToolkitModule.CdkToolkit.prototype, 'migrate').mockResolvedValue();

await exec(['migrate', '--language', 'ts', '--stack-name', 'sampleStack']);

expect(migrateSpy).toHaveBeenCalledWith(
expect.objectContaining({
language: 'typescript',
}),
);
});
47 changes: 47 additions & 0 deletions packages/aws-cdk/test/cli/language.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getLanguageAlias, getLanguageFromAlias } from '../../lib/commands/language';

describe('should get language alias from language name or alias', () => {
test.each([
['csharp', 'cs'],
['cs', 'cs'],
['fsharp', 'fs'],
['fs', 'fs'],
['go', 'go'],
['java', 'java'],
['javascript', 'js'],
['js', 'js'],
['python', 'py'],
['py', 'py'],
['typescript', 'ts'],
['ts', 'ts'],
])('getLanguageAlias(%s) should return %s', (input, expected) => {
expect(getLanguageAlias(input)).toBe(expected);
});

test('when unsupported language is specified, return undefined', () => {
expect(getLanguageAlias('ruby')).toBeUndefined();
});
});

describe('should get language name from language alias or name', () => {
test.each([
['csharp', 'csharp'],
['cs', 'csharp'],
['fsharp', 'fsharp'],
['fs', 'fsharp'],
['go', 'go'],
['java', 'java'],
['javascript', 'javascript'],
['js', 'javascript'],
['python', 'python'],
['py', 'python'],
['typescript', 'typescript'],
['ts', 'typescript'],
])('getLanguageFromAlias(%s) should return %s', (input, expected) => {
expect(getLanguageFromAlias(input)).toBe(expected);
});

test('when unsupported language alias is specified, return undefined', () => {
expect(getLanguageFromAlias('ruby')).toBeUndefined();
});
});
1 change: 1 addition & 0 deletions packages/aws-cdk/test/commands/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ test('check available init languages', async () => {
const langs = await availableInitLanguages();
expect(langs.length).toBeGreaterThan(0);
expect(langs).toContain('typescript');
expect(langs).toContain('ts');
});

test('exercise printing available templates', async () => {
Expand Down
Loading