previewcn 0.1.0-alpha.6 → 0.1.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +6 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {Command}from'commander';import
|
|
2
|
+
import {Command}from'commander';import l from'chalk';import y from'fs/promises';import a from'path';import h from'ora';import F from'prompts';import {spawn}from'child_process';import {detect}from'detect-package-manager';async function u(e){let t={isNextJs:false,isAppRouter:false};try{let n=a.join(e,"package.json"),r=await y.readFile(n,"utf-8"),c=JSON.parse(r);t.isNextJs=!!(c.dependencies?.next||c.devDependencies?.next);}catch{return t}let o=[a.join(e,"app"),a.join(e,"src","app")];for(let n of o)try{if((await y.stat(n)).isDirectory()){t.isAppRouter=!0;break}}catch{}return t}async function f(e){let t=[a.join(e,"app","layout.tsx"),a.join(e,"app","layout.ts"),a.join(e,"app","layout.jsx"),a.join(e,"app","layout.js"),a.join(e,"src","app","layout.tsx"),a.join(e,"src","app","layout.ts"),a.join(e,"src","app","layout.jsx"),a.join(e,"src","app","layout.js")];for(let o of t)try{return await y.access(o),o}catch{}return null}async function P(e){return await O(e,"@previewcn/devtools")}async function O(e,t){let o=await S(e);return o?!!(o.devDependencies?.[t]??o.dependencies?.[t]):false}async function S(e){try{let t=a.join(e,"package.json"),o=await y.readFile(t,"utf-8");return JSON.parse(o)}catch{return null}}var s={info:e=>console.log(l.blue("info"),e),success:e=>console.log(l.green("success"),e),warn:e=>console.log(l.yellow("warn"),e),error:e=>console.log(l.red("error"),e),hint:e=>console.log(l.gray("hint"),e)};var b=/^import[\s\S]*?;\s*$/gm,T='import "@previewcn/devtools/styles.css";',J='import { PreviewcnDevtools } from "@previewcn/devtools";',E='{process.env.NODE_ENV === "development" && <PreviewcnDevtools />}';function M(e,t){if(t.length===0)return e;let o=`
|
|
3
3
|
${t.join(`
|
|
4
|
-
`)}`,
|
|
4
|
+
`)}`,n=[...e.matchAll(b)];if(n.length===0)return `${t.join(`
|
|
5
5
|
`)}
|
|
6
6
|
|
|
7
|
-
${e}`;let r=
|
|
8
|
-
`+t+e.slice(
|
|
9
|
-
`);let e=[],t=await u(process.cwd());e.push({name:"Next.js App Router",pass:t.isNextJs&&t.isAppRouter,message:t.isNextJs?t.isAppRouter?"Detected":"App Router not found (using Pages Router?)":"Not a Next.js project"});let
|
|
10
|
-
`);let t=h("Detecting project type...").start(),
|
|
7
|
+
${e}`;let r=n[n.length-1],c=r.index+r[0].length;return e.slice(0,c)+o+e.slice(c)}function L(e,t){let o=e.match(/<body[^>]*>/);if(!o)return e;let n=o.index+o[0].length;return e.slice(0,n)+`
|
|
8
|
+
`+t+e.slice(n)}async function x(e){let t=await y.readFile(e,"utf-8"),o=[];t.includes("@previewcn/devtools/styles.css")||o.push(T),t.includes("PreviewcnDevtools")||o.push(J);let n=M(t,o);n.includes("<PreviewcnDevtools")||(n=L(n,E)),await y.writeFile(e,n,"utf-8");}async function D(e){try{return (await y.readFile(e,"utf-8")).includes("PreviewcnDevtools")}catch{return false}}async function j(){s.info(`Running PreviewCN diagnostics...
|
|
9
|
+
`);let e=[],t=await u(process.cwd());e.push({name:"Next.js App Router",pass:t.isNextJs&&t.isAppRouter,message:t.isNextJs?t.isAppRouter?"Detected":"App Router not found (using Pages Router?)":"Not a Next.js project"});let o=await P(process.cwd());e.push({name:"@previewcn/devtools package",pass:o,message:o?"Installed":"Not found (run `npx previewcn init`)"});let n=false,r=await f(process.cwd());r&&(n=await D(r)),e.push({name:"PreviewcnDevtools in layout",pass:n,message:n?"Found":"Not found in layout (run `npx previewcn init`)"}),console.log();for(let i of e){let d=i.pass?l.green("\u2713"):l.red("\u2717"),p=i.pass?l.green(i.message):l.red(i.message);console.log(` ${d} ${i.name}: ${p}`);}console.log(),e.every(i=>i.pass)?(s.success("All checks passed! PreviewCN devtools is ready to use."),s.info("Run your dev server and click the theme icon to open the editor.")):s.warn("Some checks failed. Run `npx previewcn init` to set up.");}function g(e){return l.cyan(e)}async function N(e,t){return !!(await F({type:"confirm",name:"value",message:e,initial:t})).value}async function k(e){return await detect({cwd:e})}async function A(e,t,o){let n=V(e,t);return new Promise((r,c)=>{let i=spawn(e,n,{cwd:o,stdio:"pipe"}),d="";i.stderr?.on("data",p=>{d+=p.toString();}),i.on("close",p=>{p===0?r():c(new Error(`Failed to install ${t}: ${d}`));}),i.on("error",p=>{c(p);});})}function V(e,t){return e==="pnpm"?["add","-D",t]:e==="yarn"?["add","-D",t]:e==="bun"?["add","-D",t]:["install","-D",t]}async function C(e){s.info(`Initializing PreviewCN...
|
|
10
|
+
`);let t=h("Detecting project type...").start(),o=await u(process.cwd());o.isNextJs||(t.fail("Not a Next.js project"),s.error("PreviewCN requires a Next.js project with App Router."),s.hint("Make sure you're in the root of a Next.js project."),process.exit(1)),o.isAppRouter||(t.fail("App Router not detected"),s.error("PreviewCN requires Next.js App Router (app/ directory)."),s.hint("Create an app/ directory or migrate from pages/ to app/."),process.exit(1)),t.succeed("Next.js App Router project detected");let n=await f(process.cwd());n||(s.error("Could not find app/layout.tsx"),process.exit(1)),s.info(`Found layout at: ${g(a.relative(process.cwd(),n))}`),e.yes||await N("This will install @previewcn/devtools and modify your app layout. Continue?",true)||(s.info("Aborted."),process.exit(0)),await Y(n),console.log(),s.success("PreviewCN devtools initialized successfully!"),console.log(),s.info("Next step:"),console.log(` Run ${g("pnpm dev")} (or your dev command) to start the dev server.`),console.log(` Click the ${g("theme palette icon")} in the bottom-right corner to open the editor.`),console.log();}async function Y(e){let t=process.cwd(),o=h("Installing @previewcn/devtools...").start();try{let r=await k(t);await A(r,"@previewcn/devtools",t),o.succeed("Installed @previewcn/devtools");}catch(r){o.fail("Failed to install @previewcn/devtools"),s.error(r instanceof Error?r.message:String(r)),s.hint("You can install it manually: pnpm add -D @previewcn/devtools");}let n=h("Adding PreviewcnDevtools to layout...").start();try{await x(e),n.succeed("Added PreviewcnDevtools to layout");}catch(r){n.fail("Failed to modify layout for devtools"),s.error(r instanceof Error?r.message:String(r)),s.hint("You may need to add PreviewcnDevtools manually. See documentation.");}}var v=new Command;v.name("previewcn").description("CLI for PreviewCN - real-time shadcn/ui theme editor").version("0.1.0");v.command("init",{isDefault:true}).description("Initialize PreviewCN devtools in your Next.js project").option("-y, --yes","Skip confirmation prompts").action(C);v.command("doctor").description("Check PreviewCN setup and diagnose issues").action(j);v.parse();
|