@ngm-dev/cli
Version:
A CLI for ui.angular-material.dev
33 lines (28 loc) • 13.9 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",j="@ngm-dev/cli",I="ngm-dev-cli",C=`${I}-manifest.json`,me=`${I}.json`,F=process.env.REGISTRY_HOST??"https://ui.angular-material.dev",q=`${F}/${process.env.REGISTRY_URL??"api/registry"}`,ve=`${F}/${process.env.NEW_TOKEN_URL??"account/tokens"}`,ye=[q],A=process.env.CLI_ACCESS_TOKEN??"pMkTiJ6X7gDBfx3qV5dJ4ud11kV0URWKwSs8dQxXLZOEh489PFwSxqyUStj0Rfah",Ee=r=>{try{return `cli_${userInfo().username}@${hostname}_${Date.now()}`}catch{return `cli_${r}@${hostname}_${Date.now()}`}},D="x-machine-id",H="x-username",Z="styles/ngm-dev-blocks-styles",we=["styles/themes","styles/vendors","styles/_base","styles/_dialogs","styles/_sizes","styles/_tabs",Z],be="CONFIGURE_NG2_CHARTS",$e=["jest","jest-canvas-mock","jest-environment-jsdom","jest-environment-node","jest-preset-angular","@types/jest","ts-jest","ts-node"],Se=["@angular/animations"];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()))}),M=o.object({meta:o.optional(re),peerDependencies:o.optional(te),configFiles:o.optional(o.array(se)),categories:o.array(x)});var ne="main",R={name:"azure",matches:r=>r.toLowerCase().startsWith("azure"),parse:(r,e)=>{let t=N(r,e);return {url:t.url,specifier:t.specifier}},baseUrl:r=>{let{owner:e,repoName:t}=N(r,{fullyQualified:false});return `https://dev.azure.com/${e}/_git/${t}`},state:async r=>{let{url:e,owner:t,project:s,repoName:a,ref:p,refs:n}=N(r,{fullyQualified:false});return {owner:t,repoName:a,ref:p,refs:n,project:s,url:e,provider:R}},resolveRaw:async(r,e)=>{if(r.provider.name!==R.name)throw new Error(`You passed the incorrect state object (${r.provider.name}) to the ${R.name} provider.`);let{owner:t,repoName:s,project:a,ref:p,refs:n}=r,i=n==="tags"?"tag":"branch";return new URL(`https://dev.azure.com/${t}/${a}/_apis/git/repositories/${s}/items?path=${e}&api-version=7.2-preview.1&versionDescriptor.version=${p}&versionDescriptor.versionType=${i}`)},authHeader:r=>["Authorization",`Bearer ${r}`],formatFetchError:(r,e)=>`There was an error fetching \`${v.bold(e)}\` from ${v.bold(r.url)}.
${v.bold("This may be for one of the following reasons:")}
1. Either \`${v.bold(e)}\` 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=(r,{fullyQualified:e})=>{let t=r.replaceAll(/(azure\/)/g,""),[s,a,p,...n]=t.split("/"),i=[...n],c;e&&(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 h=(r,e)=>{for(let t of e)if(r.startsWith(t))return true;return false},xe=(r,e)=>{for(let t of e)if(r.endsWith(t))return true;return false};var G="master",E={name:"bitbucket",matches:r=>h(r.toLowerCase(),["bitbucket","https://bitbucket.org"]),parse:(r,e)=>{let t=O(r,e);return {url:t.url,specifier:t.specifier}},baseUrl:r=>{let{owner:e,repoName:t}=O(r,{fullyQualified:false});return `https://bitbucket.org/${e}/${t}`},state:async(r,{token:e,fetch:t=fetch}={})=>{let s=O(r,{fullyQualified:false}),{url:a,owner:p,repoName:n}=s,i=s.ref;if(i===void 0)try{let c=new Headers;if(e!==void 0){let[u,d]=E.authHeader(e);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=G;}catch{i=G;}return {owner:p,ref:i,repoName:n,url:a,provider:E}},resolveRaw:async(r,e)=>{if(r.provider.name!==E.name)throw new Error(`You passed the incorrect state object (${r.provider.name}) to the ${E.name} provider.`);let{owner:t,repoName:s,ref:a}=r;return new URL(e,`https://api.bitbucket.org/2.0/repositories/${t}/${s}/src/${a}/`)},authHeader:r=>["Authorization",`Bearer ${r}`],formatFetchError:(r,e)=>`There was an error fetching \`${v.bold(e)}\` from ${v.bold(r.url)}.
${v.bold("This may be for one of the following reasons:")}
1. Either \`${v.bold(e)}\` 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=(r,{fullyQualified:e=false})=>{let t=r.replaceAll(/(https:\/\/bitbucket.org\/)|(bitbucket\/)/g,""),[s,a,...p]=t.split("/"),n=[...p],i;e&&(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 B="main",_={name:"github",matches:r=>h(r.toLowerCase(),["github","https://github.com"]),parse:(r,e)=>{let t=P(r,e);return {url:t.url,specifier:t.specifier}},baseUrl:r=>{let{owner:e,repoName:t}=P(r,{fullyQualified:false});return `https://github.com/${e}/${t}`},state:async(r,{token:e}={})=>{let t=P(r,{fullyQualified:false}),{url:s,owner:a,repoName:p}=t,n=t.ref,i=new Octokit({auth:e}),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=B;}else if(n!==B)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:_}},resolveRaw:async(r,e)=>{if(r.provider.name!==_.name)throw new Error(`You passed the incorrect state object (${r.provider.name}) to the ${_.name} provider.`);let{owner:t,repoName:s,refs:a,ref:p}=r;return new URL(e,`https://raw.githubusercontent.com/${t}/${s}/refs/${a}/${p}/`)},authHeader:r=>["Authorization",`token ${r}`],formatFetchError:(r,e)=>`There was an error fetching \`${v.bold(e)}\` from ${v.bold(r.url)}.
${v.bold("This may be for one of the following reasons:")}
1. Either \`${v.bold(e)}\` 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=(r,{fullyQualified:e=false})=>{let t=r.replaceAll(/(https:\/\/github.com\/)|(github\/)/g,""),[s,a,...p]=t.split("/"),n=[...p],i;e&&(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 z="main",$={name:"gitlab",matches:r=>h(r.toLowerCase(),["gitlab","https://gitlab.com"]),parse:(r,e)=>{let t=k(r,e);return {url:t.url,specifier:t.specifier}},baseUrl:r=>{let{owner:e,repoName:t}=k(r,{fullyQualified:false});return `https://gitlab.com/${e}/${t}`},state:async(r,{token:e,fetch:t=fetch}={})=>{let s=k(r,{fullyQualified:false}),{owner:a,repoName:p,url:n}=s,i=s.ref;if(i===void 0)try{let c=new Headers;if(e!==void 0){let[u,d]=$.authHeader(e);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=z;}catch{i=z;}return {owner:a,repoName:p,ref:i,url:n,provider:$}},resolveRaw:async(r,e)=>{if(r.provider.name!==$.name)throw new Error(`You passed the incorrect state object (${r.provider.name}) to the ${$.name} provider.`);let{owner:t,repoName:s,ref:a}=r;return new URL(`${encodeURIComponent(e)}/raw?ref=${a}`,`https://gitlab.com/api/v4/projects/${encodeURIComponent(`${t}/${s}`)}/repository/files/`)},authHeader:r=>["PRIVATE-TOKEN",r],formatFetchError:(r,e)=>`There was an error fetching \`${v.bold(e)}\` from ${v.bold(r.url)}.
${v.bold("This may be for one of the following reasons:")}
1. Either \`${v.bold(e)}\` 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=(r,{fullyQualified:e})=>{let t=r.replaceAll(/(https:\/\/gitlab.com\/)|(gitlab\/)/g,""),[s,a,...p]=t.split("/"),n=[...p],i;e&&(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 Y=(...r)=>r.map(e=>ae(e)).filter(Boolean).join("/"),ae=r=>{let e=ce(r);return pe(e)};var ce=r=>{let e=r;return e.startsWith("/")&&(e=e.slice(1)),e};var pe=r=>{let e=r;return e.endsWith("/")&&(e=e.slice(0,e.length-1)),e},W=r=>{let e=r;return e.endsWith("/")||(e=`${e}/`),e};var m={name:"http",matches:r=>{try{return new URL(r),!0}catch{return false}},parse:(r,e)=>{let t=L(r,e);return {url:t.url,specifier:t.specifier}},baseUrl:r=>{let{url:e}=L(r,{fullyQualified:false});return new URL(e).origin},state:async r=>{let{url:e}=L(r,{fullyQualified:false});return {url:e,provider:m}},resolveRaw:async(r,e)=>{if(r.provider.name!==m.name)throw new Error(`You passed the incorrect state object (${r.provider.name}) to the ${m.name} provider.`);return new URL(e,r.url)},authHeader:r=>["Authorization",`Bearer ${r}`],extraTokenHeaders:({userName:r,machineId:e})=>[[H,r],[D,e]],headers:()=>{if(!A)throw new Error("No CLI_ACCESS_TOKEN found for http provider");return new Headers({"user-agent":"ngm-dev-cli","x-cli-token":A})},formatFetchError:(r,e,t)=>`There was an error fetching ${v.bold(new URL(e,r.url).toString())}
${v.bold(t)}`},L=(r,{fullyQualified:e})=>{let t=new URL(r),s=t.pathname.split("/"),a;return e&&(a=s.slice(s.length-2).join("/"),s=s.slice(0,s.length-2)),{url:W(Y(t.origin,...s)),specifier:a}};var T=class{_result;constructor(e){this._result=e;}match(e,t){return this._result.ok?e(this._result.val):t(this._result.err)}map(e){return this.match(t=>g(e(t)),t=>f(t))}mapOr(e,t){return this.match(s=>t(s),s=>e)}mapOrElse(e,t){return this.match(s=>t(s),s=>e(s))}mapErr(e){return this.match(t=>g(t),t=>f(e(t)))}mapErrOr(e,t){return this.match(s=>e,s=>t(s))}mapErrOrElse(e,t){return this.match(s=>e(s),s=>t(s))}isOk(){return this.match(()=>true,()=>false)}isErr(){return this.match(()=>false,()=>true)}unwrap(){return this.match(e=>e,()=>{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.")},e=>e)}unwrapOr(e){return this.match(t=>t,t=>e)}unwrapErrOr(e){return this.match(()=>e,t=>t)}unwrapOrElse(e){return this.match(t=>t,t=>e(t))}unwrapErrOrElse(e){return this.match(t=>e(t),t=>t)}expect(e){return this.match(t=>t,()=>{throw new Error(e)})}expectErr(e){return this.match(()=>{throw new Error(e)},t=>t)}},g=r=>new T({ok:true,val:r}),f=r=>new T({ok:false,err:r});var Q=r=>{let e;try{e=JSON.parse(r);}catch(s){return f(`Error parsing manifest json ${s}`)}if(Array.isArray(e)){let s=o.safeParse(o.array(x),e);return s.success?g({categories:s.output}):f(`Error parsing categories (array-based config) ${s.issues.join(" ")}`)}let t=o.safeParse(M,e);return t.success?g(t.output):f(`Error parsing manifest ${t.issues.join(" ")}`)};var fe=[m],tr=r=>fe.find(t=>t.matches(r)),de=async(r,e,{verbose:t,fetch:s=fetch,token:a,userName:p,machineId:n}={})=>{let i=await r.provider.resolveRaw(r,e);t?.(`Trying to fetch from ${i}`);try{let c=new Headers;if(r.provider.headers)for(let[u,d]of r.provider.headers().entries())c.append(u,d);if(r.provider.name===m.name&&a&&p&&n&&r.provider.authHeader&&r.provider.extraTokenHeaders){let[u,d]=r.provider.authHeader(a);if(c.append(u,d),r.provider.name==="http"&&r.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,J]of r.provider.extraTokenHeaders({token:a,userName:p,machineId:n}))c.append(V,J);}else throw new Error("No auth header found for http provider")}let l=await s(i,{headers:c});return t?.(`Got a response from ${i} ${l.status} ${l.statusText}`),l.ok?g(await l.text()):(t?.(`Error: ${JSON.stringify(await l.json())}`),f(r.provider.formatFetchError(r,e,`${l.status} ${l.statusText}${l.status===401?`
You may want to authenticate using "${v.bold(`${j} auth`)}"`:""}`)))}catch(c){return t?.(`Error: ${JSON.stringify(c)}`),f(r.provider.formatFetchError(r,e,c))}},or=async(r,{fetch:e=fetch,...t}={})=>{let s=await de(r,C,{fetch:e,...t});return s.isErr()?f(s.unwrapErr()):Q(s.unwrap())};export{se as A,M as B,xe as C,Q as D,R as E,E as F,_ as G,$ as H,m as I,fe as J,tr as K,de as L,or as M,he as a,j as b,I as c,C as d,me as e,F as f,q as g,ve as h,ye as i,A as j,Ee as k,D as l,H as m,Z as n,we as o,be as p,$e as q,Se as r,g as s,f as t,Y as u,ee as v,x as w,re as x,te as y,oe as z};//# sourceMappingURL=chunk-76JHMPAQ.js.map
//# sourceMappingURL=chunk-76JHMPAQ.js.map