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