UNPKG

storybook-branch-switcher

Version:

A Storybook addon to navigate between multiple Git branches.

3 lines 3.77 kB
#!/usr/bin/env node import'zx/globals';var C=r=>r.type==="bitbucket"&&r.project!=null&&r.repository!=null,B=()=>{let r=$.env.BITBUCKET_TOKEN;if(r!=null)return `Bearer ${r}`;let t=$.env.BITBUCKET_USERNAME,n=$.env.BITBUCKET_PASSWORD;if(t!=null&&n!=null)return `Basic ${Buffer.from(`${t}:${n}`).toString("base64")}`},x=async r=>{let n=`${r.url??"https://bitbucket.org"}/rest/api/1.0/projects/${r.project}/repos/${r.repository}/pull-requests?state=open`,o=await fetch(n,{headers:{Authorization:B()}});if(o.ok)return (await o.json()).values.map(e=>({id:`PR-${e.id}`,commit:e.fromRef.latestCommit}));throw new Error(`Error during fetch: ${o.status} ${o.statusText}`)},k={isApplicable:C,fetcher:x},p=k;var E=r=>r.type==="github"&&r.owner!=null&&r.repository!=null,S=async r=>{let n=`${r.url??"https://api.github.com"}/repos/${r.owner}/${r.repository}/pulls?state=open`,o=await fetch(n,{headers:{Accept:"Accept: application/vnd.github+json",Authorization:`Bearer ${$.env.GITHUB_TOKEN}`,"X-GitHub-Api-Version":"2022-11-28"}});if(o.ok)return (await o.json()).map(e=>({id:`PR-${e.number}`,commit:e.head.sha}));throw new Error(`Error during fetch: ${o.status} ${o.statusText}`)},A={isApplicable:E,fetcher:S},d=A;var T=r=>r.type==="gitlab"&&r.projectId!=null,_=async r=>{let n=`${r.url??"https://gitlab.com"}/api/v4/projects/${r.projectId}/merge_requests?state=opened&order_by=updated_at&sort=desc`,o=await fetch(n,{headers:{"PRIVATE-TOKEN":`${$.env.GITLAB_TOKEN}`}});if(!o.ok)throw new Error(`HTTP error! status: ${o.status} ${o.statusText}`);return (await o.json()).map(e=>({id:e.id,commit:e.head.sha,title:e.title,source_branch:e.source_branch}))},j={isApplicable:T,fetcher:_},u=j;var G=[p,d,u],h=G;var f=async r=>{let t=[],n=await $`git branch --show-current`,o=await $`git log --format="%H" -n 1`;return t.push({id:n.toString().trim(),commit:o.toString().trim()}),r&&await spinner("Fetching branches..",async()=>{let i=h.find(e=>e.isApplicable(r));if(!i)throw new Error("Cannot find a branch provider");t.push(...await i.fetcher(r));}),t};var l=async r=>{fs.removeSync(r);},g=async r=>{await $`git fetch origin`,await $`git checkout ${r}`,await $`git submodule update --init`;},m=r=>{process.env.STORYBOOK_BRANCH_SWITCHER_STATE=JSON.stringify(r);},v=async r=>{await $`npm run ${r??"build-storybook"}`.pipe(process.stdout);},b=async(r,t)=>{fs.cpSync(r,t,{recursive:true});};var y=async r=>{try{return await fs.readJson(r)}catch{throw new Error(`Cannot read configuration file at ${r}`)}},P=async()=>{try{await $`node --version`;}catch{throw new Error("Node must be installed")}},w=async()=>{if(!(await $`git --version`).toString().includes("git version 2."))throw new Error("Git 2.x must be installed");try{await $`git diff-index --quiet HEAD`;}catch{throw new Error("Git workspace must be clean")}};var s=async(r,t)=>$.env.CI==="true"?(echo(r),t()):spinner(r,t),a=r=>echo(chalk.green(r)),c=r=>echo(`\u274C ${chalk.red(r)}`);(async function(){let r=argv.c??argv.config??".storybook/.branches.json",t;$.verbose=argv.verbose!==void 0;try{t=await y(r),t.directory&&cd(t.directory),await P(),await w(),a("\u{1F60E} Your workspace looks good!");}catch(i){c(i),process.exit(1);}let n=await f(t.provider),o=t.default_branch??n[0].id;echo(`\u{1F450} Branches to build: ${chalk.cyan(n.map(i=>i.id).join(", "))}`),await l(t.to);for(let i of n){echo`🚚 Move to the ${chalk.cyan(i.id)} branch`,m({list:n.map(e=>e.id),currentBranch:i.id,defaultBranch:o});try{await s(`Fetching ${i.commit}...`,()=>g(i.commit)),await v(t.script_name),await s("Copying files...",()=>b(t.from,o===i.id&&t.default_root?t.to:path.join(t.to,i.id)));}catch(e){c(e.stderr?.trim()??e);}}await $`git checkout ${o}`,a("\u{1F389} Bundle ready!");})();//# sourceMappingURL=cli.js.map //# sourceMappingURL=cli.js.map