Skip to content

Commit

Permalink
update to render devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
leehuwuj committed Mar 7, 2024
1 parent 3a7dbc6 commit ddee5a5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 88 deletions.
22 changes: 3 additions & 19 deletions packages/create-llama/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import fs from "fs";
import terminalLink from "terminal-link";
import type { InstallTemplateArgs } from "./helpers";
import { installTemplate } from "./helpers";
import { copy } from "./helpers/copy";
import { writeDevcontainer } from "./helpers/devcontainer";
import { templatesDir } from "./helpers/dir";
import { toolsRequireConfig } from "./helpers/tools";

Expand Down Expand Up @@ -112,26 +112,8 @@ export async function createApp({
path.join(templatesDir, "README-fullstack.md"),
path.join(root, "README.md"),
);
// copy dev container for fullstack
const devContainerPath =
framework === "fastapi"
? path.join(templatesDir, "devcontainers", "fullstack-python-be")
: path.join(templatesDir, "devcontainers", "fullstack-node-be");
await copy("**", path.join(root, ".devcontainer"), {
parents: true,
cwd: devContainerPath,
});
} else {
await installTemplate({ ...args, backend: true, forBackend: framework });
// copy dev container for backend
const devContainerPath =
framework === "fastapi"
? path.join(templatesDir, "devcontainers", "python")
: path.join(templatesDir, "devcontainers", "node");
await copy("**", path.join(root, ".devcontainer"), {
parents: true,
cwd: devContainerPath,
});
}

process.chdir(root);
Expand All @@ -140,6 +122,8 @@ export async function createApp({
console.log();
}

await writeDevcontainer(root, templatesDir, framework, frontend);

if (toolsRequireConfig(tools)) {
console.log(
yellow(
Expand Down
44 changes: 44 additions & 0 deletions packages/create-llama/helpers/devcontainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fs from "fs";
import path from "path";
import { TemplateFramework } from "./types";

function renderDevcontainerContent(
templatesDir: string,
framework: TemplateFramework,
frontend: boolean,
) {
const devcontainerJson: any = JSON.parse(
fs.readFileSync(path.join(templatesDir, "devcontainer.json"), "utf8"),
);

if (frontend) {
devcontainerJson.postCreateCommand =
framework === "fastapi"
? "cd backend && poetry install && cd ../frontend && npm install"
: "cd backend && npm install && cd ../frontend && npm install";
} else {
devcontainerJson.postCreateCommand =
framework === "fastapi" ? "poetry install" : "npm install";
}
return JSON.stringify(devcontainerJson, null, 2);
}

export const writeDevcontainer = async (
root: string,
templatesDir: string,
framework: TemplateFramework,
frontend: boolean,
) => {
console.log("Adding .devcontainer");
const devcontainerContent = renderDevcontainerContent(
templatesDir,
framework,
frontend,
);
const devcontainerDir = path.join(root, ".devcontainer");
fs.mkdirSync(devcontainerDir);
await fs.promises.writeFile(
path.join(devcontainerDir, "devcontainer.json"),
devcontainerContent,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@
}
}
},
"forwardPorts": [3000, 8000],
"postCreateCommand": "cd backend && poetry install && cd ../frontend && npm install"
"forwardPorts": [3000, 8000]
}

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit ddee5a5

Please sign in to comment.