UNPKG

create-trybe-app

Version:
7 lines (6 loc) 7.47 kB
#!/usr/bin/env node import oe from"fs";import P from"path";import{Command as fe}from"commander";import{input as le,confirm as J,select as me,checkbox as de}from"@inquirer/prompts";import d from"chalk";var i={error(...e){console.log(d.red(...e))},warning(...e){console.log(d.yellow(...e))},info(...e){console.log(d.cyan(...e))},success(...e){console.log(d.green(...e))}};function A(e,t){console.log(""),i.success(`Projeto criado com sucesso `),i.info("Pr\xF3ximos passos:"),i.info(` cd ${e} `),t||i.info(" npm install"),i.info(" npm run dev")}function S(){console.log(d.grey.bold(" \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557")),console.log(d.greenBright.bold(d.grey.bold(" \u2551")+" Create Trybe App \u{1F606} "+d.grey.bold("\u2551"))),console.log(d.grey.bold(` \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D `))}import R from"path";import g from"fs";import{exec as I,spawnSync as ne}from"child_process";import se from"ora";function a(e,t){let r=g.readdirSync(e);for(let o of r){let s=R.join(e,o),n=R.join(t,o);g.statSync(s).isFile()?g.copyFileSync(s,n):(g.existsSync(n)||g.mkdirSync(n),a(s,n))}}function T(e,t=""){let r=t.split("/"),o=R.join(e,...r);g.unlinkSync(o)}function b(e){g.mkdirSync(e)}function E(e){I("git init",{cwd:e})}function j(e,t){ne(t,["install"],{cwd:e,stdio:"inherit"})}function ie(e){let t=setTimeout(()=>e.start("Demora um pouco mesmo \u{1F606}"),1e4),r=setTimeout(()=>e.start("Ta demorando n\xE9..."),2e4),o=setTimeout(()=>e.start("Quase l\xE1, eu prometo!"),35e3),s=setTimeout(()=>e.start("Em 3, 2, 1 e......."),5e4),n=setTimeout(()=>e.start("Poooode ser que algo esteja errado..."),75e3);return[t,r,o,s,n]}async function M(e,t="npm"){let r=I(`${t} install`,{cwd:e}),o=se(`Instalando depend\xEAncias, isso pode levar um tempinho... `).start();await new Promise((s,n)=>{let m=ie(o);r.on("error",c=>{n(c)}),r.on("close",()=>{m.forEach(c=>{clearTimeout(c)}),o.stop(),s()})})}import p from"path";import{fileURLToPath as ae}from"url";var pe=ae(import.meta.url),l=p.dirname(pe),w=p.join(l,"..","templates","base"),h=p.join(l,"..","templates","extra","react-router"),k=p.join(l,"..","templates","extra","rtl"),O=p.join(l,"..","templates","extra","rtlrouter"),H=p.join(l,"..","templates","extra","styled-base"),U=p.join(l,"..","templates","extra","styled-router"),C=p.join(l,"..","templates","extra","redux","base"),D=p.join(l,"..","templates","extra","redux","rtl"),F=p.join(l,"..","templates","extra","redux","router"),N=p.join(l,"..","templates","extra","redux","rtlrouter");var G=["npm","yarn","pnpm","bun"];async function B(){let e=await le({message:"Enter your project name >",default:"trybe-app"});return e.trim().length===0?"trybe-app":e}async function X(){await me({message:"Qual linguagem gostaria de usar ?",choices:[{name:"Typescript",value:"ts"},{name:"Javascript",value:"js"}]})==="ts"?i.info("Bela escolha, usaremos Typescript!!!"):i.info("Escolha errada, vamos utilizar Typescript...")}async function z(e){await J({message:"Gostaria de iniciar um reposit\xF3rio git ?"})?(E(e),i.success("Git iniciado!")):i.info("Tudo bem, voc\xEA pode iniciar mais tarde.")}async function Y(){let e=await J({message:"Gostaria de instalar as depend\xEAncias (npm install) ?"});return e||(i.info("Tudo bem, voc\xEA pode instalar mais tarde."),e)}async function Q(){return await de({message:"Quais pacotes adicionar ?",choices:[{name:"React Router",value:"router"},{name:"React Testing Library",value:"vitest"},{name:"Styled Components",value:"styled"},{name:"Redux",value:"redux"}]})}import y from"fs";import _ from"path";import ue from"sort-package-json";var $={"react-router-dom":"^6.15.0",vitest:"^0.34.3",jsdom:"^22.1.0","@testing-library/jest-dom":"^6.1.2","@testing-library/react":"^14.0.0","@testing-library/user-event":"^14.4.3","@types/jest":"^29.5.4","@vitest/coverage-v8":"^0.34.3","styled-components":"^6.0.7",redux:"^4.2.1","react-redux":"^8.1.2","redux-thunk":"^2.4.2","@redux-devtools/extension":"^3.2.5","@reduxjs/toolkit":"^1.9.6"},q={test:"vitest",coverage:"vitest run --coverage"};function V(e){let t=_.join(e,"package.json"),r=JSON.parse(y.readFileSync(t,{encoding:"utf-8"}));r.name=e,y.writeFileSync(t,JSON.stringify(r,null,2))}function u(e){let{dependencies:t,projectDir:r,dev:o}=e,s=_.join(r,"package.json"),n=JSON.parse(y.readFileSync(s,{encoding:"utf-8"}));t.forEach(c=>{let x=$[c];o?n.devDependencies[c]=x:n.dependencies[c]=x});let m=ue(JSON.stringify(n,null,2));y.writeFileSync(s,m)}function K(e){let{scripts:t,projectDir:r}=e,o=_.join(r,"package.json"),s=JSON.parse(y.readFileSync(o,{encoding:"utf-8"}));t.forEach(n=>{let m=q[n];s.scripts[n]=m}),y.writeFileSync(o,JSON.stringify(s,null,2))}function W(e,t){t?(a(U,e),T(e,"src/App.css"),T(e,"src/pages/Counter.css")):(a(H,e),T(e,"src/App.css")),u({dependencies:["styled-components"],projectDir:e,dev:!1})}function Z(e){a(h,e),u({dependencies:["react-router-dom"],projectDir:e,dev:!1})}async function L(e,t,r){let o=t;G.includes(t)||(i.warning(`${t} n\xE3o \xE9 um gerenciador v\xE1lido, utilizando npm no lugar.`),o="npm"),r?j(e,o):(await M(e,o),i.success(` Depend\xEAncias instaladas!`))}function ee(e,t){a(k,e),u({dependencies:["vitest","jsdom","@testing-library/jest-dom","@testing-library/react","@testing-library/user-event","@types/jest","@vitest/coverage-v8"],projectDir:e,dev:!0}),K({scripts:["test","coverage"],projectDir:e}),t&&a(O,e)}function te(e,t,r,o){a(C,e),t&&a(F,e),r&&a(D,e),r&&t&&a(N,e),o&&T(e,"src/styles.tsx"),u({dependencies:["redux","react-redux","redux-thunk","@redux-devtools/extension","@reduxjs/toolkit"],projectDir:e,dev:!1})}var v=new fe().name("create-trybe-app");v.description("Uma CLI para criar aplica\xE7\xE3o frontend com a stack da trybe");v.argument("[dir]","O nome da aplica\xE7\xE3o e a pasta a ser criada").option("-ts, --typescript","Instrui explicitamente a CLI a usar TypeScript para o desenvolvimento",!1).option("--router","Instrui explicitamente a CLI a adicionar o React Router ao projeto",!1).option("--rtl","Instrui explicitamente a CLI a adicionar o React Testing Library ao projeto",!1).option("--styled","Instrui explicitamente a CLI a adicionar Styled Components ao projeto",!1).option("--redux","Instrui explicitamente a CLI a adicionar o Redux ao projeto",!1).option("--git","Instrui a CLI a inicializar a aplica\xE7\xE3o como um reposit\xF3rio Git",!1).option("--nogit","Instrui a CLI a n\xE3o inicializar um reposit\xF3rio Git",!1).option("-i, --install <gerenciador de pacotes>","Ap\xF3s o processo, instala as depend\xEAncias do projeto usando o gerenciador de pacotes desejado",!1).option("--debug","Ativar modo de depura\xE7\xE3o",!1).parse(process.argv);async function ge(){try{S();let t=(v.args[0]??await B()).toLowerCase().replace(/\s+/g,"-").replace(/[,/\\ ]/g,""),r=v.opts();r.typescript||await X();let{router:o,rtl:s,styled:n,redux:m,install:c,git:x,nogit:re}=r;if(!(o||s||n||m)){let f=await Q();o=f.includes("router"),s=f.includes("vitest"),n=f.includes("styled"),m=f.includes("redux")}if(b(t),a(w,t),V(t),oe.renameSync(P.join(t,"_vscode"),P.join(t,".vscode")),oe.renameSync(P.join(t,"_gitignore"),P.join(t,".gitignore")),o&&Z(t),s&&ee(t,o),n&&W(t,o),m&&te(t,o,s,n),x&&E(t),!re&&!x&&await z(t),c)await L(t,c,r.debug),A(t,!0);else{let f=await Y();f&&await L(t,"npm",r.debug),A(t,f)}}catch(e){i.error(e.message)}}ge();