From 0fb757f6c133119375570003da79a8da4d77ae24 Mon Sep 17 00:00:00 2001
From: Thuc Pham <51660321+thucpn@users.noreply.github.com>
Date: Thu, 1 Feb 2024 10:12:21 +0700
Subject: [PATCH] feat: use pnpm pack for e2e (#490)

---
 .changeset/lemon-rice-repeat.md             |  5 +++++
 .github/workflows/e2e.yml                   |  6 ++++++
 packages/create-llama/create-app.ts         |  3 ++-
 packages/create-llama/e2e/utils.ts          |  9 ++++++++-
 packages/create-llama/helpers/dir.ts        |  9 +++++++++
 packages/create-llama/helpers/index.ts      |  4 ++--
 packages/create-llama/helpers/llama-pack.ts |  8 ++++----
 packages/create-llama/helpers/python.ts     | 14 +++-----------
 packages/create-llama/helpers/typescript.ts | 12 +++---------
 packages/create-llama/package.json          |  2 +-
 10 files changed, 43 insertions(+), 29 deletions(-)
 create mode 100644 .changeset/lemon-rice-repeat.md
 create mode 100644 packages/create-llama/helpers/dir.ts

diff --git a/.changeset/lemon-rice-repeat.md b/.changeset/lemon-rice-repeat.md
new file mode 100644
index 000000000..ef998c3fa
--- /dev/null
+++ b/.changeset/lemon-rice-repeat.md
@@ -0,0 +1,5 @@
+---
+"create-llama": patch
+---
+
+feat: use pnpm pack for e2e
diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml
index 8c294c6da..70cda2ca6 100644
--- a/.github/workflows/e2e.yml
+++ b/.github/workflows/e2e.yml
@@ -49,6 +49,12 @@ jobs:
       - name: Build create-llama
         run: pnpm run build
         working-directory: ./packages/create-llama
+      - name: Pack
+        run: pnpm pack --pack-destination ./output
+        working-directory: ./packages/create-llama
+      - name: Extract Pack
+        run: tar -xvzf ./output/*.tgz -C ./output
+        working-directory: ./packages/create-llama
       - name: Run Playwright tests
         run: pnpm exec playwright test
         env:
diff --git a/packages/create-llama/create-app.ts b/packages/create-llama/create-app.ts
index 6c684d9de..4c1eaf4db 100644
--- a/packages/create-llama/create-app.ts
+++ b/packages/create-llama/create-app.ts
@@ -11,6 +11,7 @@ import fs from "fs";
 import terminalLink from "terminal-link";
 import type { InstallTemplateArgs } from "./helpers";
 import { installTemplate } from "./helpers";
+import { templatesDir } from "./helpers/dir";
 
 export type InstallAppArgs = Omit<
   InstallTemplateArgs,
@@ -100,7 +101,7 @@ export async function createApp({
     });
     // copy readme for fullstack
     await fs.promises.copyFile(
-      path.join(__dirname, "..", "templates", "README-fullstack.md"),
+      path.join(templatesDir, "README-fullstack.md"),
       path.join(root, "README.md"),
     );
   } else {
diff --git a/packages/create-llama/e2e/utils.ts b/packages/create-llama/e2e/utils.ts
index e03ec73c7..d82232780 100644
--- a/packages/create-llama/e2e/utils.ts
+++ b/packages/create-llama/e2e/utils.ts
@@ -72,7 +72,14 @@ export async function runCreateLlama(
   externalPort: number,
   postInstallAction: TemplatePostInstallAction,
 ): Promise<CreateLlamaResult> {
-  const createLlama = path.join(__dirname, "..", "dist", "index.js");
+  const createLlama = path.join(
+    __dirname,
+    "..",
+    "output",
+    "package",
+    "dist",
+    "index.js",
+  );
 
   const name = [
     templateType,
diff --git a/packages/create-llama/helpers/dir.ts b/packages/create-llama/helpers/dir.ts
new file mode 100644
index 000000000..08c2fe8fe
--- /dev/null
+++ b/packages/create-llama/helpers/dir.ts
@@ -0,0 +1,9 @@
+import path from "path";
+import { fileURLToPath } from "url";
+
+export const templatesDir = path.join(
+  fileURLToPath(import.meta.url),
+  "..",
+  "..",
+  "templates",
+);
diff --git a/packages/create-llama/helpers/index.ts b/packages/create-llama/helpers/index.ts
index 7415929d5..f5467816f 100644
--- a/packages/create-llama/helpers/index.ts
+++ b/packages/create-llama/helpers/index.ts
@@ -6,6 +6,7 @@ import path from "path";
 import { cyan } from "picocolors";
 
 import { COMMUNITY_OWNER, COMMUNITY_REPO } from "./constant";
+import { templatesDir } from "./dir";
 import { PackageManager } from "./get-pkg-manager";
 import { installLlamapackProject } from "./llama-pack";
 import { isHavingPoetryLockFile, tryPoetryRun } from "./poetry";
@@ -148,8 +149,7 @@ const copyContextData = async (
   // Copy folder
   if (dataSource?.type === "folder") {
     let srcPath =
-      dataSourceConfig.path ??
-      path.join(__dirname, "..", "templates", "components", "data");
+      dataSourceConfig.path ?? path.join(templatesDir, "components", "data");
     console.log(`\nCopying data to ${cyan(destPath)}\n`);
     await copy("**", destPath, {
       parents: true,
diff --git a/packages/create-llama/helpers/llama-pack.ts b/packages/create-llama/helpers/llama-pack.ts
index f70bc909a..adc0966af 100644
--- a/packages/create-llama/helpers/llama-pack.ts
+++ b/packages/create-llama/helpers/llama-pack.ts
@@ -2,6 +2,7 @@ import fs from "fs/promises";
 import path from "path";
 import { LLAMA_HUB_FOLDER_PATH, LLAMA_PACK_CONFIG_PATH } from "./constant";
 import { copy } from "./copy";
+import { templatesDir } from "./dir";
 import { installPythonDependencies } from "./python";
 import { getRepoRawContent } from "./repo";
 import { InstallTemplateArgs } from "./types";
@@ -29,9 +30,8 @@ const copyLlamapackEmptyProject = async ({
   root,
 }: Pick<InstallTemplateArgs, "root">) => {
   const templatePath = path.join(
-    __dirname,
-    "..",
-    "templates/components/sample-projects/llamapack",
+    templatesDir,
+    "components/sample-projects/llamapack",
   );
   await copy("**", root, {
     parents: true,
@@ -42,7 +42,7 @@ const copyLlamapackEmptyProject = async ({
 const copyData = async ({
   root,
 }: Pick<InstallTemplateArgs, "root" | "llamapack">) => {
-  const dataPath = path.join(__dirname, "..", "templates/components/data");
+  const dataPath = path.join(templatesDir, "components/data");
   await copy("**", path.join(root, "data"), {
     parents: true,
     cwd: dataPath,
diff --git a/packages/create-llama/helpers/python.ts b/packages/create-llama/helpers/python.ts
index 7fa006427..8b2ec6d90 100644
--- a/packages/create-llama/helpers/python.ts
+++ b/packages/create-llama/helpers/python.ts
@@ -4,6 +4,7 @@ import { cyan, red, yellow } from "picocolors";
 import { parse, stringify } from "smol-toml";
 import terminalLink from "terminal-link";
 import { copy } from "./copy";
+import { templatesDir } from "./dir";
 import { isPoetryAvailable, tryPoetryInstall } from "./poetry";
 import { InstallTemplateArgs, TemplateVectorDB } from "./types";
 
@@ -139,14 +140,7 @@ export const installPythonTemplate = async ({
   | "postInstallAction"
 >) => {
   console.log("\nInitializing Python project with template:", template, "\n");
-  const templatePath = path.join(
-    __dirname,
-    "..",
-    "templates",
-    "types",
-    template,
-    framework,
-  );
+  const templatePath = path.join(templatesDir, "types", template, framework);
   await copy("**", root, {
     parents: true,
     cwd: templatePath,
@@ -168,9 +162,7 @@ export const installPythonTemplate = async ({
   });
 
   if (engine === "context") {
-    const compPath = path.join(__dirname, "..", "templates", "components");
-
-    // Copy engine code
+    const compPath = path.join(templatesDir, "components");
     let vectorDbDirName = vectorDb ?? "none";
     const VectorDBPath = path.join(
       compPath,
diff --git a/packages/create-llama/helpers/typescript.ts b/packages/create-llama/helpers/typescript.ts
index 8b138c211..cfadc67b1 100644
--- a/packages/create-llama/helpers/typescript.ts
+++ b/packages/create-llama/helpers/typescript.ts
@@ -5,6 +5,7 @@ import { bold, cyan } from "picocolors";
 import { version } from "../../core/package.json";
 import { copy } from "../helpers/copy";
 import { callPackageManager } from "../helpers/install";
+import { templatesDir } from "./dir";
 import { PackageManager } from "./get-pkg-manager";
 import { InstallTemplateArgs } from "./types";
 
@@ -70,14 +71,7 @@ export const installTSTemplate = async ({
    * Copy the template files to the target directory.
    */
   console.log("\nInitializing project with template:", template, "\n");
-  const templatePath = path.join(
-    __dirname,
-    "..",
-    "templates",
-    "types",
-    template,
-    framework,
-  );
+  const templatePath = path.join(templatesDir, "types", template, framework);
   const copySource = ["**"];
   if (!eslint) copySource.push("!eslintrc.json");
 
@@ -111,7 +105,7 @@ export const installTSTemplate = async ({
    * Copy the selected chat engine files to the target directory and reference it.
    */
   let relativeEngineDestPath;
-  const compPath = path.join(__dirname, "..", "templates", "components");
+  const compPath = path.join(templatesDir, "components");
   if (engine && (framework === "express" || framework === "nextjs")) {
     console.log("\nUsing chat engine:", engine, "\n");
 
diff --git a/packages/create-llama/package.json b/packages/create-llama/package.json
index 55580c3c1..4f6d64132 100644
--- a/packages/create-llama/package.json
+++ b/packages/create-llama/package.json
@@ -17,7 +17,7 @@
     "create-llama": "./dist/index.js"
   },
   "files": [
-    "dist",
+    "dist/index.js",
     "./templates"
   ],
   "scripts": {
-- 
GitLab