@xtrn/cli 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.
- package/dist/cli.js +14 -14
- package/package.json +1 -1
- package/templates/index.ts.template +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{spawn as
|
|
3
|
-
export { default } from "${
|
|
4
|
-
`;
|
|
2
|
+
import{spawn as K}from"node:child_process";import*as M from"node:fs";import*as j from"node:path";import{spawn as Z}from"node:child_process";import*as u from"node:fs";import*as p from"node:os";import*as g from"node:path";function C(o){let n=o;while(!0){let s=g.join(n,"node_modules");if(u.existsSync(s))return s;let t=g.dirname(n);if(t===n)return null;n=t}}function F(o,n,s){let t=g.dirname(o),i=C(t);if(!i)console.warn("[xtrn] Warning: node_modules not found for symlink");let r=g.join(s,"node_modules");if(i&&!u.existsSync(r))u.symlinkSync(i,r,"junction");let m=`export { XTRNState } from "@xtrn/server";
|
|
3
|
+
export { default } from "${o}";
|
|
4
|
+
`;u.writeFileSync(g.join(s,"_cf_entry.ts"),m);let x=`name = "xtrn-dev"
|
|
5
5
|
main = "_cf_entry.ts"
|
|
6
6
|
compatibility_date = "2026-02-14"
|
|
7
7
|
compatibility_flags = ["nodejs_compat"]
|
|
@@ -17,9 +17,9 @@ class_name = "XTRNState"
|
|
|
17
17
|
[[migrations]]
|
|
18
18
|
tag = "v1"
|
|
19
19
|
new_sqlite_classes = ["XTRNState"]
|
|
20
|
-
`;
|
|
20
|
+
`;u.writeFileSync(g.join(s,"wrangler.toml"),x)}async function q(o,n){let s=g.dirname(o),t=g.basename(o),i=g.join(n,t);u.copyFileSync(o,i);let r=C(s);if(!r)console.warn("[xtrn] Warning: node_modules not found for symlink");let m=g.join(n,"node_modules");if(r&&!u.existsSync(m))u.symlinkSync(r,m,"junction");let x=`export { XTRNState } from "@xtrn/server";
|
|
21
21
|
export { default } from "${`./${t}`}";
|
|
22
|
-
`,a=g.join(n,"_cf_entry.ts");
|
|
22
|
+
`,a=g.join(n,"_cf_entry.ts");u.writeFileSync(a,x);let l=`name = "xtrn-dev"
|
|
23
23
|
main = "_cf_entry.ts"
|
|
24
24
|
compatibility_date = "2026-02-14"
|
|
25
25
|
compatibility_flags = ["nodejs_compat"]
|
|
@@ -31,15 +31,15 @@ class_name = "XTRNState"
|
|
|
31
31
|
[[migrations]]
|
|
32
32
|
tag = "v1"
|
|
33
33
|
new_sqlite_classes = ["XTRNState"]
|
|
34
|
-
`;return
|
|
35
|
-
${
|
|
36
|
-
`)){let
|
|
37
|
-
${e.dim}[xtrn dev] Shutting down...${e.reset}`),
|
|
38
|
-
`}await
|
|
39
|
-
`}async function
|
|
34
|
+
`;return u.writeFileSync(g.join(n,"wrangler.toml"),l),await G(n),g.join(n,"_cf_entry.js")}function G(o){return new Promise((n,s)=>{let t=Z("wrangler",["deploy","--dry-run","--outdir","."],{cwd:o,stdio:["ignore","pipe","pipe"]}),i="";t.stdout?.on("data",()=>{}),t.stderr?.on("data",(r)=>{i+=r.toString()}),t.on("error",(r)=>{s(Error(`Failed to spawn wrangler: ${r.message}`))}),t.on("close",(r)=>{if(r!==0){s(Error(`wrangler deploy --dry-run failed (exit ${r}):
|
|
35
|
+
${i}`));return}n()})})}function $(){return u.mkdtempSync(g.join(p.tmpdir(),"xtrn-dev-"))}function S(o){u.rmSync(o,{recursive:!0,force:!0})}import*as d from"node:fs";import{Miniflare as Sn}from"miniflare";function I(o){if(!d.existsSync(o))return{};let n=d.readFileSync(o,"utf-8"),s={};for(let t of n.split(`
|
|
36
|
+
`)){let i=t.trim();if(!i||i.startsWith("#"))continue;let r=i.indexOf("=");if(r===-1)continue;s[i.slice(0,r)]=i.slice(r+1)}return s}var e={reset:"\x1B[0m",bold:"\x1B[1m",dim:"\x1B[2m",cyan:"\x1B[36m",green:"\x1B[32m",yellow:"\x1B[33m",magenta:"\x1B[35m",white:"\x1B[37m",red:"\x1B[31m",gray:"\x1B[90m"};function V(o){let n="./index.ts",s=1234;for(let t of o)if(t.startsWith("--port="))s=Number.parseInt(t.slice(t.indexOf("=")+1),10);else if(!t.startsWith("--"))n=t;return{entryPoint:j.resolve(n),port:s}}async function P(o){for(let s=0;s<5;s++){try{let t=await fetch(`http://localhost:${o}/details`);if(t.ok)return await t.json()}catch{}await new Promise((t)=>setTimeout(t,200))}throw Error("Failed to fetch /details after server ready")}function k(o,n){let s=`http://localhost:${o}`;if(console.log(""),console.log(` ${e.bold}${e.cyan}${n.name}${e.reset} ${e.dim}${n.version}${e.reset}`),console.log(` ${e.green}➜${e.reset} ${e.bold}${s}${e.reset}`),console.log(""),n.tools.length>0){console.log(` ${e.bold}${e.white}Tools${e.reset}`);for(let t of n.tools){let i=t.tags.length>0?` ${e.dim}${t.tags.map((r)=>`[${r}]`).join(" ")}${e.reset}`:"";console.log(` ${e.cyan}${t.name}${e.reset}${i} ${e.gray}— ${t.description}${e.reset}`)}console.log("")}if(n.config.length>0){console.log(` ${e.bold}${e.white}Config${e.reset}`);for(let t of n.config)console.log(` ${e.yellow}${t.key}${e.reset} ${e.dim}${t.type}${e.reset}`);console.log("")}if(n.requiredEnv.length>0){console.log(` ${e.bold}${e.white}Required Env${e.reset}`);for(let t of n.requiredEnv)console.log(` ${e.yellow}${t}${e.reset}`);console.log("")}if(n.oauth)console.log(` ${e.bold}${e.white}OAuth${e.reset} ${e.magenta}${n.oauth.provider}${e.reset}`),console.log(` ${e.dim}authorize${e.reset} ${n.oauth.authorization_url}`),console.log(` ${e.dim}token${e.reset} ${n.oauth.token_url}`),console.log(` ${e.dim}callback${e.reset} ${n.oauth.callback_url}`),console.log(` ${e.dim}scopes${e.reset} ${n.oauth.scopes.join(", ")}`),console.log("")}function D(o,n){let s=j.join(o,"wrangler.toml"),t=K("npx",["wrangler","dev","--config",s,"--port",String(n)],{cwd:o,stdio:["ignore","pipe","pipe"]}),i="",r=new Promise((m,f)=>{let x=(l)=>{if(l.toString().includes("Ready on"))t.stdout?.off("data",x),m()},a=(l)=>{i+=l.toString()};t.stdout?.on("data",x),t.stderr?.on("data",a),t.on("error",(l)=>f(l)),t.on("close",(l)=>{if(l!==0&&l!==null)f(Error(i.trim()||`wrangler exited with code ${l}`))})});return r.then(()=>{i="",t.stderr?.on("data",(m)=>{let f=m.toString();if(f.includes("ERROR")||f.includes("Error")||f.includes("error:"))process.stderr.write(`${e.red}[xtrn dev]${e.reset} ${f}`)})}),{proc:t,ready:r}}async function W(o){let{entryPoint:n,port:s}=V(o),t=j.dirname(n);if(!M.existsSync(n))console.error(`${e.red}[xtrn dev]${e.reset} Entry point not found: ${n}`),process.exit(1);if(!M.existsSync(j.join(t,"package.json")))console.error(`${e.red}[xtrn dev]${e.reset} No package.json found in ${t}`),process.exit(1);let i=j.join(t,".dev.vars"),r=I(i),m=$();F(n,r,m);let f=null,x=()=>{if(console.log(`
|
|
37
|
+
${e.dim}[xtrn dev] Shutting down...${e.reset}`),f)f.kill(),f=null;S(m),process.exit(0)};process.on("SIGINT",x),process.on("SIGTERM",x);try{console.log(`${e.dim}[xtrn dev] Starting...${e.reset}`);let a=D(m,s);f=a.proc,f.on("error",(v)=>{console.error(`${e.red}[xtrn dev]${e.reset} Failed to start: ${v.message}`),S(m),process.exit(1)}),f.on("close",(v)=>{if(v!==0&&v!==null)console.error(`${e.red}[xtrn dev]${e.reset} Process exited with code ${v}`);S(m),process.exit(v??1)}),await a.ready;let l=await P(s);k(s,l),await new Promise(()=>{})}catch(a){if(f)f.kill();if(S(m),a instanceof Error)console.error(`${e.red}[xtrn dev]${e.reset} ${a.message}`);process.exit(1)}}import{mkdir as U,readFile as nn,writeFile as tn}from"node:fs/promises";import{dirname as sn,join as R}from"node:path";import{fileURLToPath as on}from"node:url";async function H(o){let n=o[0],s=o[1]??"v1.0.0";if(!n)console.error("Error: <name> argument is required"),console.error("Usage: xtrn new <name> [version]"),process.exit(1);let t=s.startsWith("v")?s:`v${s}`,i=t.replace(/^v/,"");if(!/^\d+\.\d+\.\d+$/.test(i))console.error(`Error: Invalid version "${s}". Expected semver (e.g., v1.0.0)`),process.exit(1);let r=on(import.meta.url),m=sn(r),f=R(m,"../templates"),x=R(process.cwd(),n);try{await U(x,{recursive:!1})}catch(a){if(a instanceof Error&&"code"in a&&a.code==="EEXIST")console.error(`Error: Directory "${n}" already exists`),process.exit(1);throw a}try{let a=[{src:"index.ts.template",dst:"index.ts"},{src:"package.json.template",dst:"package.json"},{src:"tsconfig.json.template",dst:"tsconfig.json"},{src:"README.md.template",dst:"README.md"}];for(let l of a){let v=R(f,l.src),E=R(x,l.dst),_=await nn(v,"utf-8");if(_=_.replace(/\{\{NAME\}\}/g,n),_=_.replace(/\{\{VERSION\}\}/g,t),_=_.replace(/\{\{VERSION_BARE\}\}/g,i),l.dst==="package.json"){let w=JSON.parse(_);w.dependencies["@xtrn/server"]="^1.0.0",w.devDependencies["@xtrn/cli"]="^1.0.0",w.scripts.submit="xtrn submit",_=`${JSON.stringify(w,null,"\t")}
|
|
38
|
+
`}await tn(E,_,"utf-8")}console.log(`✓ Created ${n}/`),console.log(""),console.log("Next steps:"),console.log(` cd ${n}`),console.log(" bun install"),console.log(" bun run dev")}catch(a){try{await U(x,{recursive:!0})}catch{}throw a}}import{spawn as en}from"node:child_process";import*as c from"node:fs";import{cp as J,mkdir as z,readFile as rn,readdir as fn,writeFile as mn}from"node:fs/promises";import*as X from"node:os";import*as y from"node:path";var h="xtrnai/servers",an=new Set(["node_modules","bun.lock",".dev.vars","dist","_cf_entry.ts","wrangler.toml",".wrangler",".git",".github"]);function b(o,n,s){return new Promise((t)=>{let i=en(o,n,{stdio:"pipe",cwd:s?.cwd}),r="",m="";i.stdout.on("data",(f)=>{r+=f.toString()}),i.stderr.on("data",(f)=>{m+=f.toString()}),i.on("close",(f)=>{t({stdout:r.trim(),stderr:m.trim(),code:f??1})}),i.on("error",()=>{t({stdout:"",stderr:`Failed to execute: ${o}`,code:1})})})}async function ln(){if((await b("gh",["--version"])).code!==0)console.error("[xtrn submit] GitHub CLI (gh) is not installed."),console.error(""),console.error("Install it:"),console.error(" macOS: brew install gh"),console.error(" Linux: https://github.com/cli/cli/blob/trunk/docs/install_linux.md"),console.error(" Windows: winget install --id GitHub.cli"),console.error(""),console.error("Then authenticate:"),console.error(" gh auth login"),process.exit(1);if((await b("gh",["auth","status"])).code!==0)console.error("[xtrn submit] GitHub CLI is not authenticated."),console.error(""),console.error("Run:"),console.error(" gh auth login"),process.exit(1)}function xn(o){if(!c.existsSync(y.join(o,"index.ts")))console.error("[xtrn submit] No index.ts found in current directory."),console.error("Run this command from your XTRN server project root."),process.exit(1);if(!c.existsSync(y.join(o,"package.json")))console.error("[xtrn submit] No package.json found in current directory."),console.error("Run this command from your XTRN server project root."),process.exit(1)}async function un(o){let n=y.join(o,"package.json"),s=JSON.parse(c.readFileSync(n,"utf-8")),t=s.version;if(typeof t!=="string"||!t)throw Error("package.json missing 'version' field");let i=s.name;if(!i)throw Error("package.json missing 'name' field");let r=i.replace(/^@xtrn\//,""),m=`-v${t}`,f=r.endsWith(m)?r.slice(0,-m.length):r;if(!f)throw Error(`Could not parse server name from package name: ${i}`);let x=y.resolve(o,"index.ts"),a=$();try{console.log("[xtrn submit] Validating bundle..."),await q(x,a),console.log("[xtrn submit] Bundle validated successfully.")}finally{S(a)}return console.log(`[xtrn submit] Detected: ${f} v${t}`),{name:f,version:t}}async function B(o,n){await z(n,{recursive:!0});let s=await fn(o,{withFileTypes:!0});for(let t of s){if(an.has(t.name))continue;let i=y.join(o,t.name),r=y.join(n,t.name);if(t.isDirectory())await B(i,r);else await J(i,r)}}function gn(o){let n=JSON.parse(o),s=(t)=>{if(!t||typeof t!=="object")return;let i={};for(let[r,m]of Object.entries(t))if(typeof m==="string"&&(m.startsWith("file:")||m.startsWith("link:")||m.startsWith("workspace:")))i[r]="^1.0.0";else i[r]=m;return i};if(n.dependencies)n.dependencies=s(n.dependencies);if(n.devDependencies)n.devDependencies=s(n.devDependencies);return`${JSON.stringify(n,null,"\t")}
|
|
39
|
+
`}async function cn(o,n){console.log("[xtrn submit] Preparing files..."),await B(o,n);let s=y.join(n,"package.json");if(c.existsSync(s)){let t=await rn(s,"utf-8"),i=gn(t);await mn(s,i,"utf-8"),console.log("[xtrn submit] Rewrote package.json (local deps → ^1.0.0)")}}async function yn(o,n,s){let t=c.mkdtempSync(y.join(X.tmpdir(),"xtrn-submit-"));try{console.log(`[xtrn submit] Forking ${h}...`);let i=await b("gh",["repo","fork",h,"--clone=true","--default-branch-only"],{cwd:t});if(i.code!==0&&!i.stderr.includes("already exists"))throw Error(`Fork failed: ${i.stderr}`);let m=c.readdirSync(t).find((O)=>c.statSync(y.join(t,O)).isDirectory()&&c.existsSync(y.join(t,O,".git")));if(!m)throw Error(`No cloned repo found in ${t}. Output: ${i.stdout} ${i.stderr}`);let f=y.join(t,m),x=`submit/${o}/v${n}`;console.log(`[xtrn submit] Creating branch ${x}...`);let a=await b("git",["checkout","-b",x],{cwd:f});if(a.code!==0)throw Error(`Branch creation failed: ${a.stderr}`);let l=y.join(f,"servers",o,`v${n}`);await z(l,{recursive:!0}),await J(s,l,{recursive:!0}),console.log("[xtrn submit] Committing...");let v=await b("git",["add","."],{cwd:f});if(v.code!==0)throw Error(`git add failed: ${v.stderr}`);let E=await b("git",["commit","-m",`Add ${o} v${n}`],{cwd:f});if(E.code!==0)throw Error(`git commit failed: ${E.stderr}`);console.log("[xtrn submit] Pushing...");let _=await b("git",["push","--force","origin",x],{cwd:f});if(_.code!==0)throw Error(`git push failed: ${_.stderr}`);let A=(await b("git",["remote","get-url","origin"],{cwd:f})).stdout.match(/[:/]([^/]+)\/[^/]+?(?:\.git)?$/),Y=A?`${A[1]}:${x}`:x;console.log("[xtrn submit] Creating pull request...");let T=await b("gh",["pr","create","--repo",h,"--head",Y,"--title",`Add ${o} v${n}`,"--body",`Automated submission via \`xtrn submit\` CLI.
|
|
40
40
|
|
|
41
|
-
**Server:** ${
|
|
42
|
-
**Version:** v${n}`],{cwd:
|
|
41
|
+
**Server:** ${o}
|
|
42
|
+
**Version:** v${n}`],{cwd:f});if(T.code!==0)throw Error(`PR creation failed: ${T.stderr}`);return T.stdout}finally{c.rmSync(t,{recursive:!0,force:!0})}}async function L(o){let n=process.cwd();await ln(),xn(n);let{name:s,version:t}=await un(n),i=c.mkdtempSync(y.join(X.tmpdir(),"xtrn-prepared-"));try{await cn(n,i);let r=await yn(s,t,i);console.log(""),console.log("✓ Pull request created!"),console.log(` ${r}`),console.log(""),console.log("Your server will be reviewed and deployed automatically once merged.")}finally{c.rmSync(i,{recursive:!0,force:!0})}}var Q="1.0.0",N=`
|
|
43
43
|
xtrn v${Q} — XTRN server development toolkit
|
|
44
44
|
|
|
45
45
|
Usage:
|
|
@@ -63,4 +63,4 @@ Examples:
|
|
|
63
63
|
xtrn dev ./server.ts Start dev server with custom entry
|
|
64
64
|
xtrn dev --port=3000 Start dev server on port 3000
|
|
65
65
|
xtrn submit Fork xtrn-servers and open PR
|
|
66
|
-
`.trim();async function
|
|
66
|
+
`.trim();async function vn(){let o=process.argv.slice(2),n=o[0];if(!n||n==="--help"||n==="-h")console.log(N),process.exit(0);if(n==="--version"||n==="-v")console.log(`xtrn v${Q}`),process.exit(0);if(n==="new"){let s=o.slice(1);if(s.includes("--help")||s.includes("-h"))console.log(N),process.exit(0);await H(s);return}if(n==="dev"){let s=o.slice(1);if(s.includes("--help")||s.includes("-h"))console.log(N),process.exit(0);await W(s);return}if(n==="submit"){let s=o.slice(1);if(s.includes("--help")||s.includes("-h"))console.log(N),process.exit(0);await L(s);return}console.error(`Unknown command: ${n}`),console.error('Run "xtrn --help" for usage.'),process.exit(1)}vn().catch((o)=>{console.error("[xtrn] Fatal:",o),process.exit(1)});
|
package/package.json
CHANGED