@hupu-tech/shaper 0.2.4 → 0.2.6

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.
@@ -1,15 +0,0 @@
1
- import{homedir as Y}from"node:os";import{join as O,resolve as Z,dirname as tt}from"node:path";import{mkdir as et,readFile as nt,rename as rt,writeFile as it,lstat as at,rm as ot}from"node:fs/promises";import{randomUUID as st}from"node:crypto";import{z as T}from"zod";var a=class extends Error{constructor(r,e,i){super(e);this.code=r;this.details=i}},ct=T.object({schemaVersion:T.literal(1),server:T.string().url()}).strict();function V(){return Z(process.env.SHAPER_CONFIG_HOME??O(Y(),".config","shaper"))}function _(t){let n=new URL(t);if(n.username||n.password||n.search||n.hash||n.pathname!=="/"||n.protocol!=="https:"&&!(n.protocol==="http:"&&["127.0.0.1","localhost","[::1]"].includes(n.hostname)))throw new a("INVALID_SERVER","\u670D\u52A1\u5730\u5740\u5FC5\u987B\u662F HTTPS origin\uFF1B\u672C\u673A\u5F00\u53D1\u5141\u8BB8\u56DE\u73AF HTTP\u3002");return n.origin}async function N(t,n){await et(tt(t),{recursive:!0,mode:448});try{if((await at(t)).isSymbolicLink())throw new a("UNSAFE_FILE",`\u914D\u7F6E\u4E0D\u80FD\u662F\u7B26\u53F7\u94FE\u63A5\uFF1A${t}`)}catch(e){if(e.code!=="ENOENT")throw e}let r=`${t}.${st()}.tmp`;try{await it(r,JSON.stringify(n,null,2)+`
2
- `,{mode:384,flag:"wx"}),await rt(r,t)}finally{await ot(r,{force:!0})}}async function S(t){return JSON.parse(await nt(t,"utf8"))}async function At(){if(process.env.SHAPER_SERVER)return _(process.env.SHAPER_SERVER);try{return _(ct.parse(await S(O(V(),"config.json"))).server)}catch(t){if(t.code==="ENOENT")return"https://shaper.hupu.com";throw new a("INVALID_CONFIG","\u7528\u6237\u914D\u7F6E\u65E0\u6CD5\u8BFB\u53D6\uFF0C\u8BF7\u4F7F\u7528 shaper config --server \u91CD\u65B0\u8BBE\u7F6E\u670D\u52A1\u5730\u5740\u3002")}}async function Rt(t){await N(O(V(),"config.json"),{schemaVersion:1,server:_(t)})}function bt(t){return{code:t instanceof a?t.code:"OPERATION_FAILED",message:t instanceof Error?t.message:"\u64CD\u4F5C\u672A\u5B8C\u6210",...t instanceof a&&t.details!==void 0?{details:t.details}:{}}}import{realpath as k,mkdir as z,readdir as P,readFile as I,writeFile as v,lstat as G,cp as pt,appendFile as dt}from"node:fs/promises";import{resolve as x,join as p,relative as C,sep as L,dirname as lt,basename as ut,isAbsolute as H}from"node:path";import{fileURLToPath as ft}from"node:url";import{randomUUID as K,createHash as M}from"node:crypto";import{spawn as wt}from"node:child_process";import ht from"adm-zip";import mt from"open";import{z as g}from"zod";import W from"proper-lockfile";var $=x(lt(ft(import.meta.url)),".."),gt=g.object({schemaVersion:g.literal(1),devCommand:g.string().min(1).max(500),buildCommand:g.string().min(1).max(500),outputDir:g.string().min(1),devUrl:g.string().default("http://127.0.0.1:{port}")}).passthrough(),Et=g.object({schemaVersion:g.literal(1),appId:g.string().min(1).max(200),server:g.string().url()}).strict();async function j(t){if(!t||!H(t))throw new a("WORKSPACE_REQUIRED","\u8BF7\u6307\u5B9A\u5DE5\u7A0B\u7684\u7EDD\u5BF9\u8DEF\u5F84\u3002");return k(x(t))}async function A(t){let n=p(t,".shaper");if(await z(n,{recursive:!0,mode:448}),(await G(n)).isSymbolicLink())throw new a("UNSAFE_WORKSPACE",".shaper \u4E0D\u80FD\u662F\u7B26\u53F7\u94FE\u63A5\u3002");return n}async function R(t){return gt.parse(await S(p(t,"shaper.config.json")))}async function U(t,n){let r=Et.parse(await S(p(t,".shaper","project.json")));if(r.server!==n)throw new a("SERVER_MISMATCH","\u6B64\u5DE5\u7A0B\u7ED1\u5B9A\u7684 Shaper \u670D\u52A1\u4E0E\u5F53\u524D\u767B\u5F55\u670D\u52A1\u4E0D\u540C\u3002\u8BF7\u663E\u5F0F\u5207\u6362\u670D\u52A1\u6216\u91CD\u65B0\u5173\u8054\u3002");return r}async function q(t,n,r){let e=await A(t);await N(p(e,"project.json"),{schemaVersion:1,appId:n,server:r})}async function Vt(t,n,r={}){if(!n||!H(n))throw new a("WORKSPACE_REQUIRED","\u8BF7\u6307\u5B9A\u5DE5\u7A0B\u7684\u7EDD\u5BF9\u8DEF\u5F84\u3002");await z(n,{recursive:!0});let e=await j(n),i=await A(e),s=await W.lock(i,{retries:0});try{let c;try{c=await U(e,t.auth.server)}catch(o){if(o.code!=="ENOENT")throw o}if(c&&r.appId&&r.appId!==c.appId)throw new a("ALREADY_LINKED",`\u5F53\u524D\u76EE\u5F55\u5C5E\u4E8E ${c.appId}\uFF0C\u4E0E\u63D0\u793A\u8BCD\u4E2D\u7684 ${r.appId} \u4E0D\u4E00\u81F4\u3002\u8BF7\u56DE\u5230\u539F\u5E94\u7528\u7EE7\u7EED\uFF0C\u6216\u6362\u4E00\u4E2A\u7A7A\u76EE\u5F55\uFF1Binit \u4E0D\u4F1A\u8986\u76D6\u7ED1\u5B9A\u3002`);let f=(await P(e)).filter(o=>![".git",".shaper",".shaper.lock",".DS_Store"].includes(o));if(!c&&f.length&&r.template)throw new a("EXISTING_PROJECT","\u6A21\u677F\u53EA\u7528\u4E8E\u7A7A\u76EE\u5F55\uFF1B\u8BF7\u4FDD\u7559\u5DF2\u6709\u9879\u76EE\u7684\u6784\u5EFA\u65B9\u5F0F\u3002");if(r.template&&!["h5","react","phaser"].includes(r.template))throw new a("INVALID_TEMPLATE","\u652F\u6301 h5\u3001react\u3001phaser \u6A21\u677F\u3002");let l=new Set;for(let o of["shaper.config.json","shaper.project.json",".shaper/config.json"]){let E=await S(p(e,o)).catch(m=>{if(m.code!=="ENOENT")throw m});for(let m of[E?.appId,E?.applicationId])typeof m=="string"&&m.startsWith("app_")&&l.add(m)}let d=c?.appId??r.appId??[...l][0];if(l.size>1||d&&[...l].some(o=>o!==d))throw new a("APP_ID_CONFLICT","\u539F\u9879\u76EE\u5E94\u7528 ID \u4E0E\u76EE\u6807\u4E0D\u4E00\u81F4\uFF0C\u8BF7\u6838\u5BF9\u65E7\u9879\u76EE\u914D\u7F6E\uFF1B\u4E0D\u4F1A\u521B\u5EFA\u6216\u5207\u6362\u5E94\u7528\u3002");let u=p(i,"init.json"),h=c?void 0:await S(u).catch(o=>{if(o.code!=="ENOENT")throw o});if(!c&&h&&(!h.appId||h.server!==t.auth.server||d&&d!==h.appId))throw new a("INIT_CONFLICT","\u5B58\u5728\u672A\u5B8C\u6210\u7684\u521D\u59CB\u5316\uFF0C\u8BF7\u6838\u5BF9 .shaper/init.json \u4E2D\u7684\u539F\u5E94\u7528\u548C\u670D\u52A1\uFF0C\u4E0D\u8981\u53E6\u5EFA\u5E94\u7528\u3002");if(!d&&!h&&f.length)throw new a("APP_REQUIRED","\u5DF2\u6709\u9879\u76EE\u5C1A\u672A\u8BC6\u522B\u5230\u5E94\u7528 ID\u3002\u8BF7\u5148\u786E\u8BA4\u539F\u5E94\u7528\uFF0C\u518D\u4F7F\u7528 init --app\uFF1B\u65B0\u4F5C\u54C1\u8BF7\u4ECE\u7F51\u9875\u5F00\u59CB\u521B\u4F5C\u5E76\u643A\u5E26\u9884\u5206\u914D ID\u3002");let w=d??h?.appId??`app_${K().replaceAll("-","").slice(0,10)}`;!h&&!c&&(h={appId:w,server:t.auth.server},await N(u,h));let b;try{b=await t.project(w)}catch(o){if(!(o instanceof a)||!["AUTH_REQUIRED","INVALID_TOKEN","PROJECT_NOT_AUTHORIZED","INSUFFICIENT_SCOPE","NOT_FOUND"].includes(o.code))throw o;let E=await t.auth.start({appId:w,...r.name?{applicationName:r.name}:{},initialize:!c&&l.size===0,reauthorize:!0});if(E.status==="pending"){if(!r.waitForAuthorization)return{root:e,appId:w,...E,status:"awaiting_confirmation",next:"\u5728\u6D4F\u89C8\u5668\u5B8C\u6210\u767B\u5F55\u548C\u521B\u5EFA/\u5173\u8054\u786E\u8BA4\uFF0C\u6388\u6743\u5B8C\u6210\u540E\u4F7F\u7528\u540C\u4E00\u76EE\u5F55\u548C\u53C2\u6570\u91CD\u8BD5 init\u3002"};process.stderr.write(`\u8BF7\u5728\u6D4F\u89C8\u5668\u786E\u8BA4\u521B\u5EFA\u6216\u5173\u8054\u5E94\u7528\uFF1A${E.authorizationUrl}
3
- `),await t.auth.wait(E.flowId)}b=await t.project(w)}let Q=b.name;if(c)return r.installDependencies!==!1&&await I(p(i,"install-pending"),"utf8").catch(E=>{if(E.code==="ENOENT")return"";throw E})&&(await D("npm ci",e),await v(p(i,"install-pending"),"")),{root:e,...c,alreadyInitialized:!0};if(f.length===0){let o=r.template??"h5";if(!["h5","react","phaser"].includes(o))throw new a("INVALID_TEMPLATE","\u652F\u6301 h5\u3001react\u3001phaser \u6A21\u677F\u3002");let E=p($,"templates",o);for(let m of(await P(E)).filter(y=>!["node_modules","dist"].includes(y)))await pt(p(E,m),p(e,m),{recursive:!0,force:!1,errorOnExist:!0});r.installDependencies!==!1&&await v(p(i,"install-pending"),`npm ci
4
- `)}else{if(r.template)throw new a("EXISTING_PROJECT","\u6A21\u677F\u53EA\u7528\u4E8E\u7A7A\u76EE\u5F55\uFF1B\u5DF2\u6709\u5DE5\u7A0B\u76F4\u63A5 init\uFF0C\u4FDD\u7559\u6280\u672F\u6808\u548C\u6E90\u7801\u3002");try{await R(e)}catch(o){if(o.code!=="ENOENT")throw o;await N(p(e,"shaper.config.json"),{schemaVersion:1,devCommand:"npm run dev -- --host 127.0.0.1 --port {port} --strictPort",buildCommand:"npm run build",outputDir:"dist"})}}try{await I(p(e,"package.json"))}catch(o){if(o.code!=="ENOENT")throw o;if(await I(p(e,"index.html")).catch(m=>{if(m.code!=="ENOENT")throw m})!==void 0){for(let m of["package.json","package-lock.json","vite.config.js"])try{let y=await I(p($,"templates/h5",m));if(m==="package.json"){let J=JSON.parse(y.toString("utf8"));J.scripts.check="vite build",y=Buffer.from(JSON.stringify(J,null,2)+`
5
- `)}await v(p(e,m),y,{flag:"wx"})}catch(y){if(y.code!=="EEXIST")throw y}r.installDependencies!==!1&&await v(p(i,"install-pending"),`npm ci
6
- `)}}await B(e),await q(e,w,t.auth.server);let X=await I(p(i,"install-pending"),"utf8").catch(o=>{if(o.code==="ENOENT")return"";throw o});return r.installDependencies!==!1&&X&&(await D("npm ci",e),await v(p(i,"install-pending"),"")),{root:e,name:Q,appId:w}}finally{await s()}}async function $t(t,n,r){let e=await j(n),i=await W.lock(await A(e),{retries:0});try{let s=await t.project(r);return await q(e,s.appId,t.auth.server),await B(e),{root:e,appId:s.appId,name:s.name}}finally{await i()}}async function B(t){let n=p(t,".gitignore"),r=await I(n,"utf8").catch(u=>{if(u.code==="ENOENT")return"";throw u}),i=["/.shaper/","/.shaper.lock/","/node_modules/","/dist/",".env",".env.*","!.env.example"].filter(u=>!r.split(`
7
- `).includes(u));i.length&&await dt(n,`
8
- # Shaper local state and generated files
9
- ${i.join(`
10
- `)}
11
- `);let s=p(t,"AGENTS.md"),c=await I(s,"utf8").catch(u=>{if(u.code==="ENOENT")return"";throw u}),f="<!-- shaper:project -->",l=`
12
- ${f}
13
- Read docs/product.md for goals and rules, docs/design.md for experience and implementation decisions, and docs/validation.md for actual evidence when these files exist. Use the globally installed shaper-development skill for local development and shaper-release for web delivery. For Hupu login/share use hupu-business; for functions or databases use shaper-backend. Pass this project's absolute path to project tools. Read shaper.config.json for commands and query shaper_status for live identity. Web publishing belongs to Shaper; cloud functions are deployed separately.
14
- <!-- /shaper:project -->
15
- `,d=c.includes(f)?c.replace(/<!-- shaper:project -->[\s\S]*?<!-- \/shaper:project -->\n?/,l.trimStart()):c+l;d!==c&&await v(s,d)}async function D(t,n){await new Promise((r,e)=>{let i=wt(t,{cwd:n,shell:!0,stdio:["ignore","pipe","pipe"]});i.stdout.on("data",s=>process.stderr.write(s)),i.stderr.on("data",s=>process.stderr.write(s)),i.once("error",e),i.once("exit",s=>s===0?r():e(new a("COMMAND_FAILED",`\u5DE5\u7A0B\u547D\u4EE4\u9000\u51FA\u7801\uFF1A${s??"signal"}`)))})}async function It(t){let n=await j(t),r=await R(n);await D(r.buildCommand,n);let e=await F(n,r.outputDir);return await I(p(e,"index.html")),{root:n,output:e}}async function F(t,n){let r=await k(x(t,n)),e=C(t,r);if(e===".."||e.startsWith(`..${L}`)||x(t,e)!==r)throw new a("INVALID_OUTPUT","\u8F93\u51FA\u76EE\u5F55\u5FC5\u987B\u4F4D\u4E8E\u5DE5\u7A0B\u5185\u90E8\u3002");return r}async function yt(t,n={files:300,bytes:200*1024*1024}){let r=await k(t),e=new ht,i=0,s=0;async function c(f){for(let l of(await P(f,{withFileTypes:!0})).sort((d,u)=>d.name.localeCompare(u.name))){if(/^(\.git|\.shaper|node_modules|\.DS_Store)$/.test(l.name))continue;if(/^(\.env(?:\..*)?|credentials\.json|.*\.(pem|key))$/i.test(l.name))throw new a("SECRET_FILE_IN_PACKAGE",`\u8F93\u51FA\u76EE\u5F55\u5305\u542B\u654F\u611F\u914D\u7F6E\u6587\u4EF6\uFF1A${l.name}`);let d=p(f,l.name);if(l.isSymbolicLink())throw new a("SYMLINK_IN_PACKAGE",`\u4EA4\u4ED8\u5185\u5BB9\u4E2D\u4E0D\u5141\u8BB8\u7B26\u53F7\u94FE\u63A5\uFF1A${l.name}`);if(l.isDirectory()){await c(d);continue}if(!l.isFile())throw new a("INVALID_PACKAGE_FILE","\u4EA4\u4ED8\u76EE\u5F55\u5305\u542B\u975E\u5E38\u89C4\u6587\u4EF6\u3002");let u=await G(d);if(i+=u.size,s++,s>n.files||i>n.bytes)throw new a("PACKAGE_TOO_LARGE","\u4EA4\u4ED8\u5305\u8D85\u8FC7\u6587\u4EF6\u6570\u6216\u5927\u5C0F\u9650\u5236\u3002");e.addFile(C(r,d).split(L).join("/"),await I(d))}}await c(r);for(let f of e.getEntries())f.header.time=new Date(2e3,0,1);return{content:e.toBuffer(),files:s,bytes:i}}async function zt(t,n,r={}){let e=await j(n),i=await U(e,t.auth.server),s=r.build===!1?await F(e,(await R(e)).outputDir):(await It(e)).output,c=await yt(s),f=M("sha256").update(c.content).digest("hex"),l=await A(e),d=p(l,"web-deploy.json"),u=K();try{let w=g.object({sha256:g.string(),key:g.string(),appId:g.string(),server:g.string()}).passthrough().parse(await S(d));w.sha256===f&&w.appId===i.appId&&w.server===t.auth.server&&(u=w.key)}catch(w){if(w.code!=="ENOENT")throw w}await N(d,{sha256:f,key:u,appId:i.appId,server:t.auth.server});let h=await t.request(`/api/v1/developer/projects/${encodeURIComponent(i.appId)}/web/deploy`,{method:"POST",file:c.content,key:u});return await N(d,{sha256:f,key:u,appId:i.appId,server:t.auth.server,response:h}),h}async function Gt(t,n,r,e,i){let s=await j(n),c=await U(s,t.auth.server),f=i?await I(await F(s,i)):void 0;if(f&&f.length>2*1024*1024)throw new a("INVALID_COVER","\u5C01\u9762\u4E0D\u80FD\u8D85\u8FC7 2 MiB\u3002");let l=f?{filename:ut(i),base64:f.toString("base64")}:void 0,d={versionId:r,description:e,...l?{cover:l}:{}},u=M("sha256").update(JSON.stringify({appId:c.appId,body:d})).digest("hex"),h=await t.request(`/api/v1/developer/projects/${encodeURIComponent(c.appId)}/web/publish`,{method:"POST",body:d,key:u});if(h.result?.confirmationUrl){let w=new URL(h.result.confirmationUrl);if(w.origin!==t.auth.server||w.pathname!=="/applications/create")throw new a("INVALID_CONFIRMATION_URL","\u786E\u8BA4\u9875\u9762\u5730\u5740\u4E0D\u6B63\u786E");try{await mt(w.href,{wait:!1})}catch{}}return{...h,submitted:!1,next:"\u5728\u6D4F\u89C8\u5668\u6838\u5BF9\u5E94\u7528\u4FE1\u606F\u5E76\u70B9\u51FB\u63D0\u4EA4\u5BA1\u6838\u3002\u901A\u8FC7\u5E94\u7528\u7248\u672C\u72B6\u6001\u67E5\u8BE2\u6700\u7EC8\u7ED3\u679C\u3002"}}async function Ht(t){let n=await j(t),r=await R(n),e=[];try{await I(p(n,"package.json"))}catch{e.push("package.json \u4E0D\u5B58\u5728")}let i=x(n,r.outputDir),s=C(n,i);return(s===".."||s.startsWith(`..${L}`))&&e.push("outputDir \u8D85\u51FA\u5DE5\u7A0B\u76EE\u5F55"),{root:n,passed:e.length===0,findings:e,checks:["project configuration","package manifest","output path"],notRun:["application behavior","build","cloud connectivity"]}}export{a,V as b,_ as c,N as d,S as e,At as f,Rt as g,bt as h,$ as i,j,R as k,U as l,Vt as m,$t as n,It as o,yt as p,zt as q,Gt as r,Ht as s};
@@ -1,8 +0,0 @@
1
- ---
2
- name: hupu-cloud
3
- description: 兼容旧项目中对 hupu-cloud 的引用;新后端任务使用 shaper-backend。
4
- ---
5
-
6
- # 旧名称兼容
7
-
8
- 后端职责已迁移到 shaper-backend。继续当前任务前读取同次发行安装的 shaper-backend 技能;若缺失,修复官方技能安装。保留现有项目与云资源,不因技能改名重新初始化或创建环境。