UNPKG

bene-mono-cli

Version:
3 lines 4.6 kB
#!/usr/bin/env node import {Command}from'commander';import s from'fs-extra';import c from'path';import u from'prompts';import b from'os';import {execa}from'execa';async function w(n){let{templatePath:e,targetPath:a}=n,i="bene-mono",t="main",o=`https://codeload.github.com/ailnk0/${i}/tar.gz/${t}`,r=c.join(b.tmpdir(),`${i}-${Date.now()}`);await s.ensureDir(r);try{let l=await fetch(o);if(!l.ok)throw new Error(`Failed to download template: ${l.statusText}`);let h=c.resolve(r,"template.tar.gz");await s.writeFile(h,Buffer.from(await l.arrayBuffer())),await execa("tar",["-xzf",h,"-C",r,"--strip-components=2",`${i}-${t}/templates/${e}`]);let k=c.resolve(r,e);await s.move(k,a);}finally{await s.remove(r);}}function f(n){let e=n;for(;e!==c.parse(e).root;){if(s.existsSync(c.join(e,"pnpm-workspace.yaml")))return e;e=c.dirname(e);}return s.existsSync(c.join(e,"pnpm-workspace.yaml"))?e:null}async function d(n,e,a){if(e===a)return;let i=await s.readdir(n,{withFileTypes:true});for(let t of i){let o=c.join(n,t.name);if(t.isDirectory())t.name!=="node_modules"&&t.name!==".git"&&t.name!=="dist"&&await d(o,e,a);else if(t.isFile())try{let r=await s.readFile(o,"utf-8");if(r.includes(e)){let l=r.replace(new RegExp(e,"g"),a);await s.writeFile(o,l,"utf-8");}}catch{}}}var g=async()=>{try{let n=process.cwd();console.log(n);let e=f(n);e?(console.log(`Monorepo root detected: ${e}`),console.log("A new app will be created within the existing monorepo."),await P(e)):await T(n);}catch(n){console.error(n);}},y=new Command().name("init").description("Initialize a monorepo project").action(g),v=[{title:"Turbo v2 | Vite v7 | React v19 | Tailwind v4 | shadcn (Default)",value:"turbo2-vite7-react19-tailwind4-shadcn"},{title:"Turbo v2 | Vite v7 | React v19 | Tailwind v4 | shadcn | storybook",value:"turbo2-vite7-react19-tailwind4-shadcn-storybook"},{title:"Turbo v2 | Next v15 | React v19 | Tailwind v4 | shadcn",value:"turbo2-next15-react19-tailwind4-shadcn"}],T=async n=>{let{templateType:e,projectName:a,workspaceAlias:i}=await u([{type:"select",name:"templateType",message:"Select a template for the new monorepo:",choices:v,initial:0},{type:"text",name:"projectName",message:"Project name:",initial:"my-monorepo",format:o=>o.trim(),validate:o=>o.length>128?"Name should be less than 128 characters.":true},{type:"text",name:"workspaceAlias",message:"Enter the workspace alias:",initial:"@workspace"}]);console.log(`Selected template: ${e}`),console.log(`Project name: ${a}`),(!e||!a||!i)&&(console.log("Template type, project name and workspace alias are required. Exiting."),process.exit(1));let t=c.join(n,a);s.existsSync(c.resolve(n,a,"package.json"))&&(console.error(`A project with the name ${a} already exists.`),process.exit(1)),console.log(`Creating a new monorepo project '${a}' in ${t}...`);try{await w({templatePath:e,targetPath:t});let o=c.join(t,"package.json"),r=await s.readJson(o);r.name=a,await s.writeJson(o,r,{spaces:2}),await d(t,"@workspace",i),console.log(`Monorepo Project '${a}' created successfully.`);}catch(o){console.error("Error creating monorepo project:",o),await s.remove(t);}};async function P(n){let{templateType:e,appName:a,workspaceAlias:i}=await u([{type:"select",name:"templateType",message:"Select a template for the new app:",choices:v,initial:0},{type:"text",name:"appName",message:"What is the name of the new app?",initial:"my-app",format:o=>o.trim(),validate:o=>o.length>128?"Name should be less than 128 characters.":true},{type:"text",name:"workspaceAlias",message:"Enter the workspace alias:",initial:"@workspace"}]);console.log(`Selected template: ${e}`),console.log(`App name: ${a}`),(!a||!e||!i)&&(console.log("Template type, app name and workspace alias are required. Exiting."),process.exit(1));let t=c.join(n,"apps",a);s.existsSync(t)&&(console.error(`Error: App directory '${t}' already exists.`),process.exit(1)),console.log(`Creating a new app '${a}' in '${t}'...`);try{await w({templatePath:`${e}/apps/web`,targetPath:t});let o=c.join(t,"package.json"),r=await s.readJson(o);r.name=a,await s.writeJson(o,r,{spaces:2}),await d(t,"@workspace",i),console.log(`App '${a}' created successfully.`);}catch(o){console.error("Error creating app:",o),await s.remove(t);}}var x={version:"0.0.20"};process.on("SIGINT",()=>process.exit(0));process.on("SIGTERM",()=>process.exit(0));async function C(){let n=new Command().name("bene-mono-cli").description("A CLI to generate monorepo").version(x.version,"-v, --version","the version number").action(g);n.addCommand(y),n.parse(process.argv);}C();//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map