Skip to content

Commit 3f1f974

Browse files
authored
chore: experimental cdk migrate command (aws#25859)
cdk migrate command. See readme for additional description of command. Default input path is a file named "template.txt" in the parent directory for now. Default output path is the current directory. If you have any issues with the actually generated code, please create an issue on https://github.com/iph/noctilucent Please refer to noctilucent or command help supported languages to see what languages are supported. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 341de48 commit 3f1f974

File tree

36 files changed

+214
-61
lines changed

36 files changed

+214
-61
lines changed

.gitmodules

Whitespace-only changes.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"patch-package": "^6.5.1",
3434
"semver": "^7.5.2",
3535
"standard-version": "^9.5.0",
36-
"typescript": "~4.9.5"
36+
"ts-node": "^10.9.1",
37+
"typescript": "~5.1.3"
3738
},
3839
"resolutions": {
3940
"colors": "1.4.0",

packages/@aws-cdk-testing/cli-integ/lib/corking.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ export class MemoryStream extends stream.Writable {
1919
this.parts.splice(0, this.parts.length);
2020
}
2121

22-
public async flushTo(strm: NodeJS.WritableStream) {
22+
public async flushTo(strm: NodeJS.WritableStream): Promise<void> {
2323
const flushed = strm.write(this.buffer());
2424
if (!flushed) {
2525
return new Promise(ok => strm.once('drain', ok));
2626
}
27+
return;
2728
}
2829

2930
public toString() {

packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/tsconfig-custom.json

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"inlineSourceMap": true,
55
"inlineSources": true,
66
"alwaysStrict": true,
7-
"charset": "utf8",
87
"declaration": true,
98
"emitDecoratorMetadata": true,
109
"experimentalDecorators": true,

packages/@aws-cdk-testing/framework-integ/test/aws-lambda-nodejs/tsconfig.json

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"inlineSourceMap": true,
55
"inlineSources": true,
66
"alwaysStrict": true,
7-
"charset": "utf8",
87
"declaration": true,
98
"experimentalDecorators": true,
109
"incremental": true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
commit=${CODEBUILD_RESOLVED_SOURCE_VERSION:-}
5+
# CODEBUILD_RESOLVED_SOURCE_VERSION is not defined (i.e. local build or CodePipeline build),
6+
# use the HEAD commit hash
7+
if [ -z "${commit}" ]; then
8+
commit="$(git rev-parse --verify HEAD)"
9+
fi
10+
11+
cat > build-info.json <<HERE
12+
{
13+
"comment": "Generated at $(date -u +"%Y-%m-%dT%H:%M:%SZ") by generate.sh",
14+
"commit": "${commit:0:7}"
15+
}
16+
HERE

packages/@aws-cdk/cli-lib-alpha/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"build+test+package": "yarn build+test && yarn package",
5353
"bundle": "esbuild --bundle lib/index.ts --target=node14 --platform=node --external:fsevents --minify-whitespace --outfile=lib/main.js",
5454
"compat": "cdk-compat",
55-
"gen": "../../../packages/aws-cdk/generate.sh",
55+
"gen": "./generate.sh",
5656
"lint": "cdk-lint",
5757
"package": "cdk-package",
5858
"pkglint": "pkglint -f",

packages/aws-cdk-lib/aws-lambda-nodejs/lib/bundling.ts

-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ export class Bundling implements cdk.BundlingOptions {
216216
...this.props.metafile ? [`--metafile=${pathJoin(options.outputDir, 'index.meta.json')}`] : [],
217217
...this.props.banner ? [`--banner:js=${JSON.stringify(this.props.banner)}`] : [],
218218
...this.props.footer ? [`--footer:js=${JSON.stringify(this.props.footer)}`] : [],
219-
...this.props.charset ? [`--charset=${this.props.charset}`] : [],
220219
...this.props.mainFields ? [`--main-fields=${this.props.mainFields.join(',')}`] : [],
221220
...this.props.inject ? this.props.inject.map(i => `--inject:${i}`) : [],
222221
...this.props.esbuildArgs ? [toCliArgs(this.props.esbuildArgs)] : [],

packages/aws-cdk-lib/aws-lambda-nodejs/lib/util.ts

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export function getTsconfigCompilerOptions(tsconfigPath: string): string {
149149
const compilerOptions = extractTsConfig(tsconfigPath);
150150
const excludedCompilerOptions = [
151151
'composite',
152+
'charset',
152153
'noEmit',
153154
'tsBuildInfoFile',
154155
];

packages/aws-cdk-lib/aws-lambda-nodejs/test/bundling.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ test('esbuild bundling with esbuild options', () => {
233233
defineInstructions,
234234
'--log-level=silent --keep-names --tsconfig=/asset-input/lib/custom-tsconfig.ts',
235235
'--metafile=/asset-output/index.meta.json --banner:js="/* comments */" --footer:js="/* comments */"',
236-
'--charset=utf8 --main-fields=module,main --inject:./my-shim.js',
236+
'--main-fields=module,main --inject:./my-shim.js',
237237
'--log-limit="0" --resolve-extensions=".ts,.js" --splitting --keep-names',
238238
].join(' '),
239239
],

packages/aws-cdk-lib/aws-lambda-nodejs/test/util.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ describe('getTsconfigCompilerOptions', () => {
186186
const compilerOptions = getTsconfigCompilerOptions(tsconfig);
187187
expect(compilerOptions).toEqual([
188188
'--alwaysStrict',
189-
'--charset utf8',
190189
'--declaration',
191190
'--declarationMap false',
192191
'--experimentalDecorators',
@@ -219,7 +218,6 @@ describe('getTsconfigCompilerOptions', () => {
219218
const compilerOptions = getTsconfigCompilerOptions(tsconfig);
220219
expect(compilerOptions).toEqual([
221220
'--alwaysStrict',
222-
'--charset utf8',
223221
'--declaration',
224222
'--declarationMap false',
225223
'--experimentalDecorators',

packages/aws-cdk-lib/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"stripDeprecated": true,
3333
"compressAssembly": true,
3434
"pre": [
35-
"npx ts-node -P tsconfig.dev.json region-info/build-tools/generate-static-data.ts",
35+
"ts-node -P tsconfig.dev.json region-info/build-tools/generate-static-data.ts",
3636
"(cp -f $(node -p 'require.resolve(\"aws-sdk/apis/metadata.json\")') custom-resources/lib/aws-custom-resource/sdk-api-metadata.json && rm -rf custom-resources/test/aws-custom-resource/cdk.out)",
3737
"(rm -rf core/test/fs/fixtures && cd core/test/fs && tar -xzf fixtures.tar.gz)",
3838
"(rm -rf assets/test/fs/fixtures && cd assets/test/fs && tar -xzvf fixtures.tar.gz)",
@@ -162,7 +162,7 @@
162162
"ts-mock-imports": "^1.3.8",
163163
"ts-node": "^10.9.1",
164164
"sinon": "^9.2.4",
165-
"typescript": "~5.0.4",
165+
"typescript": "~5.1.3",
166166
"typescript-json-schema": "^0.56.0"
167167
},
168168
"peerDependencies": {

packages/aws-cdk-migration/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"license": "Apache-2.0",
3636
"dependencies": {
3737
"glob": "^7.2.3",
38-
"typescript": "~4.9.5"
38+
"typescript": "~5.1.3"
3939
},
4040
"devDependencies": {
4141
"@types/glob": "^7.2.0",

packages/aws-cdk/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const baseConfig = require('@aws-cdk/cdk-build-tools/config/eslintrc');
22
baseConfig.ignorePatterns.push('lib/init-templates/**/typescript/**/*.ts');
33
baseConfig.ignorePatterns.push('test/integ/cli/sam_cdk_integ_app/**/*.ts');
4+
baseConfig.ignorePatterns.push('vendor/noctilucent/**/*.ts');
45
baseConfig.parserOptions.project = __dirname + '/tsconfig.json';
56
module.exports = baseConfig;

packages/aws-cdk/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ test/integ/cli/*.d.ts
3939
.DS_Store
4040

4141
junit.xml
42+
43+
# Exclude the noctilucent WASM package
44+
lib/vendor/noctilucent/

packages/aws-cdk/.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ tsconfig.json
2828
# exclude cdk artifacts
2929
**/cdk.out
3030
junit.xml
31+
32+
# exclude noctilucent source
33+
/vendor/noctilucent/

packages/aws-cdk/generate.sh

+29-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,32 @@ cat > build-info.json <<HERE
1313
"comment": "Generated at $(date -u +"%Y-%m-%dT%H:%M:%SZ") by generate.sh",
1414
"commit": "${commit:0:7}"
1515
}
16-
HERE
16+
HERE
17+
18+
# Build noctilucent package
19+
(
20+
# Check out the submodule if it's not there already
21+
if [ ! -f "vendor/noctilucent/Cargo.toml" ]; then
22+
git -C ./vendor clone https://github.com/iph/noctilucent.git
23+
fi
24+
25+
# update the package to the pinned commit hash
26+
git -C vendor/noctilucent reset --hard HEAD
27+
git -C vendor/noctilucent fetch && git -C vendor/noctilucent checkout 6da7c9fade55f8443bba7b8fdfcd4ebfe5208fb1
28+
29+
# Install wasm-pack if it's not there already
30+
if ! command -v wasm-pack >/dev/null 2>/dev/null; then
31+
echo "installing wasm-pack, this may take a while..."
32+
cargo install wasm-pack
33+
fi
34+
35+
pkgroot=$(cd $(dirname -- "$0") && pwd)
36+
37+
cd vendor/noctilucent
38+
wasm-pack build --target nodejs \
39+
--out-dir="${pkgroot}/lib/vendor/noctilucent" \
40+
--out-name=index
41+
42+
cd ../../lib/vendor/noctilucent
43+
rm package.json
44+
)

packages/aws-cdk/lib/cli.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { CdkToolkit, AssetBuildTime } from '../lib/cdk-toolkit';
2222
import { realHandler as context } from '../lib/commands/context';
2323
import { realHandler as docs } from '../lib/commands/docs';
2424
import { realHandler as doctor } from '../lib/commands/doctor';
25+
import { MIGRATE_SUPPORTED_LANGUAGES, cliMigrate } from '../lib/commands/migrate';
2526
import { RequireApproval } from '../lib/diff';
2627
import { availableInitLanguages, cliInit, printAvailableTemplates } from '../lib/init';
2728
import { data, debug, error, print, setLogLevel, setCI } from '../lib/logging';
@@ -269,6 +270,12 @@ async function parseCommandLineArguments(args: string[]) {
269270
.option('list', { type: 'boolean', desc: 'List the available templates' })
270271
.option('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' }),
271272
)
273+
.command('migrate', false /* hidden from "cdk --help" */, (yargs: Argv) => yargs
274+
.option('language', { type: 'string', alias: 'l', desc: 'The language to be used for the new project (default can be configured in ~/.cdk.json)', choices: MIGRATE_SUPPORTED_LANGUAGES })
275+
.option('input-path', { type: 'string', alias: 'inputpath', desc: 'The path to the CloudFormation template to migrate' })
276+
.option('output-path', { type: 'string', alias: 'outputpath', desc: 'The output path for the migrated cdk code' })
277+
.option('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' }),
278+
)
272279
.command('context', 'Manage cached context values', (yargs: Argv) => yargs
273280
.option('reset', { alias: 'e', desc: 'The context key (or its index) to reset', type: 'string', requiresArg: true })
274281
.option('force', { alias: 'f', desc: 'Ignore missing key error', type: 'boolean', default: false })
@@ -649,14 +656,16 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
649656
} else {
650657
return cliInit(args.TEMPLATE, language, undefined, args.generateOnly);
651658
}
659+
case 'migrate':
660+
const migrateLanguage = configuration.settings.get(['language']);
661+
return cliMigrate(args.inputpath, migrateLanguage, args.generateOnly, args.outputpath);
652662
case 'version':
653663
return data(version.DISPLAY_VERSION);
654664

655665
default:
656666
throw new Error('Unknown command: ' + command);
657667
}
658668
}
659-
660669
}
661670

662671
/**
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import { initializeProject, availableInitTemplates } from '../../lib/init';
4+
import { warning } from '../logging';
5+
import * as nocti from '../vendor/noctilucent';
6+
7+
/** The list of languages supported by the built-in noctilucent binary. */
8+
export const MIGRATE_SUPPORTED_LANGUAGES: readonly string[] = nocti.supported_languages();
9+
10+
export async function cliMigrate(
11+
inputpath: string = process.cwd() + '/../template.txt',
12+
language = MIGRATE_SUPPORTED_LANGUAGES[0],
13+
generateOnly = false,
14+
outputpath = process.cwd(),
15+
) {
16+
warning('This is an experimental feature. We make no guarantees about the outcome or stability of the functionality.');
17+
const type = 'default'; // "default" is the default type (and maps to 'app')
18+
const template = (await availableInitTemplates()).find(t => t.hasName(type!));
19+
if (!template) {
20+
throw new Error(`couldn't find template for ${type} app type, this should never happen`);
21+
}
22+
23+
if (!MIGRATE_SUPPORTED_LANGUAGES.includes(language)) {
24+
throw new Error(`Unsupported language for cdk migrate: ${language}. Supported languages are: ${MIGRATE_SUPPORTED_LANGUAGES.join(', ')}`);
25+
}
26+
27+
await initializeProject(template, language, true, generateOnly, outputpath);
28+
const template_file = fs.readFileSync(inputpath, 'utf8');
29+
const generated_app = nocti.transmute(template_file, language);
30+
31+
// clear out the init'd bin/lib files to replace with our own
32+
delete_files(outputpath + '/lib/');
33+
34+
// we hardcode everything to be called noctstack still so this works for now.
35+
// Will change this to be much smarter once we can change stack name in noct
36+
const bin_app = `#!/usr/bin/env node
37+
import 'source-map-support/register';
38+
import * as cdk from 'aws-cdk-lib';
39+
import { NoctStack } from '../lib/generated_stack';
40+
41+
const app = new cdk.App();
42+
new NoctStack(app, 'NoctStack', {
43+
/* If you don't specify 'env', this stack will be environment-agnostic.
44+
* Account/Region-dependent features and context lookups will not work,
45+
* but a single synthesized template can be deployed anywhere. */
46+
47+
/* Uncomment the next line to specialize this stack for the AWS Account
48+
* and Region that are implied by the current CLI configuration. */
49+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
50+
51+
/* Uncomment the next line if you know exactly what Account and Region you
52+
* want to deploy the stack to. */
53+
// env: { account: '123456789012', region: 'us-east-1' },
54+
55+
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
56+
});`;
57+
const myname = path.basename(outputpath);
58+
fs.writeFileSync(outputpath + '/lib/' + 'generated_stack.ts', generated_app);
59+
fs.writeFileSync(outputpath + '/bin/' + `${myname}.ts`, bin_app);
60+
}
61+
62+
function delete_files(filepath: string) {
63+
fs.readdir(filepath, (err, files) => {
64+
if (err) throw err;
65+
for (const file of files) {
66+
fs.unlink(filepath + file, (cause) => {
67+
if (cause) throw cause;
68+
});
69+
}
70+
});
71+
}

packages/aws-cdk/lib/init-templates/app/typescript/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"ts-jest": "^29.1.0",
1818
"aws-cdk": "%cdk-version%",
1919
"ts-node": "^10.9.1",
20-
"typescript": "~5.0.4"
20+
"typescript": "~5.1.3"
2121
},
2222
"dependencies": {
2323
"aws-cdk-lib": "%cdk-version%",

packages/aws-cdk/lib/init-templates/lib/typescript/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"constructs": "%constructs-version%",
1616
"jest": "^29.5.0",
1717
"ts-jest": "^29.1.0",
18-
"typescript": "~5.0.4"
18+
"typescript": "~5.1.3"
1919
},
2020
"peerDependencies": {
2121
"aws-cdk-lib": "%cdk-version%",

packages/aws-cdk/lib/init-templates/sample-app/typescript/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"ts-jest": "^29.1.0",
1818
"aws-cdk": "%cdk-version%",
1919
"ts-node": "^10.9.1",
20-
"typescript": "~5.0.4"
20+
"typescript": "~5.1.3"
2121
},
2222
"dependencies": {
2323
"aws-cdk-lib": "%cdk-version%",

packages/aws-cdk/lib/init.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export async function printAvailableTemplates(language?: string) {
247247
}
248248
}
249249

250-
async function initializeProject(template: InitTemplate, language: string, canUseNetwork: boolean, generateOnly: boolean, workDir: string) {
250+
export async function initializeProject(template: InitTemplate, language: string, canUseNetwork: boolean, generateOnly: boolean, workDir: string) {
251251
await assertIsEmptyDirectory(workDir);
252252
print(`Applying project template ${chalk.green(template.name)} for ${chalk.blue(language)}`);
253253
await template.install(language, workDir);

packages/aws-cdk/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
},
3636
"resources": {
3737
"../../node_modules/vm2/lib/bridge.js": "lib/bridge.js",
38-
"../../node_modules/vm2/lib/setup-sandbox.js": "lib/setup-sandbox.js"
38+
"../../node_modules/vm2/lib/setup-sandbox.js": "lib/setup-sandbox.js",
39+
"lib/vendor/noctilucent/index_bg.wasm": "lib/index_bg.wasm"
3940
},
4041
"allowedLicenses": [
4142
"Apache-2.0",

packages/aws-cdk/tsconfig.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"compilerOptions": {
33
"target": "ES2020",
44
"module": "commonjs",
5-
"lib": ["es2019", "es2020", "dom"],
5+
"lib": [
6+
"es2019",
7+
"es2020",
8+
"dom"
9+
],
610
"strict": true,
711
"alwaysStrict": true,
812
"skipLibCheck": true,
@@ -25,7 +29,8 @@
2529
],
2630
"exclude": [
2731
"lib/init-templates/**/typescript/*/*.ts",
28-
"test/integ/cli/sam_cdk_integ_app/**/*"
32+
"test/integ/cli/sam_cdk_integ_app/**/*",
33+
"vendor/**/*",
2934
],
3035
"references": [
3136
{

packages/aws-cdk/vendor/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Vendored-in dependencies
2+
3+
The dependencies in this directory are checked out using the `gen` script.
4+
This will fetch and clone the noctilucent crate and generate the wasm code if
5+
that has not been done already, ensuring the dependencies are adequately
6+
checked out.
7+
8+
In order to update the notcilucent crate, run the ./generate.sh script. If you wish
9+
to update to a different noctilucent commit hash instead of the one provided, modify
10+
the hash in the generate.sh script and then rerun ./generate.sh
11+
12+
The `THIRD_PARTY_LICENSES` file might need updating accordingly, which can be
13+
automatically done by running `yarn pkglint`.

packages/awslint/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@types/jest": "^29.5.1",
3131
"@types/yargs": "^15.0.15",
3232
"@aws-cdk/pkglint": "0.0.0",
33-
"typescript": "~4.9.5",
33+
"typescript": "~5.1.3",
3434
"@typescript-eslint/eslint-plugin": "^4.33.0",
3535
"@typescript-eslint/parser": "^4.33.0",
3636
"eslint": "^7.32.0",

0 commit comments

Comments
 (0)