UNPKG

refraction-cli

Version:
202 lines (193 loc) 5.56 kB
#!/usr/bin/env node // src/index.ts import { program } from "commander"; import chalk from "chalk"; import { encode } from "@beskar-labs/gpt-encoder"; // package.json var package_default = { name: "refraction-cli", version: "1.0.7", description: "AI code generation for your terminal.", main: "dist/index.js", publishConfig: { access: "public", registry: "https://registry.npmjs.org/" }, bin: { refraction: "./dist/index.js" }, repository: { type: "git", url: "git+https://github.com/beskar-co/refraction-cli.git" }, keywords: [ "refraction", "ai", "terminal", "generation" ], bugs: { url: "https://github.com/beskar-co/refraction-cli/issues" }, homepage: "https://github.com/beskar-co/refraction-cli#readme", scripts: { build: "tsup", test: 'echo "Error: no test specified" && exit 1' }, type: "module", author: "Hayden Bleasel <hello@haydenbleasel.com>", license: "MIT", dependencies: { "@beskar-labs/gpt-encoder": "^1.0.7", axios: "^1.4.0", chalk: "^5.2.0", commander: "^10.0.1" }, devDependencies: { "@beskar-labs/harmony": "^2.2.8", eslint: "^8.41.0", jest: "^29.5.0", prettier: "^2.8.8", stylelint: "^15.6.2", tsup: "^6.7.0", typescript: "^5.0.4" }, prettier: "@beskar-labs/harmony/prettier", stylelint: { extends: "@beskar-labs/harmony/stylelint" } }; // src/lib/config.ts import os from "os"; import path from "path"; import fs from "fs"; var homeDir = process.env.XDG_CONFIG_HOME ? process.env.XDG_CONFIG_HOME : os.homedir(); var configFile = path.join(homeDir, "refraction-config.json"); if (!fs.existsSync(configFile)) { const newConfig = { userId: "", teamId: "" }; fs.writeFileSync(configFile, JSON.stringify(newConfig)); } var getConfig = (str) => { const config = fs.readFileSync(configFile, "utf-8"); const parsed = JSON.parse(config); return parsed[str]; }; var setConfig = (str, value) => { const config = fs.readFileSync(configFile, "utf-8"); const parsed = JSON.parse(config); parsed[str] = value; fs.writeFileSync(configFile, JSON.stringify(parsed)); }; // src/lib/request.ts import axios from "axios"; var base = "https://app.refraction.dev"; var request = async (path2, body, props) => { const endpoint = new URL(path2, base).href; const userId = getConfig("userId"); const teamId = getConfig("teamId"); const response = await axios.post(endpoint, body, { headers: { "Content-Type": "application/json", "X-Refraction-User": userId, "X-Refraction-Team": teamId, "X-Refraction-Source": "cli" }, ...props }); return response; }; var request_default = request; // src/lib/stream.ts var getStream = async (language, code) => { const response = await request_default( "/api/generate/create", { code, language }, { responseType: "stream" } ); return response.data; }; // src/lib/helpers.ts import { AxiosError } from "axios"; var parseError = (error) => { var _a, _b; if (error instanceof AxiosError) { return (_b = (_a = error.response) == null ? void 0 : _a.data.message) != null ? _b : error.message; } if (error instanceof Error) { return error.message; } return "An unknown error occurred."; }; // src/lib/authenticate.ts var authenticate = async () => { const userId = getConfig("userId"); const teamId = getConfig("teamId"); if (!userId) { throw new Error( "Your Refraction User ID or Team ID are incorrect. Please run `refraction login` to update them." ); } try { await request_default("/api/vscode/authenticate", { userId, teamId }); } catch (error) { const message = parseError(error); throw new Error(message); } }; var authenticate_default = authenticate; // src/index.ts var { log } = console; program.name("refraction").description(package_default.description).version(package_default.version); program.command("generate").description("Generate a Bash command using AI").argument("<command>", "command to generate").option( "--language <language>", "language to generate the command in", "shell" ).action(async (code, options) => { if (!code) { log(chalk.red("No code provided.")); return; } const tokens = encode(code).length; if (tokens > 3e3) { log( chalk.red( `Sorry, but the code you've selected is too long (${tokens} tokens). Please reduce the length of your code to 3000 tokens or less.` ) ); } try { await authenticate_default(); const data = await getStream(options.language, code); data.on("readable", () => { const chunk = data.read(); if (!chunk) { return; } const value = typeof chunk === "string" ? chunk : chunk.toString(); process.stdout.write(value); }); data.on("finish", () => { process.stdout.write("\n"); }); } catch (error) { const message = parseError(error); if (message === "Request failed with status code 401") { log( chalk.red( "Your Refraction User ID or Team ID are incorrect. Please run `refraction login` to update them." ) ); return; } log(chalk.red(message)); } }); program.command("login").description("Add Refraction User and Team IDs").argument("<userId>", "Your User ID").argument("[teamId]", "Your Team ID").action((userId, teamId) => { setConfig("userId", userId); setConfig("teamId", teamId); log(chalk.magenta("Successfully saved your Refraction details!")); }); program.parse();