Skip to content

Commit

Permalink
feat: add cdktfAppFile
Browse files Browse the repository at this point in the history
  • Loading branch information
gmeligio committed Jan 11, 2024
1 parent 08f162c commit 6ead086
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/CdktfConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export interface TerraformDependencyConstraint {

export type RequirementDefinition = string | TerraformDependencyConstraint;

// TODO: Use `type ObjectValues<T> = T[keyof T];` instead of enum
export enum Language {
TYPESCRIPT = 'typescript',
PYTHON = 'python',
Expand Down Expand Up @@ -142,15 +141,15 @@ export class CdktfConfig extends Component {
}

const filename = 'cdktf.json';
const key = 'projectId';
const projectIdKey = 'projectId';
const filePath = path.join(project.outdir, filename);

let projectId: string | undefined;
if (fs.existsSync(filePath)) {
const optionsFile = fs.readFileSync(filePath, 'utf8');
const optionsObject = JSON.parse(optionsFile);

projectId = optionsObject[key];
projectId = optionsObject[projectIdKey];
}

this.app = options.app;
Expand Down
11 changes: 10 additions & 1 deletion src/CdktfTypescriptApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export interface CdktfTypeScriptAppOptions extends typescript.TypeScriptProjectO
* @default "latest"
*/
readonly constructsVersion: string;

/**
* The file containing the CDKTF app
*
* @default "main.ts"
*/
readonly cdktfAppFile?: string;
}

export class CdktfTypeScriptApp extends TypeScriptAppProject {
Expand Down Expand Up @@ -61,8 +68,10 @@ export class CdktfTypeScriptApp extends TypeScriptAppProject {
// No compile step because we do all of it in typescript directly
this.compileTask.reset();

const appFile = options.cdktfAppFile ?? 'main.ts';

this.cdktfConfig = new CdktfConfig(this, {
app: 'npx ts-node main.ts',
app: `npx ts-node ${appFile}`,
language: Language.TYPESCRIPT,
...options.cdktfConfig,
});
Expand Down
29 changes: 28 additions & 1 deletion test/CdktfTypescriptApp.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
import { Language } from '../src/CdktfConfig';
import { CdktfTypeScriptApp } from '../src/CdktfTypescriptApp';

describe('CdktfApp', () => {
describe('CdktfTypeScriptApp', () => {
describe('cdktfAppFile', () => {
test('should use `main.ts` by default', () => {
const app = new CdktfTypeScriptApp({
name: 'test_project',
defaultReleaseBranch: 'main',
cdktfVersion: '0.0.0',
constructsVersion: '0.0.0',
});

expect(app.cdktfConfig.app).toStrictEqual('npx ts-node main.ts');
});

test('can be overridden with custom file', () => {
const appFile = 'src/main.ts';

const app = new CdktfTypeScriptApp({
name: 'test_project',
defaultReleaseBranch: 'main',
cdktfVersion: '0.0.0',
constructsVersion: '0.0.0',
cdktfAppFile: appFile,
});

expect(app.cdktfConfig.app).toStrictEqual(`npx ts-node ${appFile}`);
});
});

describe('app', () => {
test('should use `ts-node` by default', () => {
const app = new CdktfTypeScriptApp({
Expand Down

0 comments on commit 6ead086

Please sign in to comment.