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(cli): add support for local templates #3746

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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__",
])
);
});
});
Loading