UNPKG

@hirall/cli

Version:

Command-line interface for Hirall

235 lines (222 loc) 8.93 kB
#!/usr/bin/env node var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); // src/index.ts var import_commander = require("commander"); var import_chalk = __toESM(require("chalk")); var import_inquirer = __toESM(require("inquirer")); var import_ora = __toESM(require("ora")); var fs = __toESM(require("fs")); var path = __toESM(require("path")); var import_axios = __toESM(require("axios")); var program = new import_commander.Command(); program.name("hirall").description("Hirall CLI - Backend-as-a-Service").version("1.0.0"); program.command("init").description("Initialize a new Hirall project").option("-n, --name <name>", "Project name").action(async (options) => { console.log(import_chalk.default.blue("\n\u{1F680} Welcome to Hirall!\n")); const answers = await import_inquirer.default.prompt([ { type: "input", name: "name", message: "Project name:", default: options.name || "my-hirall-app" }, { type: "list", name: "template", message: "Choose a template:", choices: [ { name: "Next.js", value: "nextjs" }, { name: "React", value: "react" }, { name: "Vue", value: "vue" }, { name: "Node.js API", value: "nodejs" }, { name: "Empty", value: "empty" } ] }, { type: "confirm", name: "typescript", message: "Use TypeScript?", default: true } ]); const spinner = (0, import_ora.default)("Creating project...").start(); try { const projectPath = path.join(process.cwd(), answers.name); fs.mkdirSync(projectPath, { recursive: true }); const packageJson = { name: answers.name, version: "1.0.0", private: true, scripts: { dev: "next dev", build: "next build", start: "next start" }, dependencies: { "@hirall/client": "^1.0.0", "@hirall/react": "^1.0.0" } }; fs.writeFileSync( path.join(projectPath, "package.json"), JSON.stringify(packageJson, null, 2) ); const envContent = `# Hirall Configuration NEXT_PUBLIC_HIRALL_URL=https://your-project.hirall.io NEXT_PUBLIC_HIRALL_ANON_KEY=your-anon-key HIRALL_SERVICE_KEY=your-service-key `; fs.writeFileSync(path.join(projectPath, ".env.local"), envContent); const exampleCode = answers.typescript ? `import { createClient } from '@hirall/client'; const hirall = createClient( process.env.NEXT_PUBLIC_HIRALL_URL!, process.env.NEXT_PUBLIC_HIRALL_ANON_KEY! ); export default hirall; ` : `const { createClient } = require('@hirall/client'); const hirall = createClient( process.env.NEXT_PUBLIC_HIRALL_URL, process.env.NEXT_PUBLIC_HIRALL_ANON_KEY ); module.exports = hirall; `; const libDir = path.join(projectPath, "lib"); fs.mkdirSync(libDir, { recursive: true }); fs.writeFileSync( path.join(libDir, `hirall.${answers.typescript ? "ts" : "js"}`), exampleCode ); const readme = `# ${answers.name} A Hirall project created with @hirall/cli ## Getting Started 1. Install dependencies: \`\`\`bash npm install \`\`\` 2. Update \`.env.local\` with your Hirall project credentials 3. Run the development server: \`\`\`bash npm run dev \`\`\` ## Learn More - [Hirall Documentation](https://docs.hirall.io) - [API Reference](https://docs.hirall.io/api) `; fs.writeFileSync(path.join(projectPath, "README.md"), readme); spinner.succeed(import_chalk.default.green("Project created successfully!")); console.log(import_chalk.default.gray("\nNext steps:")); console.log(import_chalk.default.gray(` cd ${answers.name}`)); console.log(import_chalk.default.gray(" npm install")); console.log(import_chalk.default.gray(" npm run dev\n")); } catch (error) { spinner.fail(import_chalk.default.red("Failed to create project")); console.error(error.message); process.exit(1); } }); program.command("login").description("Login to your Hirall account").action(async () => { var _a, _b, _c; console.log(import_chalk.default.blue("\n\u{1F510} Login to Hirall\n")); const answers = await import_inquirer.default.prompt([ { type: "input", name: "email", message: "Email:", validate: (input) => input.includes("@") || "Please enter a valid email" }, { type: "password", name: "password", message: "Password:", mask: "*" } ]); const spinner = (0, import_ora.default)("Authenticating...").start(); try { const response = await import_axios.default.post("https://api.hirall.io/v1/auth/signin", { email: answers.email, password: answers.password }); const { access_token } = response.data.data; const configDir = path.join(process.env.HOME || process.env.USERPROFILE || "", ".hirall"); fs.mkdirSync(configDir, { recursive: true }); fs.writeFileSync(path.join(configDir, "config.json"), JSON.stringify({ token: access_token }, null, 2)); spinner.succeed(import_chalk.default.green("Logged in successfully!")); } catch (error) { spinner.fail(import_chalk.default.red("Login failed")); console.error(((_c = (_b = (_a = error.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error) == null ? void 0 : _c.message) || error.message); process.exit(1); } }); program.command("projects").description("List your Hirall projects").action(async () => { var _a, _b, _c; const spinner = (0, import_ora.default)("Fetching projects...").start(); try { const configPath = path.join(process.env.HOME || process.env.USERPROFILE || "", ".hirall", "config.json"); const config = JSON.parse(fs.readFileSync(configPath, "utf-8")); const response = await import_axios.default.get("https://api.hirall.io/v1/projects", { headers: { Authorization: `Bearer ${config.token}` } }); spinner.succeed(import_chalk.default.green("Projects:")); response.data.data.forEach((project) => { console.log(import_chalk.default.gray(` ${project.name}`)); console.log(import_chalk.default.gray(` ID: ${project.id}`)); console.log(import_chalk.default.gray(` URL: ${project.url}`)); }); } catch (error) { spinner.fail(import_chalk.default.red("Failed to fetch projects")); console.error(((_c = (_b = (_a = error.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error) == null ? void 0 : _c.message) || error.message); process.exit(1); } }); var dbCommand = program.command("db").description("Database commands"); dbCommand.command("push").description("Push local schema to database").action(async () => { const spinner = (0, import_ora.default)("Pushing schema...").start(); spinner.succeed(import_chalk.default.green("Schema pushed!")); }); dbCommand.command("pull").description("Pull schema from database").action(async () => { const spinner = (0, import_ora.default)("Pulling schema...").start(); spinner.succeed(import_chalk.default.green("Schema pulled!")); }); dbCommand.command("reset").description("Reset database").action(async () => { const answers = await import_inquirer.default.prompt([ { type: "confirm", name: "confirm", message: import_chalk.default.yellow("\u26A0\uFE0F This will delete all data. Are you sure?"), default: false } ]); if (!answers.confirm) { console.log(import_chalk.default.gray("Cancelled")); return; } const spinner = (0, import_ora.default)("Resetting database...").start(); spinner.succeed(import_chalk.default.green("Database reset!")); }); program.command("generate").description("Generate TypeScript types from database schema").action(async () => { const spinner = (0, import_ora.default)("Generating types...").start(); spinner.succeed(import_chalk.default.green("Types generated!")); }); program.parse(process.argv);