Skip to content
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

feat(cli): control library init version with a command-line parameter #149

Merged
merged 5 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions packages/aws-cdk/lib/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,18 +490,18 @@ interface Versions {
* This has been built into the CLI at build time.
*/
async function loadInitVersions(): Promise<Versions> {
const recommendedFlagsFile = path.join(__dirname, './init-templates/.init-version.json');
const contents = JSON.parse(await fs.readFile(recommendedFlagsFile, { encoding: 'utf-8' }));
const initVersionFile = path.join(__dirname, './init-templates/.init-version.json');
const contents = JSON.parse(await fs.readFile(initVersionFile, { encoding: 'utf-8' }));

const ret = {
'aws-cdk-lib': contents['aws-cdk-lib'],
'aws-cdk-lib': process.env.CDK_INIT_LIB_VERSION ?? contents['aws-cdk-lib'],
'constructs': contents.constructs,
'aws-cdk': versionNumber(),
};
for (const [key, value] of Object.entries(ret)) {
/* istanbul ignore next */
if (!value) {
throw new ToolkitError(`Missing init version from ${recommendedFlagsFile}: ${key}`);
throw new ToolkitError(`Missing init version from ${initVersionFile}: ${key}`);
}
}

Expand Down
17 changes: 17 additions & 0 deletions packages/aws-cdk/test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ describe('constructs version', () => {
expect(await fs.pathExists(path.join(workDir, 'lib'))).toBeTruthy();
});

cliTest('can override requested version with environment variable', async (workDir) => {
process.env.CDK_INIT_LIB_VERSION = '2.100';
try {
await cliInit({
type: 'lib',
language: 'typescript',
workDir,
});

// Check that package.json and lib/ got created in the current directory
const pj = JSON.parse(await fs.readFile(path.join(workDir, 'package.json'), 'utf-8'));
expect(Object.entries(pj.devDependencies)).toContainEqual(['aws-cdk-lib', '2.100']);
} finally {
delete process.env.CDK_INIT_LIB_VERSION;
}
});

cliTest('asking for a nonexistent template fails', async (workDir) => {
await expect(cliInit({
type: 'banana',
Expand Down