Skip to content

Instantly share code, notes, and snippets.

@shibanovp
Last active February 13, 2024 16:46
Show Gist options
  • Save shibanovp/f9b9631c9f33250a663b3d7fc6be05df to your computer and use it in GitHub Desktop.
Save shibanovp/f9b9631c9f33250a663b3d7fc6be05df to your computer and use it in GitHub Desktop.
{
"name": "setup-chat",
"description": "the gist to setup Build your own AI-Powered chat app with Next.js and LangChain from blog.experienced.dev",
"version": "0.3.0",
"engines": {
"node": ">=18"
},
"bin": "./setup.js"
}
#!/usr/bin/env node
const fs = require("fs");
const https = require("https");
const path = require("path");
const url = require("url");
const { spawnSync } = require("child_process");
const dest = process.argv[2] || "chat";
if (fs.existsSync(dest)) {
throw new Error(`Destination directory "${dest}" is not empty.`);
}
spawnSync(
"npx",
[
"create-next-app@latest",
"--ts",
"--tailwind",
"--no-eslint",
"--no-src-dir",
"--import-alias",
"@/*",
"--experimental-app",
dest,
],
{
stdio: "inherit",
}
);
spawnSync(
"npm",
[
"install",
"--prefix",
dest,
"-S",
"langchain",
"debug",
"supports-color@^8.1.1",
],
{
stdio: "inherit",
}
);
const downloadGist = async ({ gistUrl, targetDir, targetFileName }) => {
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir);
}
const parsedUrl = url.parse(gistUrl);
const filePath = path.join(targetDir, targetFileName);
const requestOptions = {
hostname: parsedUrl.hostname,
path: parsedUrl.path,
method: "GET",
};
const requestPromise = (options) => {
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
if (res.statusCode !== 200) {
reject(
new Error(`Error downloading Gist: status code ${res.statusCode}`)
);
return;
}
const fileStream = fs.createWriteStream(filePath);
res.pipe(fileStream);
fileStream.on("finish", () => {
fileStream.close();
resolve(filePath);
});
});
req.on("error", (error) => {
reject(new Error(`Error downloading Gist: ${error.message}`));
});
req.end();
});
};
try {
const downloadedFilePath = await requestPromise(requestOptions);
console.log(`Gist downloaded to: ${downloadedFilePath}`);
} catch (error) {
console.error(error.message);
}
};
const FILES = [
{
gistUrl:
"https://gist.githubusercontent.com/shibanovp/2178a3e80d92d0ff2a9ce983b3c512ee/raw/e199fd42029ad734786f17082e94fc34a904798a/.env.local",
targetDir: path.join(dest),
targetFileName: ".env.local",
},
{
gistUrl:
"https://gist.githubusercontent.com/shibanovp/83d4781ce8c08b4538d3924930135805/raw/6f2d0bffe6c64ce1fb78c7a240a21df485752cbf/page.tsx",
targetDir: path.join(dest, "app"),
targetFileName: "page.tsx",
},
{
gistUrl:
"https://gist.githubusercontent.com/shibanovp/7a401e1d8d55a2df36322dcb626d4796/raw/b0c9bc22928bb8f49a71d8e85df587b35db45863/Chat.tsx",
targetDir: path.join(dest, "app", "components"),
targetFileName: "Chat.tsx",
},
{
gistUrl:
"https://gist.githubusercontent.com/shibanovp/6f3e62bc3e4941135a1d4f3c48bcc8af/raw/856e4bb11ce08c3f43ddeedd23b5cf2f0bc32d16/ChatMessage.tsx",
targetDir: path.join(dest, "app", "components"),
targetFileName: "ChatMessage.tsx",
},
{
gistUrl:
"https://gist.githubusercontent.com/shibanovp/2f208becec6ef2fc1166c73594092c20/raw/9ff22caad1ba1d50f2492528083024af77605b31/route.ts",
targetDir: path.join(dest, "app", "api", "chat"),
targetFileName: "route.ts",
},
];
for (const file of FILES) {
downloadGist(file);
}
@alexpchin
Copy link

Error: ENOENT: no such file or directory, mkdir 'chat/app/api/chat'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment