UNPKG

@lunora/cli

Version:

The Lunora CLI: init, dev, deploy, codegen, migrate, seed, doctor, insights, logs, registry, and the rest of the project commands

3 lines (2 loc) • 2.35 kB
import{existsSync as l,readFileSync as u,mkdirSync as v,realpathSync as E,statSync as b,writeFileSync as M,chmodSync as k,renameSync as x,unlinkSync as $}from"node:fs";import{dirname as h}from"@visulima/path";import{parse as w,modify as O,applyEdits as j}from"jsonc-parser";const g=(t,n)=>{let i=t,r;try{i=E(t),r=b(i).mode}catch{}const o=`${i}.lunora-${Math.random().toString(36).slice(2,10)}.tmp`;try{v(h(i),{recursive:!0}),M(o,n,{encoding:"utf8",mode:r??384}),k(o,r??384),x(o,i)}catch(e){try{$(o)}catch{}throw e}},A={formattingOptions:{insertSpaces:!0,tabSize:4}},d=t=>{const n=[],i=w(t,n,{allowTrailingComma:!0});return{errors:n,value:i}},f=t=>typeof t=="object"&&t!==null&&!Array.isArray(t),p=(t,n,i)=>{if(!f(t))return;const r=t[n];return f(r)?r[i]:void 0},F=t=>{if(!l(t.path))return"absent";let n;try{n=u(t.path,"utf8")}catch{return"invalid"}if(n.trim().length===0)return"absent";const{errors:i,value:r}=d(n);return i.length>0||!f(r)?"invalid":p(r,t.key,t.name)===void 0?"absent":"present"},J=t=>F(t)==="present",S=(t,n,i,r,o)=>{try{const e=O(t,[i,r],o,A);e.length>0&&g(n,j(t,e));return}catch(e){return e instanceof Error?e.message:String(e)}},P=t=>{const{entry:n,force:i=!1,key:r,name:o,path:e}=t,c=l(e)?u(e,"utf8"):"";if(c.trim().length===0)return v(h(e),{recursive:!0}),g(e,`${JSON.stringify({[r]:{[o]:n}},void 0,4)} `),{action:"created",path:e};const{errors:s,value:a}=d(c);if(s.length>0)return{action:"invalid",error:`parse error at offset ${String(s[0]?.offset??0)}`,path:e};if(!f(a))return{action:"invalid",error:"the file's root is not a JSON object",path:e};const y=a[r];if(y!==void 0&&!f(y))return{action:"invalid",error:`"${r}" is not an object`,path:e};if(p(a,r,o)!==void 0&&!i)return{action:"skipped",path:e};const m=S(c,e,r,o,n);return m===void 0?{action:"updated",path:e}:{action:"invalid",error:m,path:e}},z=t=>{const{key:n,name:i,path:r}=t;if(!l(r))return{action:"absent",path:r};let o;try{o=u(r,"utf8")}catch(a){return{action:"invalid",error:a instanceof Error?a.message:String(a),path:r}}const{errors:e,value:c}=d(o);if(e.length>0)return{action:"invalid",error:`parse error at offset ${String(e[0]?.offset??0)}`,path:r};if(p(c,n,i)===void 0)return{action:"absent",path:r};const s=S(o,r,n,i,void 0);return s===void 0?{action:"removed",path:r}:{action:"invalid",error:s,path:r}};export{J as h,F as i,z as r,P as u,g as w};