UNPKG

create-abi

Version:

Interactive CLI for create Abi.js projects.

3 lines (2 loc) 8.34 kB
import{b as C}from"./chunk-SH2VV5O3.js";import{F as k}from"./chunk-5AAY5SUW.js";import{b as l,c as v,p as m,q as h,r as y,s as I,t as g,u as R,v as f,w as $,z as S}from"./chunk-TP6KBC4Q.js";import j from"fs";import d from"path";import{copySync as A,ensureDirSync as E,pathExistsSync as z}from"fs-extra/esm";import{$ as D}from"panam/executor";import o from"panam/pm";var b={name:"create-abi",type:"module",license:"MIT",version:"0.0.0",description:"Interactive CLI for create Abi.js projects.",scripts:{check:"tsc --noEmit",build:"tsup-node --env.NODE_ENV production",prod:"pnpm check && pnpm build",start:"tsup-node --env.NODE_ENV development --watch",test:"pnm tsx bin/test.ts",tsx:"tsx"},contributors:[{name:"Sigui Kess\xE9 Emmanuel",email:"contact@sigui.ci",url:"https://twitter.com/siguici"}],repository:{type:"git",url:"https://github.com/abi-js/abi",directory:"packages/create-abi"},types:"./dist/index.d.ts",main:"./dist/index.js",module:"./dist/index.js",exports:{".":{types:"./dist/index.d.ts",import:"./dist/index.js",require:"./dist/index.cjs",default:"./dist/index.js"},"./app":{types:"./dist/app.d.ts",import:"./dist/app.js",require:"./dist/app.cjs",default:"./dist/app.js"},"./cli":{types:"./dist/cli.d.ts",import:"./dist/cli.js",require:"./dist/cli.cjs",default:"./dist/cli.js"},"./console":{types:"./dist/console.d.ts",import:"./dist/console.js",require:"./dist/console.cjs",default:"./dist/console.js"},"./core":{types:"./dist/core.d.ts",import:"./dist/core.js",require:"./dist/core.cjs",default:"./dist/core.js"},"./tester":{types:"./dist/tester.d.ts",import:"./dist/tester.js",require:"./dist/tester.cjs",default:"./dist/tester.js"},"./utils":{types:"./dist/utils.d.ts",import:"./dist/utils.js",require:"./dist/utils.cjs",default:"./dist/utils.js"},"./package.json":"./package.json"},files:["dist","starters"],bin:"./dist/cli.js",keywords:["abi","abi-js","abi.js","cli","console","create-abi","generator","skeleton","starter-kit","template"],publishConfig:{access:"public"},bugs:"https://github.com/@abi-js/abi/issues",dependencies:{"@clack/prompts":"^0.11.0","cross-spawn":"^7.0.6","fs-extra":"^11.3.0",kleur:"^4.1.5",panam:"^0.3.0",tsx:"^4.20.3",which:"^5.0.0","which-pm-runs":"^1.1.0",yargs:"^18.0.0"},devDependencies:{"@japa/assert":"^4.0.1","@japa/runner":"^4.2.0","@types/cross-spawn":"^6.0.6","@types/fs-extra":"^11.0.4","@types/node":"^24.0.10","@types/which":"^3.0.4","@types/which-pm-runs":"^1.0.2","@types/yargs":"^17.0.33",rimraf:"^6.0.1",tsup:"^8.5.0",typescript:"^5.8.3"}};var a={destination:"./abi-app",runtime:"node",force:void 0,install:void 0,git:void 0,yes:void 0,no:void 0,dryRun:void 0};function B(c){return{...a,...c}}var w=class extends C{configure(){this.strict().interactive().alias("h","help").useYes().useNo().command("* [destination] [runtime]","Create a new project powered by Abi.js").argument("destination",{type:"string",default:a.destination,desc:"Directory of the project"}).argument("runtime",{type:"string",default:a.runtime,desc:"JavaScript/TypeScript runtime to use",choices:["node","deno","bun"]}).option("force",{alias:"f",type:"boolean",default:a.force,desc:"Overwrite target directory if it exists"}).option("install",{alias:"i",type:"boolean",default:a.install,desc:"Install dependencies"}).option("git",{type:"boolean",default:a.git,desc:"Use Git to save changes"}).option("dryRun",{type:"boolean",desc:"Walk through steps without executing"}).example("npm create abi@latest","Create a project with default options").example("npm create abi@latest ./abi-app","Create a project in a specific directory").example("npm create abi@latest ./abi-app node","Create a project using a server runtime").example("npm create abi@latest ./abi-app node --it","Create a project in interactive command mode").usage("npm create abi [destination] [runtime] [...options]")}parse(e){return B(super.parse(e))}validate(e){return{destination:e.destination,runtime:e.runtime,force:e.force??(!!e.yes&&!e.no),install:e.install??(!!e.yes&&!e.no),git:e.git??(!!e.yes&&!e.no),dryRun:!!e.dryRun,outDir:m(e.destination),packageName:f(e.destination)}}async interact(e){let t=e.destination===a.destination?await this.scanString(`Where would you like to create your new project? ${this.gray("(Use './' for current directory)")}`,e.destination):e.destination,i=m(t.trim()),s=y(i),r=e.force===void 0?s&&!!await this.scanBoolean(e,`Directory "./${h(i)}" already exists and is not empty. Would you like to force the copy?`,!1):e.force,n;if(e.runtime===a.runtime){let x=await this.scanBoolean(e,"Would you like to use another runtime instead of Node.js?",!1)&&await this.scanChoice("Which runtime do you prefer?",[{value:"node",label:"Node"},{value:"deno",label:"Deno"},{value:"bun",label:"Bun"}],e.runtime)||"node";k(x,W=>["node","deno","bun"].includes(W)),n=x}else n=e.runtime;let N=!!(e.install===void 0?await this.scanBoolean(e,`Would you like to install ${o.name} dependencies?`):e.install),P=!!(e.git===void 0?await this.scanBoolean(e,!s||r?"Would you like to initialize Git?":"Would you like to save the changes with Git?"):e.git),G=!!e.dryRun,u="";s&&r&&(j.existsSync(g("deno",i))?u="deno":j.existsSync(g("package",i))&&(u="package"));let p=u!==""?$(u,i).name:f(t);return p=e.yes?p:await this.scanString("What should be the name of this package?",p),{destination:t,runtime:n,install:N,git:P,force:r,outDir:i,packageName:p,dryRun:G}}async execute(e){try{let t=await this.start(e);return this.updatePackageJson(e),await this.runGit(e),this.end(e,t),0}catch(t){return console.error("An error occurred during Abi.js project creation:",t),1}}async prepareDir(e){let t=e.outDir;y(t)&&(e.force?(e.dryRun||await I(t),this.info(`Directory "${t}" successfully emptied \u{1F525}`)):(this.error(`Directory "${t}" already exists.`),this.info("Please either remove this directory, choose another location or run the command again with '--force | -f' flag."),this.cancel(),process.exit(1)))}async runCreate(e){await this.prepareDir(e),this.copyStarter(e)}async start(e){return this.intro(`Let's create a ${this.bgYellow(" Abi.js")} App \u2728`),await this.runCreate(e),this.runInstall(e)}end(e,t){let i=e.outDir,s=process.cwd()===i,r=h(i),n=[];s?n.push(`\u{1F984} ${this.bgMagenta(" Success! ")}`):n.push(`\u{1F984} ${this.bgMagenta(" Success! ")} ${this.cyan("Project created in")} ${this.bold(this.magenta(r))} ${this.cyan("directory")}`),n.push(""),n.push(`\u{1F430} ${this.cyan("Next steps:")}`),s||n.push(` cd ${r}`),t||n.push(` ${o.name} install`),n.push(` ${o.name} start`),this.note(n.join(` `),"Ready to start \u{1F680}"),this.outro("Happy coding! \u{1F4BB}\u{1F389}")}updatePackageJson(e){let{outDir:t,packageName:i}=e,s=e.runtime==="deno"?"deno":"package";S(i,s,t),this.info(`Updated package name to "${i}" \u{1F4E6}\uFE0F`),o.isNpm()||(this.info(`Replacing 'npm run' by '${o.runCommand()}' in package.json...`),R(s,t))}async runInstall(e){let t=!1;return e.install&&(this.step("Installing dependencies..."),e.dryRun||await o.install({cwd:e.outDir}),t=!0),t}async runGit(e){if(e.git){let t=this.spinner(),i=e.outDir,s=j.existsSync(d.join(i,".git"));if(s&&this.info("Git has already been initialized before."),t.start("Initializing Git..."),!e.dryRun){let r=[];try{if(s||r.push(await D("git",["init"],{cwd:i}).result),r.push(await D("git",["add","-A"],{cwd:i}).result),r.push(await D("git",["commit","-m","Initial commit \u{1F389}"],{cwd:i}).result),r.some(n=>n.status===!1))throw"";t.stop("Git initialized \u{1F3B2}")}catch{t.stop("Git failed to initialize"),s?this.error("Git failed to add new changes. You can do this manually by running: git add -A && git commit"):this.error("Git failed to initialize. You can do this manually by running: git init")}}}}copyShared(e){for(let t of["gitignore"]){let i=t;t==="gitignore"&&(i=".gitignore");let s=d.join(e.outDir,i),r=z(s);if(t.startsWith(".")&&t.endsWith("ignore")&&this.step(`${r?"Merging":"Copying"} \`${i}\` file... \u{1F648}`),!e.dryRun){let n=d.join(l,"..","starters",t);v(n,s)}}}copyStarter(e,t){if(this.step(`Creating new project in ${this.bgBlue(` ${e.outDir} `)} ... \u{1F407}`),!e.dryRun){let i=e.outDir;try{E(i),t||(t=d.join(l,"..","starters",e.runtime)),A(t,i),this.copyShared(e)}catch(s){this.error(this.red(`Template copy failed: ${s}`))}}}};function T(c=b.name,e=b.version){return new w(c,e)}var H=T();export{a,B as b,w as c,T as d,H as e};