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(remodel): generate index.ts and .jsiirc.json files #23970

Merged
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
2 changes: 1 addition & 1 deletion tools/@aws-cdk/cfn2ts/lib/augmentation-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import { SpecName } from './spec-utils';
* ```
*/
export class AugmentationGenerator {
public readonly outputFile: string;
private readonly code = new CodeMaker();
private readonly outputFile: string;

constructor(moduleName: string, private readonly spec: schema.Specification, private readonly affix: string) {
this.outputFile = `${moduleName}-augmentations.generated.ts`;
Expand Down
2 changes: 1 addition & 1 deletion tools/@aws-cdk/cfn2ts/lib/canned-metrics-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import { CodeMaker, toCamelCase } from 'codemaker';
* ```
*/
export class CannedMetricsGenerator {
public readonly outputFile: string;
private readonly code = new CodeMaker({ indentationLevel: 2 });
private readonly outputFile: string;

constructor(moduleName: string, private readonly namespace: string) {
this.outputFile = `${moduleName}-canned-metrics.generated.ts`;
Expand Down
39 changes: 36 additions & 3 deletions tools/@aws-cdk/cfn2ts/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function generateAll(outPath: string, options: CodeGeneratorOptions
const spec = cfnSpec.filteredSpecification(s => s.startsWith(`${scope}::`));
const module = pkglint.createModuleDefinitionFromCfnNamespace(scope);
const packagePath = path.join(outPath, module.moduleName);
const libPath = path.join(packagePath, '/lib');

if (Object.keys(spec.ResourceTypes).length === 0) {
throw new Error(`No resource was found for scope ${scope}`);
Expand All @@ -56,16 +57,48 @@ export async function generateAll(outPath: string, options: CodeGeneratorOptions

const generator = new CodeGenerator(name, spec, affix, options);
generator.emitCode();
await generator.save(packagePath);
await generator.save(libPath);
const outputFiles = [generator.outputFile];

const augs = new AugmentationGenerator(name, spec, affix);
if (augs.emitCode()) {
await augs.save(packagePath);
await augs.save(libPath);
outputFiles.push(augs.outputFile);
}

const canned = new CannedMetricsGenerator(name, scope);
if (canned.generate()) {
await canned.save(packagePath);
await canned.save(libPath);
outputFiles.push(canned.outputFile);
}

// Create index.ts files if needed
if (!fs.existsSync(path.join(packagePath, 'index.ts'))) {
await fs.writeFile(path.join(packagePath, 'index.ts'), 'export * from ./lib;\n');
}
if (!fs.existsSync(path.join(libPath, 'index.ts'))) {
const lines = [`// ${scope} CloudFormation Resources:`];
lines.push(...outputFiles.map((f) => `export * from './${f.replace('.ts', '')}'`));

await fs.writeFile(path.join(libPath, 'index.ts'), lines.join('\n') + '\n');
}

// Create .jsiirc.json files if needed
if (!fs.existsSync(path.join(packagePath, '.jsiirc.json'))) {
const jsiirc = {
targets: {
java: {
package: module.javaPackage,
},
dotnet: {
package: module.dotnetPackage,
},
python: {
module: module.pythonModuleName,
},
},
};
await fs.writeJson(path.join(packagePath, '.jsiirc.json'), jsiirc, { spaces: 2 });
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateAll } from '@aws-cdk/cfn2ts';
import * as path from 'path';

const srcDir = path.join(__dirname, '..', 'lib');
const srcDir = path.join(__dirname, '..');
generateAll(srcDir, {
coreImport: 'aws-cdk-lib',
});