sameweb-cli
Version:
A CLI tool to clone websites into local folders using Puppeteer and AI. Useful for developers, researchers, and learners who want to quickly grab a site's structure and assets.
110 lines (91 loc) ⢠2.9 kB
JavaScript
import readline from "readline";
import os from "os";
import fs from "fs";
import path from "path";
import { main } from "./cloner.js";
import dotenv from "dotenv";
dotenv.config();
// Path to state file in user home directory
export const STATE_FILE = path.join(os.homedir(), ".sameweb-state.json");
// Simple async prompt
function prompt(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) =>
rl.question(query, (ans) => {
rl.close();
resolve(ans.trim());
}),
);
}
// Load state if exists
export function loadState() {
if (fs.existsSync(STATE_FILE)) {
try {
return JSON.parse(fs.readFileSync(STATE_FILE, "utf-8"));
} catch (err) {
console.error("ā ļø Failed to read state file, resetting...");
}
}
return { cloned: false, url: null, apiKey: null };
}
// Save state
function saveState(state) {
fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2), "utf-8");
}
async function runCLI() {
console.log("š Welcome to sameweb-cli!");
let state = loadState();
// --- If no website cloned yet, force clone first ---
if (!state.cloned) {
console.log("š First time setup: You need to clone a website.");
let url = await prompt("š Enter website URL to clone: ");
while (!url) {
url = await prompt(
"ā URL is required. Please enter a valid website URL: ",
);
}
let apiKey = process.env.OPENAI_API_KEY ?? "";
if (!apiKey) {
apiKey= await prompt("š Enter your OpenAI API key(or create .env file(OPENAI_API_KEY) for direct import): ");
}
while (!apiKey) {
apiKey = await prompt(
"ā API key is required. Please enter your OpenAI API key: ",
);
}
console.log("ā” Cloning website, please wait...\n");
await main(url, apiKey, null);
// Save state
state = { cloned: true, url, apiKey };
saveState(state);
console.log(
`\nā
Website cloned successfully into '${url}'. Now you can ask queries to modify it.`,
);
} else {
console.log(`š Detected cloned website: ${state.url}`);
console.log("ā
You can directly ask queries to modify it.");
}
// --- Query loop ---
while (true) {
const input = await prompt(
"\nš¬ Enter a query (or type 'exit' / 'reset' ): ",
);
if (input.toLowerCase() === "exit") {
console.log("š Goodbye! Thanks for using sameweb-cli.");
break;
}
if (input.toLowerCase() === "reset") {
console.log(
"ā»ļø Resetting state... You will need to clone again next run.",
);
fs.unlinkSync(STATE_FILE);
break;
}
await main(null, state.apiKey, input);
}
}
runCLI();