Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.
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
76 changes: 41 additions & 35 deletions packages/cdktf-cli/lib/get/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { CodeMaker } from 'codemaker';
import { withTempDir, shell } from '../util';
import { jsiiCompile } from './jsii';
import { mkdtemp } from '../util';
import * as srcmak from 'jsii-srcmak';
import { TerraformProviderConstraint } from './generator/provider-generator';

export enum Language {
Expand All @@ -19,6 +19,12 @@ export interface GetOptions {
readonly codeMakerOutput: string;
readonly targetNames: string[];
readonly isModule?: boolean;

/**
* Path to copy the output .jsii file.
* @default - jsii file is not emitted
*/
readonly outputJsii?: string;
}

export abstract class GetBase {
Expand All @@ -35,48 +41,48 @@ export abstract class GetBase {

if (isTypescript) {
await code.save(codeMakerOutdir);
return
}

for (const name of options.targetNames) {
// this is not typescript, so we generate in a staging directory and harvest the code
await withTempDir('get', async () => {
if (!isTypescript || options.outputJsii) {
for (const name of options.targetNames) {
const terraformProvider = new TerraformProviderConstraint(name)
const source = isModule ? terraformProvider.fqn : terraformProvider.name;
const compatibleName = source.replace(/\//gi, '_')
await code.save('.');
await jsiiCompile('.', {
main: source,
name: compatibleName,
providerPath: this.typesPath(source)
});
const providerPath = this.typesPath(source);
const fileName = `${path.join(providerPath)}.ts`

const pacmak = require.resolve('jsii-pacmak/bin/jsii-pacmak');
await shell(pacmak, [ '--target', options.targetLanguage, '--code-only' ]);
await this.harvestCode(options, codeMakerOutdir, source.replace(/-/gi, '_'));
});
}
}
await mkdtemp(async staging => {

private async harvestCode(options: GetOptions, targetdir: string, targetName: string) {
switch (options.targetLanguage) {
case Language.TYPESCRIPT:
throw new Error('no op for typescript');
// this is not typescript, so we generate in a staging directory and
// use jsii-srcmak to compile and extract the language-specific source
// into our project.
await code.save(staging);

case Language.PYTHON:
await this.harvestPython(targetdir, targetName);
break;
// these are the module dependencies we compile against
const deps = ['@types/node', 'constructs', 'cdktf'];

default:
throw new Error(`unsupported language ${options.targetLanguage} (yet)`);
}
}
const opts: srcmak.Options = {
entrypoint: fileName,
deps: deps.map(dep => path.dirname(require.resolve(`${dep}/package.json`))),
moduleKey: source.replace(/\//gi, '_')
};

// used for testing.
if (options.outputJsii) {
opts.jsii = { path: options.outputJsii };
}

// python!
if (options.targetLanguage === Language.PYTHON) {
opts.python = {
outdir: codeMakerOutdir,
moduleName: source.replace(/\//gi, '.').replace(/-/gi, '_')
};
}

private async harvestPython(targetdir: string, targetName: string) {
const target = path.join(targetdir, targetName);
await fs.move(`dist/python/src/${targetName}`, target, { overwrite: true });
// Make the codeMakerOutput dir a Python package for IDEs and linting
fs.writeFileSync(path.join(targetdir, '__init__.py'), '', 'utf-8');
await srcmak.srcmak(staging, opts);
});
}
}
}

protected abstract typesPath(name: string): string;
Expand Down
85 changes: 0 additions & 85 deletions packages/cdktf-cli/lib/get/jsii.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/cdktf-cli/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ export async function withTempDir(dirname: string, closure: () => Promise<void>)
}
}

export async function mkdtemp(closure: (dir: string) => Promise<void>) {
const workdir = await fs.mkdtemp(path.join(os.tmpdir(), 'cdktf.'));
try {
await closure(workdir);
} finally {
await fs.remove(workdir);
}
}

async function get(url: string, protocol: typeof http | typeof https = https): Promise<string> {
return new Promise((ok, ko) => {
const req = protocol.get(url, res => {
Expand Down
3 changes: 1 addition & 2 deletions packages/cdktf-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
"ink": "^2.7.1",
"ink-confirm-input": "^2.0.0",
"ink-spinner": "^3.0.1",
"jsii": "1.1.0",
"jsii-pacmak": "1.1.0",
"jsii-srcmak": "^0.1.22",
"log4js": "^6.3.0",
"open": "^7.0.4",
"react": "^16.13.1",
Expand Down
Loading