Skip to content

Commit

Permalink
feat(cli): add support for local templates
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Oct 20, 2024
1 parent 0642f23 commit 466f5c9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/cdktf-cli/src/bin/cmds/helper/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,22 @@ async function getTemplate(
}
}

// treat as remote url
if (!templates.includes(templateName)) {
return fetchRemoteTemplate(templateName);
} else {
if (templates.includes(templateName)) {
return {
Name: templateName,
Path: path.join(templatesDir, templateName),
};
}
if (
templateName.startsWith("http://") ||
templateName.startsWith("https://")
) {
return fetchRemoteTemplate(templateName);
}
return {
Name: path.basename(templateName),
Path: templateName,
};
}

async function fetchRemoteTemplate(templateUrl: string): Promise<Template> {
Expand Down
8 changes: 8 additions & 0 deletions test/typescript/init-local-template/cdktf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"language": "typescript",
"app": "npx ts-node main.ts",
"terraformProviders": [
"null@ ~> 3.1.0"
],
"context": {}
}
35 changes: 35 additions & 0 deletions test/typescript/init-local-template/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) HashiCorp, Inc
// SPDX-License-Identifier: MPL-2.0
import { TestDriver } from "../../test-helper";
const fs = require('fs').promises;
const path = require('path');

describe("local templates", () => {
test("can init", async () => {
const driver = new TestDriver(__dirname);
const templateDir = path.resolve(
__dirname,
"..",
"..",
"..",
"packages/@cdktf/cli-core/templates/typescript"
);
await driver.init(templateDir);
const files = await fs.readdir(driver.workingDirectory);
expect(files).toEqual(
expect.arrayContaining([
".gen",
".gitignore",
".npmrc",
"cdktf.json",
"help",
"jest.config.js",
"main.ts",
"package.json",
"setup.js",
"tsconfig.json",
"__tests__",
])
);
});
});

0 comments on commit 466f5c9

Please sign in to comment.