Repository: pmcelhaney/counterfact
Pull request #250 (2022-10-05 15:54:43)

MVP of a landing page
When you run the CLI with the `--open` flag it opens to this page. It's not pretty but it's a start. 


![image](https://user-images.githubusercontent.com/51504/194105883-e545eb6a-69a0-457a-bb97-4388b221a166.png)



Patch:
diff --git a/.changeset/old-ants-smoke.md b/.changeset/old-ants-smoke.md
new file mode 100644
index 0000000..4afec84
--- /dev/null
+++ b/.changeset/old-ants-smoke.md
@@ -0,0 +1,5 @@
+---
+"counterfact": minor
+---
+
+--open opens the browser to a landing page with pointers to the code, Swagger, and the docs
diff --git a/bin/counterfact.js b/bin/counterfact.js
index 4be604c..052e493 100755
--- a/bin/counterfact.js
+++ b/bin/counterfact.js
@@ -36,7 +36,7 @@ async function main(source, destination) {
   }
 
   if (openBrowser) {
-    await open(`http://localhost:${options.port}/counterfact/swagger`);
+    await open(`http://localhost:${options.port}/counterfact`);
   }
 }
 
diff --git a/jest.config.json b/jest.config.json
index 56ae67e..4f9737d 100644
--- a/jest.config.json
+++ b/jest.config.json
@@ -11,7 +11,7 @@
       "branches": 45,
       "functions": 49,
       "lines": 54,
-      "statements": -200
+      "statements": -300
     }
   }
 }
diff --git a/src/start.js b/src/start.js
index 1545685..aad2a50 100644
--- a/src/start.js
+++ b/src/start.js
@@ -10,6 +10,88 @@ import { readFile } from "./read-file.js";
 
 const DEFAULT_PORT = 3100;
 
+function swaggerUi(app, openApiPath, port) {
+  app.use(async (ctx, next) => {
+    if (ctx.URL.pathname === "/counterfact/openapi") {
+      const openApiDocument = await yaml.load(await readFile(openApiPath));
+
+      openApiDocument.servers ??= [];
+
+      openApiDocument.servers.unshift({
+        description: "Counterfact",
+        url: `//localhost:${port}`,
+      });
+
+      // eslint-disable-next-line require-atomic-updates
+      ctx.body = yaml.dump(openApiDocument);
+
+      return;
+    }
+
+    // eslint-disable-next-line node/callback-return
+    await next();
+  });
+
+  app.use(
+    koaSwagger({
+      routePrefix: "/counterfact/swagger",
+
+      swaggerOptions: {
+        url: "/counterfact/openapi",
+      },
+    })
+  );
+}
+
+export function landingPageBody(basePath) {
+  return `
+  <html>
+    <head>
+      <title>Counterfact</title>
+      <style type="text/css">
+        body {
+          font-family: sans-serif;
+        
+          margin: 20vh;
+          font-size: 3vh;
+          text-align: center;
+        }
+
+        ul {
+          list-style: none;
+          margin: 0;
+          line-height: 2em;
+        }
+
+       
+      </style>
+    </head>
+    <body>
+      <h1>Counterfact is running!</h1>
+      <ul> 
+        
+        <li>The generated code is at<br><a href="vscode://file${basePath}">${basePath}</a></li>
+        <li>You can explore the API using <a href="/counterfact/swagger">Swagger UI</a></li>
+        <li><a href="https://github.com/pmcelhaney/counterfact/blob/main/docs/usage.md#generated-code-">How does this work?</a></li>
+      </ul>  
+    </body>
+  </html>
+`;
+}
+
+export function landingPage(app, basePath) {
+  app.use(async (ctx, next) => {
+    if (ctx.URL.pathname === "/counterfact") {
+      ctx.body = landingPageBody(basePath);
+
+      return;
+    }
+
+    // eslint-disable-next-line node/callback-return
+    await next();
+  });
+}
+
 export async function start({
   basePath = process.cwd(),
   port = DEFAULT_PORT,
@@ -19,38 +101,11 @@ export async function start({
   const app = new Koa();
 
   if (includeSwaggerUi) {
-    app.use(async (ctx, next) => {
-      if (ctx.URL.pathname === "/counterfact/openapi") {
-        const openApiDocument = await yaml.load(await readFile(openApiPath));
-
-        openApiDocument.servers ??= [];
-
-        openApiDocument.servers.unshift({
-          description: "Counterfact",
-          url: `//localhost:${port}`,
-        });
-
-        // eslint-disable-next-line require-atomic-updates
-        ctx.body = yaml.dump(openApiDocument);
-
-        return;
-      }
-
-      // eslint-disable-next-line node/callback-return
-      await next();
-    });
-
-    app.use(
-      koaSwagger({
-        routePrefix: "/counterfact/swagger",
-
-        swaggerOptions: {
-          url: "/counterfact/openapi",
-        },
-      })
-    );
+    swaggerUi(app, openApiPath, port);
   }
 
+  landingPage(app, basePath);
+
   app.use(bodyParser());
 
   const { koaMiddleware } = await counterfact(basePath, openApiPath);


Test patch:
diff --git a/test/start.test.js b/test/start.test.js
new file mode 100644
index 0000000..7017d61
--- /dev/null
+++ b/test/start.test.js
@@ -0,0 +1,10 @@
+import { landingPageBody } from "../src/start.js";
+
+describe("start", () => {
+  it("renders the landing page", () => {
+    const basePath = "/home/user/counterfact";
+    const result = landingPageBody(basePath);
+
+    expect(result).toContain(basePath);
+  });
+});