@ycmd/creds
Version:
LSK.js CLI Creds is the easiest way to manage GitHub / Gitlab secrets and credentials
5 lines • 7.47 kB
JavaScript
var S=Object.defineProperty;var c=(s,e)=>S(s,"name",{value:e,configurable:!0}),y=(s=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(s,{get:(e,r)=>(typeof require<"u"?require:e)[r]}):s)(function(s){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+s+'" is not supported')});import{Err as I}from"@lskjs/err";import{log as P}from"@lskjs/log/log";import{getComment as H,jsonToFile as x}from"@lskjs/stringify";import{map as M,mapSeries as C}from"fishbird";import{existsSync as T}from"fs";import{mkdir as K,unlink as V}from"fs/promises";import{Err as w}from"@lskjs/err";import E from"libsodium-wrappers";import{Err as d}from"@lskjs/err";import{log as l}from"@lskjs/log/log";import U from"axios";import{map as g}from"fishbird";var p=class{static{c(this,"Service")}projectId;projectName;projectPath;projectCredsUrl;token;server;force;client;constructor(e){Object.assign(this,e),this.checkConfig();let r={baseURL:this.getBaseUrl(),headers:this.getHeaders()};this.client=U.create(r)}checkConfig(){throw new d("NOT_IMPLEMENTED")}getBaseUrl(){return null}getHeaders(){return{}}getServiceLink(){return null}getProjectName(){return this.projectName}getProjectId(){return this.projectId}getProjectPath(){return this.projectPath}getProjectUrl(){return null}getProjectCredsUrl(){return this.projectCredsUrl}getProjectCICDSettingURL(){return null}async uploadSecret(e,r){throw new d("NOT_IMPLEMENTED")}async uploadVariable(e,r){throw new d("NOT_IMPLEMENTED")}async removeOldHooks(){}async uploadHook(e){}async uploadHooks(e){if(!e)throw new d("!env");let{hooks:r=[]}=e;try{await this.removeOldHooks()}catch(t){l.error("[ERR] Old hooks removing failed:",t.message)}await g(r,async(t,o)=>{try{await this.uploadHook(t),l.info(`[OK] Hook ${o} uploaded`)}catch(a){l.error(`[ERR] Hook ${o} not uploaded:`,a.message)}})}async uploadAll(e){if(!e)throw new d("!env");let{secrets:r={},variables:t={},files:o=[]}=e;await this.uploadHooks(e),await g(Object.entries(r),async([a,i])=>{try{await this.uploadSecret(a,i),l.info(`[OK] Secret ${a} uploaded`)}catch(u){l.error(`[ERR] Secret ${a} not uploaded, because`,u.message),l.error(u)}}),await g(Object.entries(t),async([a,i])=>{try{await this.uploadVariable(a,i),l.info(`[OK] Variable ${a} uploaded`)}catch(u){l.error(`[ERR] Variable ${a} not uploaded, because`,u.message)}}),await g(o,async({name:a,credType:i,content:u})=>{let n=a,h=u;try{if(i==="variable")await this.uploadVariable(n,h);else if(i==="secret")await this.uploadSecret(n,h);else if(i==="skip"){l.debug(`[SKIP] File ${n} uploaded as ${i}`);return}else throw new d("unknown credType",{credType:i});l.info(`[OK] File ${n} uploaded as ${i}`)}catch(m){l.error(`[ERR] File ${n} not uploaded as ${i}, because`,m.message)}})}};var v=class extends p{static{c(this,"GithubService")}checkConfig(){if(!this.token)throw new w("!token")}f;getBaseUrl(){return`https://${this.server||"api.github.com"}/repos/${this.getProjectPath()}`}getHeaders(){return{Accept:"application/vnd.github+json",Authorization:`Bearer ${this.token}`,"X-GitHub-Api-Version":"2022-11-28"}}getServiceLink(){return"github.com"}getProjectUrl(){return`https://${this.getServiceLink()}/${this.projectName}`}getProjectCICDSettingURL(){return`${this.getProjectUrl()}/settings/secrets/actions`}async uploadSecret(e,r){let{data:t}=await this.client({method:"get",url:"/actions/secrets/public-key"}).catch(h=>{throw new w(h.message,{data:h?.response?.data})});if(!t?.key)throw new w("!publicKey");if(!t?.key_id)throw new w("!publicKeyId");await E.ready;let o=E,a=o.from_base64(t.key,o.base64_variants.ORIGINAL),i=o.from_string(r),u=o.crypto_box_seal(i,a),n=o.to_base64(u,o.base64_variants.ORIGINAL);await this.client({method:"put",url:`/actions/secrets/${e}`,data:{encrypted_value:n,key_id:t.key_id}})}async uploadVariable(e,r){let{data:t,status:o}=await this.client({method:"get",url:`/actions/variables/${e}`}).catch(a=>a?.response);o===404&&await this.client({method:"post",url:"/actions/variables",data:{name:e,value:r}}),o===200&&t.name.toLowerCase()===e.toLowerCase()&&await this.client({method:"patch",url:`/actions/variables/${e}`,data:{name:e,value:r}})}uploadHook(){throw new w("Github hooks not supported yet")}};import{Err as f}from"@lskjs/err";import{log as j}from"@lskjs/log/log";import{map as D}from"fishbird";var b=class extends p{static{c(this,"GitlabService")}checkConfig(){if(!this.server)throw new f("!server");if(!this.projectId)throw new f("!projectId");if(!this.token)throw new f("!token")}getBaseUrl(){return`https://${this.server}/api/v4/projects/${this.getProjectId()}`}getHeaders(){return{"PRIVATE-TOKEN":this.token}}getServiceLink(){return this.server}getProjectUrl(){return`https://${this.getServiceLink()}/${this.getProjectPath()}`}getProjectCICDSettingURL(){return`${this.getProjectUrl()}/-/settings/ci_cd`}async uploadSecret(e,r){let{data:t}=await this.client({method:"get",url:`/variables/${e}`}).catch(o=>{if(!this.force)throw o;return{data:{value:"@lskjs/creds"}}});if(t.value&&t.value.indexOf("@lskjs/creds")===-1&&!this.force){j.warn(`[IGNORE] Project ${this.projectId} ${e}`);return}await this.client({method:"delete",url:`/variables/${e}`}).catch(()=>{}),await this.client({method:"post",url:"/variables",data:{key:e,variable_type:"file",value:r,protected:!0}})}async uploadVariable(){throw j.warn("GitLab uploading variable doesn't supported"),new f("NOT_IMPLEMENTED")}async uploadEnv(){throw j.warn("GitLab uploading env doesn't supported"),new f("NOT_IMPLEMENTED")}async removeOldHooks(){let{data:e}=await this.client({method:"get",url:"/hooks"}).catch(r=>{if(!this.force)throw r;return{data:{value:"@lskjs/creds"}}});await D(e,async({id:r})=>{await this.client({method:"delete",url:`/hooks/${r}`})})}async uploadHook(e){await this.client({method:"post",url:"/hooks",data:e})}};import{map as _}from"fishbird";import{readdir as R}from"fs/promises";import{resolve as k}from"path";async function $(s){let e=await R(s,{withFileTypes:!0});return(await _(e,async t=>t.isDirectory()?[{name:t.name,dir:k(s),filename:k(s,t.name)},...await $(k(s,t.name))]:[])).flat()}c($,"getDirs");async function A(s,e={}){let r=e.buildDir||`${s}/build`,t,o=`${s}/config.js`;try{t=y(o)}catch(n){if(n.code==="MODULE_NOT_FOUND"){P.error(`${o} not found`);return}throw n}await V(`${r}`).catch(()=>{}),await K(r,{recursive:!0});let a=t.service?.serviceName;if(!a)throw new I("!serviceName");let i;if(a==="github")i=new v({...t.service,...e});else if(a==="gitlab")i=new b({...t.service,...e});else throw new I("incorrect serviceName",{serviceName:a});let{files:u=[]}=t;await C(u,async n=>{let{type:h,filename:m,handler:L}=n,N=await L(n,t),O=H({filename:m,values:[["Server",i.getServiceLink()],["Project",i.getProjectPath()],["Project ID",i.getProjectId()],["Project Url",i.getProjectUrl()],["CI/CD Setting",i.getProjectCICDSettingURL()]],footer:i.getProjectCredsUrl()&&`
Auto generated by ${i.getProjectCredsUrl()}
If you want to change something, please contact admin repo.
`.trim()});await x(`${r}/${m}`,N,{type:h,compare:!e.force,comment:O}),P.info(`[build] ${i.getProjectPath()} (${m})`)})}c(A,"buildCommand");async function ke(s,e={}){let r=await $(s),t=(await M(r,async o=>{let{filename:a}=o;return await T(`${a}/config.js`)?o:null})).filter(Boolean);return C(t,async({filename:o})=>{await A(o,e).catch(a=>{P.error(`Build error ${o}: `,a)})})}c(ke,"buildDeepCommand");export{A as buildCommand,ke as buildDeepCommand};
//# sourceMappingURL=buildCommand.mjs.map