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 22, 2024
1 parent fe2a712 commit 6df9072
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 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
2 changes: 1 addition & 1 deletion packages/cdktf-cli/src/bin/cmds/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Command extends BaseCommand {
.showHelpOnFail(true)
.option("template", {
type: "string",
desc: `The template to be used to create a new project. Either URL to zip file or one of the built-in templates.`,
desc: `The template to be used to create a new project. Either URL to zip file, one of the built-in templates, or a local directory.`,
})
.option("project-name", {
type: "string",
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": {}
}
36 changes: 36 additions & 0 deletions test/typescript/init-local-template/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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);
driver.switchToTempDir();
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 6df9072

Please sign in to comment.