@vercel/geistdocs 1.1.0 → 1.1.1

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.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import{program as j}from"commander";import{copyFile as Q,rm as Y}from"fs/promises";import{join as L}from"path";import{cancel as tt,intro as et,isCancel as st,log as nt,outro as ot,spinner as rt,text as at}from"@clack/prompts";import{exec as q}from"child_process";import{promisify as Z}from"util";var D="https://github.com/vercel/geistdocs";var u={stdio:"ignore"};var E="geistdocs-update",d=Z(q);var it="https://github.com/vercel/geistdocs.git",ct=async t=>{await d(`git clone --depth 1 --filter=blob:none --sparse ${it} ${t}`,u),await d("git sparse-checkout set apps/template",{...u,cwd:t}),await d("mv apps/template/* apps/template/.[!.]* . 2>/dev/null || true",{...u,cwd:t,shell:"/bin/bash"}),await d("rm -rf apps .git",{...u,cwd:t})},lt=async()=>{await Q(".env.example",".env.local")},pt=async()=>{await Y(L("content","docs"),{recursive:!0,force:!0})},gt=async()=>{await d("pnpm install",u)},dt=async()=>{await d("git init",u),await d("git add .",u),await d('git commit -m "\u2728 Initial commit"',u)},ft=async()=>{let t=await at({message:"What is your project named?",placeholder:"my-app",validate(e){if(e.length===0)return"Please enter a project name."}});return st(t)&&(tt("Operation cancelled."),process.exit(0)),t.toString()},R=async t=>{try{et("Let's start a Geistdocs project!");let e=process.cwd(),n=t.name||await ft(),o=rt(),s=L(e,n);o.start("Cloning template..."),await ct(n),o.message("Moving into project..."),process.chdir(s),o.message("Setting up environment variables..."),await lt(),o.message("Deleting demo content..."),await pt(),o.message("Installing dependencies..."),await gt(),t.disableGit||(o.message("Initializing Git repository..."),await dt()),o.stop("Project initialized successfully!"),ot("Run `pnpm dev` to start the development server.")}catch(e){let n=e instanceof Error?e.message:`Failed to initialize project: ${e}`;nt.error(n),process.exit(1)}};import{createHash as mt}from"crypto";import{existsSync as I}from"fs";import{readFile as A,writeFile as z}from"fs/promises";import{dirname as ut,parse as G,resolve as v}from"path";import{intro as ht,isCancel as wt,log as f,outro as x,spinner as yt,text as Tt}from"@clack/prompts";import{glob as $t}from"glob";import{Project as xt,SyntaxKind as C}from"ts-morph";var U=10,Pt="content/docs/**/*.mdx",N=".translation-cache.json",vt=/\.[a-z]{2}\.mdx$/,St=async t=>(await $t(t,{cwd:process.cwd(),absolute:!0,nodir:!0})).filter(n=>n.endsWith(".mdx")).filter(n=>!vt.test(n)),bt=t=>{let e=v(process.cwd(),t),s=new xt({skipAddingFilesFromTsConfig:!0}).addSourceFileAtPath(e).getVariableDeclaration("translations");if(!s)throw new Error("Could not find translations export in config file");let a=s.getInitializer();if(!a||a.getKind()!==C.ObjectLiteralExpression)throw new Error("translations must be an object literal");let i=a.asKind(C.ObjectLiteralExpression);if(!i)throw new Error("Could not parse translations object");let l=i.getProperties().filter(p=>p.getKind()===C.PropertyAssignment).map(p=>p.asKind(C.PropertyAssignment)?.getName()).filter(p=>p!==void 0);if(l.length===0)throw new Error("No locales found in translations object");let[y,...b]=l;return b},Ft=t=>mt("sha256").update(t).digest("hex"),Et=async()=>{let t=v(process.cwd(),N);if(!I(t))return{};try{let e=await A(t,"utf-8");return JSON.parse(e)}catch{return f.warn("Failed to load translation cache, starting fresh"),{}}},_=async t=>{let e=v(process.cwd(),N);await z(e,JSON.stringify(t,null,2),"utf-8")},Ct=(t,e,n,o)=>{let s=t[e];if(!s)return!0;let a=s[n];return a?a.hash!==o:!0},jt=async(t,e,n)=>{let o=await A(t,"utf-8"),s=await fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:o,locale:e})}),a=await s.text();if(!s.ok){let i=`Translation failed with status ${s.status}`;try{let l=JSON.parse(a);i=l.error||l.message||i}catch{i=a||i}throw new Error(i)}try{return JSON.parse(a).translation}catch{throw new Error(`Failed to parse response: ${a.slice(0,200)}`)}},Dt=async(t,e,n,o,s)=>{let a=ut(t),{name:i,ext:l}=G(t),y=v(a,`${i}.${e}${l}`);await z(y,n,"utf-8"),o[t]||(o[t]={}),o[t][e]={hash:s,translatedAt:new Date().toISOString()},await _(o)},M=async(t,e)=>{ht("Translate MDX content");let n=t??Pt,o=[],s=e.url||process.env.GEISTDOCS_TRANSLATE_URL||"https://geistdocs.com/translate",a=e.config??"geistdocs.tsx";if(a)try{o=bt(a),f.info(`Loaded ${o.length} locale(s) from config: ${o.join(", ")}`)}catch(r){f.error(`Failed to load config: ${r instanceof Error?r.message:String(r)}`),x("Translation cancelled"),process.exit(1)}let i=await St(n);if(i.length===0&&(f.error(`No MDX files found matching pattern: ${n}`),x("Translation cancelled"),process.exit(1)),f.info(`Found ${i.length} MDX file(s) matching pattern`),o.length===0){let r=await Tt({message:"Enter target locales (comma-separated):",placeholder:"cn,fr,es",validate:c=>{if(!c)return"At least one locale is required"}});wt(r)&&(x("Translation cancelled"),process.exit(0)),o=r.split(",").map(c=>c.trim())}let l=v(process.cwd(),N),y=await Et();I(l)||await _(y),f.info(`Using cache: ${l}`);let b=new Map;for(let r of i){let c=await A(r,"utf-8");b.set(r,Ft(c))}let p=[],T=0;for(let r of o)for(let c of i){let $=b.get(c);if(!$)throw new Error(`Hash not found for file: ${c}`);e.force||Ct(y,c,r,$)?p.push({filePath:c,locale:r,hash:$}):T+=1}if(p.length===0){f.success("All translations are up to date!"),x(`Skipped ${T} file(s) (unchanged)`);return}let w=[];for(let r=0;r<p.length;r+=U)w.push(p.slice(r,r+U));f.info(`Processing ${p.length} translation(s) in ${w.length} batch(es) of up to ${U}`),T>0&&f.info(`Skipped ${T} translation(s) (unchanged)`);let P=yt();try{let r=[];P.start("Starting translation...");let c=0;for(let h=0;h<w.length;h++){let F=w[h];P.message(`Processing batch ${h+1}/${w.length} (${F.length} translations) - ${c}/${p.length} complete`),await Promise.all(F.map(async g=>{let{name:V,ext:B}=G(g.filePath),W=await jt(g.filePath,g.locale,s);await Dt(g.filePath,g.locale,W,y,g.hash),r.push({name:V,locale:g.locale,ext:B}),c+=1})),h<w.length-1&&(P.message(`Batch ${h+1}/${w.length} complete. Pausing...`),await new Promise(g=>setTimeout(g,1e3)))}P.stop("Translation complete!");for(let{name:h,locale:F,ext:g}of r)f.success(`Saved: ${h}.${F}${g}`);let $=[`Translated ${r.length} file(s)`];T>0&&$.push(`Skipped ${T} unchanged`),x($.join(" \u2022 "))}catch(r){let c=r instanceof Error?r.message:String(r);P.stop(`Translation failed: ${c}`),x("Exiting"),process.exit(1)}};import{copyFile as Ut,mkdir as H,rm as J}from"fs/promises";import{dirname as At,join as S}from"path";import{intro as Nt,log as m,outro as k,spinner as Ot}from"@clack/prompts";import{glob as Lt}from"glob";var K="apps/template",Rt=["**/*"],It=["content/docs/**"],zt=async t=>{let e=process.cwd(),n=S(e,t);await J(n,{recursive:!0,force:!0}),await H(n,{recursive:!0})},Gt=async t=>await d(`git clone --depth 1 ${D} ${t}`),O=async t=>await J(t,{recursive:!0,force:!0}),_t=async(t,e,n)=>(await Lt(t,{cwd:n,nodir:!0,dot:!0,ignore:[".git/**",...e]})).sort(),Mt=async(t,e)=>{let n=process.cwd(),o=S(e,K,t),s=S(n,t);await H(At(s),{recursive:!0}),await Ut(o,s)},kt=async(t,e,n)=>{let o={success:[],failed:[]};for(let s of t)try{n.message(`Updating ${s}...`),await Mt(s,e),o.success.push(s)}catch(a){let i=a instanceof Error?a.message:`${a}`;o.failed.push({file:s,error:i})}return o},Ht=t=>{if(t.success.length>0){m.success(`Successfully updated ${t.success.length} files:`);for(let e of t.success)m.info(` \u2713 ${e}`)}if(t.failed.length>0){m.warn(`Failed to update ${t.failed.length} files:`);for(let{file:e,error:n}of t.failed)m.error(` \u2717 ${e}: ${n}`)}},X=async t=>{let e=process.cwd(),n=S(e,E),o=t&&t.length>0?t:Rt;try{Nt("Let's update your Geistdocs project!"),m.info(`Repository: ${D}`),t&&t.length>0&&m.info(`Updating specific files: ${t.join(", ")}`);let s=Ot();s.start("Creating temporary directory..."),await zt(E),s.message("Cloning repository..."),await Gt(E),s.message("Scanning for files...");let a=S(n,K),i=await _t(o,t&&t.length>0?[]:It,a);if(s.stop(`Found ${i.length} files to update`),i.length===0){m.warn("No files matched the patterns. Nothing to update."),await O(n),k("Update cancelled.");return}m.info(`Patterns: ${o.length}`),m.info(`Files to update: ${i.length}`),s.start("Updating files...");let l=await kt(i,n,s);s.message("Cleaning up..."),await O(n),s.stop("Update complete!"),Ht(l),k("Please review and test the changes carefully.")}catch(s){let a=s instanceof Error?s.message:`${s}`;try{await O(n)}catch{}m.error(`Failed to update project: ${a}`),process.exit(1)}};j.command("init").description("Initialize a new Geistdocs project").option("--name <name>","Name of the project").option("--disable-git","Disable git initialization").action(R);j.command("update [files...]").description("Update a Geistdocs project (optionally specify files/patterns to update)").action(X);j.command("translate [pattern]").description("Translate MDX file(s) to one or more locales (default: content/docs/**/*.mdx)").option("--config <path>","Path to config file to load locales from (defaults to geistdocs.tsx)").option("--url <url>","Custom translation API URL (defaults to GEISTDOCS_TRANSLATE_URL or https://geistdocs.com/translate)").option("--force","Force re-translation of all files, ignoring cache").action(M);j.parse(process.argv);
2
+ import{program as j}from"commander";import{copyFile as Y,rm as tt}from"fs/promises";import{join as R}from"path";import{cancel as et,intro as st,isCancel as nt,log as ot,outro as rt,spinner as at,text as it}from"@clack/prompts";import{exec as Z}from"child_process";import{promisify as Q}from"util";var D="https://github.com/vercel/geistdocs";var y={stdio:"ignore"};var E="geistdocs-update",m=Q(Z);var ct="https://github.com/vercel/geistdocs.git",lt=async t=>{await m(`git clone --depth 1 --filter=blob:none --sparse ${ct} ${t}`,y),await m("git sparse-checkout set apps/template",{...y,cwd:t}),await m("mv apps/template/* apps/template/.[!.]* . 2>/dev/null || true",{...y,cwd:t,shell:"/bin/bash"}),await m("rm -rf apps .git",{...y,cwd:t})},pt=async()=>{await Y(".env.example",".env.local")},gt=async()=>{await tt(R("content","docs"),{recursive:!0,force:!0})},dt=async()=>{await m("pnpm install",y)},ft=async()=>{await m("git init",y),await m("git add .",y),await m('git commit -m "\u2728 Initial commit"',y)},mt=async()=>{let t=await it({message:"What is your project named?",placeholder:"my-app",validate(e){if(e.length===0)return"Please enter a project name."}});return nt(t)&&(et("Operation cancelled."),process.exit(0)),t.toString()},I=async t=>{try{st("Let's start a Geistdocs project!");let e=process.cwd(),o=t.name||await mt(),n=at(),a=R(e,o);n.start("Cloning template..."),await lt(o),n.message("Moving into project..."),process.chdir(a),n.message("Setting up environment variables..."),await pt(),n.message("Deleting demo content..."),await gt(),n.message("Installing dependencies..."),await dt(),t.disableGit||(n.message("Initializing Git repository..."),await ft()),n.stop("Project initialized successfully!"),rt("Run `pnpm dev` to start the development server.")}catch(e){let o=e instanceof Error?e.message:`Failed to initialize project: ${e}`;ot.error(o),process.exit(1)}};import{createHash as ht}from"crypto";import{existsSync as z}from"fs";import{readFile as O,writeFile as G}from"fs/promises";import{dirname as ut,parse as _,resolve as v}from"path";import{intro as wt,isCancel as yt,log as h,outro as F,spinner as $t,text as Tt}from"@clack/prompts";import{glob as xt}from"glob";import{Project as Pt,SyntaxKind as C}from"ts-morph";var U=10,Ft="content/docs/**/*.mdx",A=".translation-cache.json",St=/\.[a-z]{2}\.mdx$/,vt=async t=>(await xt(t,{cwd:process.cwd(),absolute:!0,nodir:!0})).filter(o=>o.endsWith(".mdx")).filter(o=>!St.test(o)),bt=t=>{let e=v(process.cwd(),t),a=new Pt({skipAddingFilesFromTsConfig:!0}).addSourceFileAtPath(e).getVariableDeclaration("translations");if(!a)throw new Error("Could not find translations export in config file");let r=a.getInitializer();if(!r||r.getKind()!==C.ObjectLiteralExpression)throw new Error("translations must be an object literal");let s=r.asKind(C.ObjectLiteralExpression);if(!s)throw new Error("Could not parse translations object");let c=s.getProperties().filter(p=>p.getKind()===C.PropertyAssignment).map(p=>p.asKind(C.PropertyAssignment)?.getName()).filter(p=>p!==void 0);if(c.length===0)throw new Error("No locales found in translations object");let[d,...u]=c;return u},Et=t=>ht("sha256").update(t).digest("hex"),Ct=async()=>{let t=v(process.cwd(),A);if(!z(t))return{};try{let e=await O(t,"utf-8");return JSON.parse(e)}catch{return h.warn("Failed to load translation cache, starting fresh"),{}}},M=async t=>{let e=v(process.cwd(),A);await G(e,JSON.stringify(t,null,2),"utf-8")},jt=(t,e,o,n)=>{let a=t[e];if(!a)return!0;let r=a[o];return r?r.hash!==n:!0},Dt=async(t,e,o)=>{let n=await O(t,"utf-8"),a=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:n,locale:e})}),r=await a.text();if(!a.ok){let s=`Translation failed with status ${a.status}`;try{let c=JSON.parse(r);s=c.error||c.message||s}catch{s=r||s}throw new Error(s)}try{return JSON.parse(r).translation}catch{throw new Error(`Failed to parse response: ${r.slice(0,200)}`)}},Ut=async(t,e,o,n,a)=>{let r=ut(t),{name:s,ext:c}=_(t),d=v(r,`${s}.${e}${c}`);await G(d,o,"utf-8"),n[t]||(n[t]={}),n[t][e]={hash:a,translatedAt:new Date().toISOString()},await M(n)},k=async(t,e)=>{wt("Translate MDX content");let o=t??Ft,n=[],a=e.url||process.env.GEISTDOCS_TRANSLATE_URL||"https://geistdocs.com/translate",r=e.config??"geistdocs.tsx";if(r)try{n=bt(r),h.info(`Loaded ${n.length} locale(s) from config: ${n.join(", ")}`)}catch(i){h.error(`Failed to load config: ${i instanceof Error?i.message:String(i)}`),F("Translation cancelled"),process.exit(1)}let s=await vt(o);if(s.length===0&&(h.error(`No MDX files found matching pattern: ${o}`),F("Translation cancelled"),process.exit(1)),h.info(`Found ${s.length} MDX file(s) matching pattern`),n.length===0){let i=await Tt({message:"Enter target locales (comma-separated):",placeholder:"cn,fr,es",validate:l=>{if(!l)return"At least one locale is required"}});yt(i)&&(F("Translation cancelled"),process.exit(0)),n=i.split(",").map(l=>l.trim())}let c=v(process.cwd(),A),d=await Ct();z(c)||await M(d),h.info(`Using cache: ${c}`);let u=new Map;for(let i of s){let l=await O(i,"utf-8");u.set(i,Et(l))}let p=[],$=0;for(let i of n)for(let l of s){let P=u.get(l);if(!P)throw new Error(`Hash not found for file: ${l}`);e.force||jt(d,l,i,P)?p.push({filePath:l,locale:i,hash:P}):$+=1}if(p.length===0){h.success("All translations are up to date!"),F(`Skipped ${$} file(s) (unchanged)`);return}let w=[];for(let i=0;i<p.length;i+=U)w.push(p.slice(i,i+U));h.info(`Processing ${p.length} translation(s) in ${w.length} batch(es) of up to ${U}`),$>0&&h.info(`Skipped ${$} translation(s) (unchanged)`);let x=$t();try{let i=[];x.start("Starting translation...");let l=0;for(let T=0;T<w.length;T++){let b=w[T];x.message(`Processing batch ${T+1}/${w.length} (${b.length} translations) - ${l}/${p.length} complete`),await Promise.all(b.map(async f=>{let{name:B,ext:W}=_(f.filePath),q=await Dt(f.filePath,f.locale,a);await Ut(f.filePath,f.locale,q,d,f.hash),i.push({name:B,locale:f.locale,ext:W}),l+=1})),T<w.length-1&&(x.message(`Batch ${T+1}/${w.length} complete. Pausing...`),await new Promise(f=>setTimeout(f,1e3)))}x.stop("Translation complete!");for(let{name:T,locale:b,ext:f}of i)h.success(`Saved: ${T}.${b}${f}`);let P=[`Translated ${i.length} file(s)`];$>0&&P.push(`Skipped ${$} unchanged`),F(P.join(" \u2022 "))}catch(i){let l=i instanceof Error?i.message:String(i);x.stop(`Translation failed: ${l}`),F("Exiting"),process.exit(1)}};import{copyFile as Ot,mkdir as K,rm as L}from"fs/promises";import{dirname as At,join as S}from"path";import{intro as Nt,log as g,outro as H,spinner as Lt}from"@clack/prompts";import{glob as Rt}from"glob";var X="apps/template",It=["**/*"],zt=["content/docs/**"],Gt=async t=>{let e=process.cwd(),o=S(e,t);await L(o,{recursive:!0,force:!0}),await K(o,{recursive:!0})},_t=async t=>await m(`git clone --depth 1 ${D} ${t}`),N=async t=>await L(t,{recursive:!0,force:!0}),J=async(t,e,o)=>(await Rt(t,{cwd:o,nodir:!0,dot:!0,ignore:[".git/**",...e]})).sort(),Mt=async(t,e)=>{let o=process.cwd(),n=S(e,X,t),a=S(o,t);await K(At(a),{recursive:!0}),await Ot(n,a)},kt=async t=>{let e=process.cwd(),o=S(e,t);await L(o,{force:!0})},Ht=async(t,e,o,n)=>{let a={success:[],deleted:[],failed:[]};for(let r of t)try{n.message(`Updating ${r}...`),await Mt(r,o),a.success.push(r)}catch(s){let c=s instanceof Error?s.message:`${s}`;a.failed.push({file:r,error:c})}for(let r of e)try{n.message(`Deleting ${r}...`),await kt(r),a.deleted.push(r)}catch(s){let c=s instanceof Error?s.message:`${s}`;a.failed.push({file:r,error:c})}return a},Jt=t=>{if(t.success.length>0){g.success(`Successfully updated ${t.success.length} files:`);for(let e of t.success)g.info(` \u2713 ${e}`)}if(t.deleted.length>0){g.success(`Deleted ${t.deleted.length} files:`);for(let e of t.deleted)g.info(` \u{1F5D1} ${e}`)}if(t.failed.length>0){g.warn(`Failed to update ${t.failed.length} files:`);for(let{file:e,error:o}of t.failed)g.error(` \u2717 ${e}: ${o}`)}},V=async(t,e)=>{let o=process.cwd(),n=S(o,E),a=t&&t.length>0?t:It,r=t&&t.length>0?[]:zt;try{Nt("Let's update your Geistdocs project!"),g.info(`Repository: ${D}`),t&&t.length>0&&g.info(`Updating specific files: ${t.join(", ")}`),e?.sync&&g.info("Sync mode enabled: will delete files removed from template");let s=Lt();s.start("Creating temporary directory..."),await Gt(E),s.message("Cloning repository..."),await _t(E),s.message("Scanning for files...");let c=S(n,X),d=await J(a,r,c),u=[];if(e?.sync){s.message("Comparing with local files...");let $=await J(a,r,o),w=new Set(d);u=$.filter(x=>!w.has(x))}if(s.stop(`Found ${d.length} files to update`),d.length===0&&u.length===0){g.warn("No files matched the patterns. Nothing to update."),await N(n),H("Update cancelled.");return}g.info(`Patterns: ${a.length}`),g.info(`Files to update: ${d.length}`),e?.sync&&g.info(`Files to delete: ${u.length}`),s.start("Updating files...");let p=await Ht(d,u,n,s);s.message("Cleaning up..."),await N(n),s.stop("Update complete!"),Jt(p),H("Please review and test the changes carefully.")}catch(s){let c=s instanceof Error?s.message:`${s}`;try{await N(n)}catch{}g.error(`Failed to update project: ${c}`),process.exit(1)}};j.command("init").description("Initialize a new Geistdocs project").option("--name <name>","Name of the project").option("--disable-git","Disable git initialization").action(I);j.command("update [files...]").description("Update a Geistdocs project (optionally specify files/patterns to update)").option("--sync","Delete local files that no longer exist in the template").action(V);j.command("translate [pattern]").description("Translate MDX file(s) to one or more locales (default: content/docs/**/*.mdx)").option("--config <path>","Path to config file to load locales from (defaults to geistdocs.tsx)").option("--url <url>","Custom translation API URL (defaults to GEISTDOCS_TRANSLATE_URL or https://geistdocs.com/translate)").option("--force","Force re-translation of all files, ignoring cache").action(k);j.parse(process.argv);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vercel/geistdocs",
3
3
  "description": "CLI for Geistdocs projects, including initialization and updating.",
4
- "version": "1.1.0",
4
+ "version": "1.1.1",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "geistdocs": "dist/index.js"