UNPKG

next-json-cms

Version:

Git-based JSON CMS for Next.js projects with real-time preview - supports both app and pages router

67 lines (66 loc) 2.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.startEditor = startEditor; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const child_process_1 = require("child_process"); async function isNextProject() { const packageJsonPath = path_1.default.join(process.cwd(), "package.json"); if (!fs_1.default.existsSync(packageJsonPath)) { return false; } try { const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, "utf-8")); return (!!packageJson.dependencies?.next || !!packageJson.devDependencies?.next); } catch { return false; } } async function runCommand(command, args, options = {}) { return new Promise((resolve, reject) => { const child = (0, child_process_1.spawn)(command, args, { stdio: "inherit", shell: true, env: options.env, }); child.on("close", (code) => { if (code === 0) { resolve(); } else { reject(new Error(`Command failed with code ${code}`)); } }); child.on("error", (error) => { reject(error); }); }); } async function startEditor(options) { const { port, host } = options; // Check if we're in a project directory if (!fs_1.default.existsSync(path_1.default.join(process.cwd(), "content"))) { throw new Error("No content directory found. Please run this command in a JSON CMS project directory."); } // Set up environment variables const env = { ...process.env, PORT: port.toString(), HOSTNAME: host, }; // Check if this is a Next.js project const isNext = await isNextProject(); if (isNext) { console.log("📦 Starting development server..."); console.log(`\nThe CMS will be available at: http://${host}:${port}/editor`); console.log(`The home page will be available at: http://${host}:${port}`); await runCommand("npm", ["run", "dev"], { env }); } else { throw new Error("This is not a Next.js project. Please run this command in a valid Next.js project directory."); } }