@ngm-dev/cli
Version:
A CLI for ui.angular-material.dev
33 lines (28 loc) • 13.7 kB
JavaScript
import {userInfo,hostname}from'node:os';import*as o from'valibot';import v from'chalk';import {Octokit}from'octokit';var he="@ngm-dev/cli",T="@ngm-dev/cli",I="ngm-dev-cli",C=`${I}-manifest.json`,ge=`${I}.json`,H=process.env.REGISTRY_HOST??"https://ui.angular-material.dev",X=`${H}/${process.env.REGISTRY_URL??"api/registry"}`,me=`${H}/${process.env.NEW_TOKEN_URL??"account/tokens"}`,ve=[X],ye=e=>{try{return `cli_${userInfo().username}@${hostname}_${Date.now()}`}catch{return `cli_${e}@${hostname}_${Date.now()}`}},j="x-machine-id",F="x-username",Z="styles/ngm-dev-blocks-styles",we=["styles/themes","styles/vendors","styles/_base","styles/_dialogs","styles/_sizes","styles/_tabs",Z],Ee="CONFIGURE_NG2_CHARTS";var ee=o.object({name:o.string(),category:o.string(),localDependencies:o.array(o.string()),dependencies:o.array(o.string()),devDependencies:o.array(o.string()),tests:o.boolean(),list:o.optional(o.boolean(),true),directory:o.string(),subdirectory:o.boolean(),files:o.array(o.string()),_imports_:o.record(o.string(),o.string())}),x=o.object({name:o.string(),blocks:o.array(ee)}),re=o.object({authors:o.optional(o.array(o.string())),bugs:o.optional(o.string()),description:o.optional(o.string()),homepage:o.optional(o.string()),repository:o.optional(o.string()),tags:o.optional(o.array(o.string()))}),te=o.record(o.string(),o.union([o.string(),o.object({version:o.string(),message:o.string()})])),oe=o.object({name:o.string(),path:o.string(),expectedPath:o.optional(o.string()),optional:o.optional(o.boolean(),false)}),se=o.object({...oe.entries,dependencies:o.optional(o.array(o.string())),devDependencies:o.optional(o.array(o.string()))}),D=o.object({meta:o.optional(re),peerDependencies:o.optional(te),configFiles:o.optional(o.array(se)),categories:o.array(x)});var ne="main",S={name:"azure",matches:e=>e.toLowerCase().startsWith("azure"),parse:(e,r)=>{let t=N(e,r);return {url:t.url,specifier:t.specifier}},baseUrl:e=>{let{owner:r,repoName:t}=N(e,{fullyQualified:false});return `https://dev.azure.com/${r}/_git/${t}`},state:async e=>{let{url:r,owner:t,project:s,repoName:a,ref:p,refs:n}=N(e,{fullyQualified:false});return {owner:t,repoName:a,ref:p,refs:n,project:s,url:r,provider:S}},resolveRaw:async(e,r)=>{if(e.provider.name!==S.name)throw new Error(`You passed the incorrect state object (${e.provider.name}) to the ${S.name} provider.`);let{owner:t,repoName:s,project:a,ref:p,refs:n}=e,i=n==="tags"?"tag":"branch";return new URL(`https://dev.azure.com/${t}/${a}/_apis/git/repositories/${s}/items?path=${r}&api-version=7.2-preview.1&versionDescriptor.version=${p}&versionDescriptor.versionType=${i}`)},authHeader:e=>["Authorization",`Bearer ${e}`],formatFetchError:(e,r)=>`There was an error fetching \`${v.bold(r)}\` from ${v.bold(e.url)}.
${v.bold("This may be for one of the following reasons:")}
1. Either \`${v.bold(r)}\` or the containing repository doesn't exist
2. Your repository path is incorrect (wrong branch, wrong tag)
3. You are using an expired access token or a token that doesn't have access to this repository
4. The cached state for this git provider is incorrect (try using ${v.bold("--no-cache")})
`},N=(e,{fullyQualified:r})=>{let t=e.replaceAll(/(azure\/)/g,""),[s,a,p,...n]=t.split("/"),i=[...n],c;r&&(c=i.slice(i.length-2).join("/"),i=i.slice(0,i.length-2));let l=ne,u="heads";return ["tags","heads"].includes(i[0])&&(u=i[0],i[1]&&i[1]!==""&&(l=i[1])),{url:`azure/${s}/${a}/${p}${l?`/${u}/${l}`:""}`,owner:s,repoName:p,project:a,ref:l,refs:u,specifier:c}};var g=(e,r)=>{for(let t of r)if(e.startsWith(t))return true;return false},Ae=(e,r)=>{for(let t of r)if(e.endsWith(t))return true;return false};var M="master",w={name:"bitbucket",matches:e=>g(e.toLowerCase(),["bitbucket","https://bitbucket.org"]),parse:(e,r)=>{let t=O(e,r);return {url:t.url,specifier:t.specifier}},baseUrl:e=>{let{owner:r,repoName:t}=O(e,{fullyQualified:false});return `https://bitbucket.org/${r}/${t}`},state:async(e,{token:r,fetch:t=fetch}={})=>{let s=O(e,{fullyQualified:false}),{url:a,owner:p,repoName:n}=s,i=s.ref;if(i===void 0)try{let c=new Headers;if(r!==void 0){let[u,d]=w.authHeader(r);c.append(u,d);}let l=await t(`https://api.bitbucket.org/2.0/repositories/${p}/${n}`,{headers:c});l.ok?i=(await l.json()).mainbranch.name:i=M;}catch{i=M;}return {owner:p,ref:i,repoName:n,url:a,provider:w}},resolveRaw:async(e,r)=>{if(e.provider.name!==w.name)throw new Error(`You passed the incorrect state object (${e.provider.name}) to the ${w.name} provider.`);let{owner:t,repoName:s,ref:a}=e;return new URL(r,`https://api.bitbucket.org/2.0/repositories/${t}/${s}/src/${a}/`)},authHeader:e=>["Authorization",`Bearer ${e}`],formatFetchError:(e,r)=>`There was an error fetching \`${v.bold(r)}\` from ${v.bold(e.url)}.
${v.bold("This may be for one of the following reasons:")}
1. Either \`${v.bold(r)}\` or the containing repository doesn't exist
2. Your repository path is incorrect (wrong branch, wrong tag)
3. You are using an expired access token or a token that doesn't have access to this repository
4. The cached state for this git provider is incorrect (try using ${v.bold("--no-cache")})
`},O=(e,{fullyQualified:r=false})=>{let t=e.replaceAll(/(https:\/\/bitbucket.org\/)|(bitbucket\/)/g,""),[s,a,...p]=t.split("/"),n=[...p],i;r&&(i=n.slice(n.length-2).join("/"),n=n.slice(0,n.length-2));let c;return n[0]==="src"&&(c=n[1]),{url:`bitbucket/${s}/${a}${c?`/src/${c}`:""}`,specifier:i,owner:s,repoName:a,ref:c}};var G="main",A={name:"github",matches:e=>g(e.toLowerCase(),["github","https://github.com"]),parse:(e,r)=>{let t=P(e,r);return {url:t.url,specifier:t.specifier}},baseUrl:e=>{let{owner:r,repoName:t}=P(e,{fullyQualified:false});return `https://github.com/${r}/${t}`},state:async(e,{token:r}={})=>{let t=P(e,{fullyQualified:false}),{url:s,owner:a,repoName:p}=t,n=t.ref,i=new Octokit({auth:r}),c="heads";if(n===void 0)try{let{data:l}=await i.rest.repos.get({owner:a,repo:p});n=l.default_branch;}catch{n=G;}else if(n!==G)try{let{data:l}=await i.rest.git.listMatchingRefs({owner:a,repo:p,ref:"tags"});l.some(u=>u.ref===`refs/tags/${n}`)&&(c="tags");}catch{c="heads";}return {owner:a,refs:c,ref:n,repoName:p,url:s,provider:A}},resolveRaw:async(e,r)=>{if(e.provider.name!==A.name)throw new Error(`You passed the incorrect state object (${e.provider.name}) to the ${A.name} provider.`);let{owner:t,repoName:s,refs:a,ref:p}=e;return new URL(r,`https://raw.githubusercontent.com/${t}/${s}/refs/${a}/${p}/`)},authHeader:e=>["Authorization",`token ${e}`],formatFetchError:(e,r)=>`There was an error fetching \`${v.bold(r)}\` from ${v.bold(e.url)}.
${v.bold("This may be for one of the following reasons:")}
1. Either \`${v.bold(r)}\` or the containing repository doesn't exist
2. Your repository path is incorrect (wrong branch, wrong tag)
3. You are using an expired access token or a token that doesn't have access to this repository
4. The cached state for this git provider is incorrect (try using ${v.bold("--no-cache")})
`},P=(e,{fullyQualified:r=false})=>{let t=e.replaceAll(/(https:\/\/github.com\/)|(github\/)/g,""),[s,a,...p]=t.split("/"),n=[...p],i;r&&(i=n.slice(n.length-2).join("/"),n=n.slice(0,n.length-2));let c;return n.length>0&&n[0]==="tree"&&(c=n[1]),{url:`github/${s}/${a}${c?`/tree/${c}`:""}`,specifier:i,owner:s,repoName:a,ref:c}};var B="main",$={name:"gitlab",matches:e=>g(e.toLowerCase(),["gitlab","https://gitlab.com"]),parse:(e,r)=>{let t=k(e,r);return {url:t.url,specifier:t.specifier}},baseUrl:e=>{let{owner:r,repoName:t}=k(e,{fullyQualified:false});return `https://gitlab.com/${r}/${t}`},state:async(e,{token:r,fetch:t=fetch}={})=>{let s=k(e,{fullyQualified:false}),{owner:a,repoName:p,url:n}=s,i=s.ref;if(i===void 0)try{let c=new Headers;if(r!==void 0){let[u,d]=$.authHeader(r);c.append(u,d);}let l=await t(`https://gitlab.com/api/v4/projects/${encodeURIComponent(`${a}/${p}`)}`,{headers:c});l.ok?i=(await l.json()).default_branch:i=B;}catch{i=B;}return {owner:a,repoName:p,ref:i,url:n,provider:$}},resolveRaw:async(e,r)=>{if(e.provider.name!==$.name)throw new Error(`You passed the incorrect state object (${e.provider.name}) to the ${$.name} provider.`);let{owner:t,repoName:s,ref:a}=e;return new URL(`${encodeURIComponent(r)}/raw?ref=${a}`,`https://gitlab.com/api/v4/projects/${encodeURIComponent(`${t}/${s}`)}/repository/files/`)},authHeader:e=>["PRIVATE-TOKEN",e],formatFetchError:(e,r)=>`There was an error fetching \`${v.bold(r)}\` from ${v.bold(e.url)}.
${v.bold("This may be for one of the following reasons:")}
1. Either \`${v.bold(r)}\` or the containing repository doesn't exist
2. Your repository path is incorrect (wrong branch, wrong tag)
3. You are using an expired access token or a token that doesn't have access to this repository
4. The cached state for this git provider is incorrect (try using ${v.bold("--no-cache")})
`},k=(e,{fullyQualified:r})=>{let t=e.replaceAll(/(https:\/\/gitlab.com\/)|(gitlab\/)/g,""),[s,a,...p]=t.split("/"),n=[...p],i;r&&(i=n.slice(n.length-2).join("/"),n=n.slice(0,n.length-2));let c;if(n[0]==="-"&&n[1]==="tree")if(n[2].includes("?")){let[l]=n[2].split("?");c=l;}else c=n[2];return {url:`gitlab/${s}/${a}${c?`/-/tree/${c}`:""}`,owner:s,repoName:a,ref:c,specifier:i}};var z=(...e)=>e.map(r=>ae(r)).filter(Boolean).join("/"),ae=e=>{let r=ce(e);return pe(r)};var ce=e=>{let r=e;return r.startsWith("/")&&(r=r.slice(1)),r};var pe=e=>{let r=e;return r.endsWith("/")&&(r=r.slice(0,r.length-1)),r},Y=e=>{let r=e;return r.endsWith("/")||(r=`${r}/`),r};var m={name:"http",matches:e=>{try{return new URL(e),!0}catch{return false}},parse:(e,r)=>{let t=L(e,r);return {url:t.url,specifier:t.specifier}},baseUrl:e=>{let{url:r}=L(e,{fullyQualified:false});return new URL(r).origin},state:async e=>{let{url:r}=L(e,{fullyQualified:false});return {url:r,provider:m}},resolveRaw:async(e,r)=>{if(e.provider.name!==m.name)throw new Error(`You passed the incorrect state object (${e.provider.name}) to the ${m.name} provider.`);return new URL(r,e.url)},authHeader:e=>["Authorization",`Bearer ${e}`],extraTokenHeaders:({userName:e,machineId:r})=>[[F,e],[j,r]],headers:()=>{let e=process.env.CLI_ACCESS_TOKEN;if(!e)throw new Error("No CLI_ACCESS_TOKEN found for http provider");return new Headers({"user-agent":"ngm-dev-cli","x-cli-token":e})},formatFetchError:(e,r,t)=>`There was an error fetching ${v.bold(new URL(r,e.url).toString())}
${v.bold(t)}`},L=(e,{fullyQualified:r})=>{let t=new URL(e),s=t.pathname.split("/"),a;return r&&(a=s.slice(s.length-2).join("/"),s=s.slice(0,s.length-2)),{url:Y(z(t.origin,...s)),specifier:a}};var _=class{_result;constructor(r){this._result=r;}match(r,t){return this._result.ok?r(this._result.val):t(this._result.err)}map(r){return this.match(t=>h(r(t)),t=>f(t))}mapOr(r,t){return this.match(s=>t(s),s=>r)}mapOrElse(r,t){return this.match(s=>t(s),s=>r(s))}mapErr(r){return this.match(t=>h(t),t=>f(r(t)))}mapErrOr(r,t){return this.match(s=>r,s=>t(s))}mapErrOrElse(r,t){return this.match(s=>r(s),s=>t(s))}isOk(){return this.match(()=>true,()=>false)}isErr(){return this.match(()=>false,()=>true)}unwrap(){return this.match(r=>r,()=>{throw new Error("Attempted to call `.unwrap()` on a non `Ok` value.")})}unwrapErr(){return this.match(()=>{throw new Error("Attempted to call `.unwrapErr()` on a non `Err` value.")},r=>r)}unwrapOr(r){return this.match(t=>t,t=>r)}unwrapErrOr(r){return this.match(()=>r,t=>t)}unwrapOrElse(r){return this.match(t=>t,t=>r(t))}unwrapErrOrElse(r){return this.match(t=>r(t),t=>t)}expect(r){return this.match(t=>t,()=>{throw new Error(r)})}expectErr(r){return this.match(()=>{throw new Error(r)},t=>t)}},h=e=>new _({ok:true,val:e}),f=e=>new _({ok:false,err:e});var Q=e=>{let r;try{r=JSON.parse(e);}catch(s){return f(`Error parsing manifest json ${s}`)}if(Array.isArray(r)){let s=o.safeParse(o.array(x),r);return s.success?h({categories:s.output}):f(`Error parsing categories (array-based config) ${s.issues.join(" ")}`)}let t=o.safeParse(D,r);return t.success?h(t.output):f(`Error parsing manifest ${t.issues.join(" ")}`)};var ue=[m],Ze=e=>ue.find(t=>t.matches(e)),fe=async(e,r,{verbose:t,fetch:s=fetch,token:a,userName:p,machineId:n}={})=>{let i=await e.provider.resolveRaw(e,r);t?.(`Trying to fetch from ${i}`);try{let c=new Headers;if(e.provider.headers)for(let[u,d]of e.provider.headers().entries())c.append(u,d);if(e.provider.name===m.name)if(a&&p&&n&&e.provider.authHeader&&e.provider.extraTokenHeaders&&e.provider.headers){let[u,d]=e.provider.authHeader(a);if(c.append(u,d),e.provider.name==="http"&&e.provider.extraTokenHeaders){if(!p)throw new Error("No user name found for http provider");if(!n)throw new Error("No machine id found for http provider");for(let[V,q]of e.provider.extraTokenHeaders({token:a,userName:p,machineId:n}))c.append(V,q);}else throw new Error("No auth header found for http provider")}else throw new Error(`Token and username are required for ${e.provider.name} provider. Use "${v.cyan.bold(`${T} auth`)}" to authenticate.`);let l=await s(i,{headers:c});return t?.(`Got a response from ${i} ${l.status} ${l.statusText}`),l.ok?h(await l.text()):f(e.provider.formatFetchError(e,r,`${l.status} ${l.statusText}${l.status===401?`
You may want to authenticate using "${v.bold(`${T} auth`)}"`:""}`))}catch(c){return f(e.provider.formatFetchError(e,r,c))}},er=async(e,{fetch:r=fetch,...t}={})=>{let s=await fe(e,C,{fetch:r,...t});return s.isErr()?f(s.unwrapErr()):Q(s.unwrap())};export{Q as A,S as B,w as C,A as D,$ as E,m as F,ue as G,Ze as H,fe as I,er as J,he as a,T as b,I as c,C as d,ge as e,H as f,X as g,me as h,ve as i,ye as j,j as k,F as l,Z as m,we as n,Ee as o,h as p,f as q,z as r,ee as s,x as t,re as u,te as v,oe as w,se as x,D as y,Ae as z};//# sourceMappingURL=chunk-SNPGRLIR.js.map
//# sourceMappingURL=chunk-SNPGRLIR.js.map