@h3ravel/console
Version:
CLI utilities for scaffolding, running migrations, tasks and for H3ravel.
77 lines (75 loc) • 19.8 kB
JavaScript
import"./zero-Ddh7ucIS.js";import"tsx/esm";import{Application as e,ConsoleCommand as t,ConsoleKernel as n,ServiceProvider as r}from"@h3ravel/core";import{FileSystem as i,Logger as a,TaskManager as o}from"@h3ravel/shared";import{execa as s}from"execa";import c from"preferred-pm";import{glob as l,mkdir as u,readFile as d,rm as f,writeFile as p}from"node:fs/promises";import{beforeLast as m}from"@h3ravel/support";import h from"dayjs";import g from"node:path";import{Option as _,program as v}from"commander";import{existsSync as y}from"node:fs";import{fork as b}from"child_process";import{dirname as x,join as S,resolve as C}from"path";import{build as w}from"tsdown";var T=class extends t{signature=`build:
{--m|minify=false : Minify your bundle output}
`;description=`Build the app for production`;async handle(){try{await this.fire()}catch(e){a.error(e)}}async fire(){let e=env(`DIST_DIR`,`dist`),t=(await c(base_path()))?.name??`pnpm`,n=this.option(`minify`),r={EXTENDED_DEBUG:Number(this.option(`verbose`,0))>0?`true`:`false`,CLI_BUILD:`true`,NODE_ENV:`production`,DIST_DIR:e,DIST_MINIFY:n,LOG_LEVEL:[`silent`,`info`,`warn`,`error`][Number(this.option(`verbose`,0))]},i=r.LOG_LEVEL===`silent`?`--silent`:null;a.log([[`
INFO `,`bgBlue`],[` Creating Production Bundle`,`white`]],``),console.log(``),await o.taskRunner(a.log([[` SUCCESS `,`bgGreen`],[` Production Bundle Created`,`white`]],``,!1),async()=>{await s(t,[`tsdown`,i,`--config-loader`,`unconfig`,`-c`,`tsdown.default.config.ts`].filter(e=>e!==null),{stdout:`inherit`,stderr:`inherit`,cwd:base_path(),env:Object.assign({},process.env,r)}),console.log(``)})}},E=class extends t{};String.raw`
111
111111111
1111111111 111111
111111 111 111111
111111 111 111111
11111 111 11111
1111111 111 1111111
111 11111 111 111111 111 1111 1111 11111111 1111
111 11111 1111 111111 111 1111 1111 1111 11111 1111
111 11111 11111 111 1111 1111 111111111111 111111111111 1111 1111111 1111
111 111111 1111 111 111111111111 111111 11111 1111 111 1111 11111111 1111 1111
111 111 11111111 111 1101 1101 111111111 11111111 1111 1111111111111111101
111 1111111111111111 1111 111 1111 1111 111 11111011 1111 111 1111111 1101 1111
111 11111 1110111111111111 111 1111 1111 1111111101 1111 111111111 1111011 111111111 1111
1111111 111110111110 111 1111 1111 111111 1111 11011101 10111 11111 1111
11011 111111 11 11111
111111 11101 111111
111111 111 111111
111111 111 111111
111111111
110
`;const D=String.raw`
_ _ _____ _
| | | |___ / _ __ __ ___ _____| |
| |_| | |_ \| '__/ _ \ \ / / _ \ |
| _ |___) | | | (_| |\ V / __/ |
|_| |_|____/|_| \__,_| \_/ \___|_|
`;var O=class extends E{signature=`list`;description=`List all available commands`;async handle(){let e=[{short:`-h`,long:`--help`,description:`Display help for the given command. When no command is given display help for the list command`}].concat(this.program.options).map(e=>a.describe(a.log(` `+[e.short,e.long].filter(e=>!!e).join(`, `),`green`,!1),e.description,25,!1).join(``)),t=this.program.commands.map(e=>a.describe(a.log(` `+e.name(),`green`,!1),e.description(),25,!1).join(``)).reduce((e,t)=>{let n=t.replace(/\x1b\[\d+m/g,``),r=n.includes(`:`)?n.split(`:`)[0].trim():`__root__`;return e[r]??=[],e[r].push(t),e},{}),n=Object.entries(t).map(([e,t])=>{let n=e===`__root__`?``:e;return[a.log(n,`yellow`,!1),t.join(`
`)].join(`
`)});a.log([[`H3ravel Framework`,`white`],[this.kernel.modulePackage.version,`green`]],` `),console.log(``),console.log(D),console.log(``),a.log(`Usage:`,`yellow`),a.log(` command [options] [arguments]`,`white`),console.log(``),a.log(`Options:`,`yellow`),console.log(e.join(`
`).trim()),console.log(``),a.log(`Available Commands:`,`yellow`),console.log(n.join(`
`).trim())}},k=class e{static CREATE_PATTERNS=[/^create_(\w+)_table$/,/^create_(\w+)$/];static CHANGE_PATTERNS=[/.+_(to|from|in)_(\w+)_table$/,/.+_(to|from|in)_(\w+)$/];static guess(t){for(let n of e.CREATE_PATTERNS){let e=t.match(n);if(e)return[e[1],!0]}for(let n of e.CHANGE_PATTERNS){let e=t.match(n);if(e)return[e[2],!1]}return[]}},A=class extends E{signature=`#make:
{controller : Create a new controller class.
| {--a|api : Exclude the create and edit methods from the controller}
| {--m|model= : Generate a resource controller for the given model}
| {--r|resource : Generate a resource controller class}
| {--force : Create the controller even if it already exists}
}
{resource : Create a new resource.
| {--c|collection : Create a resource collection}
| {--force : Create the resource even if it already exists}
}
{migration : Generates a new database migration class.
| {--l|type=ts : The file type to generate}
| {--t|table : The table to migrate}
| {--c|create : The table to be created}
}
{command : Create a new Musket command.
| {--command : The terminal command that will be used to invoke the class}
| {--force : Create the class even if the console command already exists}
}
{factory : Create a new model factory.}
{seeder : Create a new seeder class.}
{view : Create a new view.
| {--force : Create the view even if it already exists}
}
{model : Create a new Eloquent model class.
| {--api : Indicates if the generated controller should be an API resource controller}
| {--c|controller : Create a new controller for the model}
| {--f|factory : Create a new factory for the model}
| {--m|migration : Create a new migration file for the model}
| {--r|resource : Indicates if the generated controller should be a resource controller}
| {--a|all : Generate a migration, seeder, factory, policy, resource controller, and form request classes for the model}
| {--s|seed : Create a new seeder for the model}
| {--t|type=ts : The file type to generate}
| {--force : Create the model even if it already exists}
}
{^name : The name of the [name] to generate}
`;description=`Generate component classes`;async handle(){let e=this.dictionary.baseCommand??this.dictionary.name;this.argument(`name`)||this.program.error(`Please provide a valid name for the `+e),await this[{controller:`makeController`,resource:`makeResource`,migration:`makeMigration`,factory:`makeFactory`,seeder:`makeSeeder`,model:`makeModel`,view:`makeView`,command:`makeCommand`}[e]]()}async makeController(){let e=this.option(`api`)?`-resource`:``,t=this.argument(`name`),n=this.option(`force`),r=i.findModulePkg(`@h3ravel/http`,this.kernel.cwd)??``,o=g.join(r,`dist/stubs/controller${e}.stub`),s=app_path(`Http/Controllers/${t}.ts`);t.includes(`/`)&&await u(m(s,`/`),{recursive:!0}),!n&&await i.fileExists(s)&&a.error(`ERORR: ${t} controller already exists`);let c=await d(o,`utf-8`);c=c.replace(/{{ name }}/g,t),await p(s,c),a.split(`INFO: Controller Created`,a.log(g.basename(s),`gray`,!1))}makeResource(){a.success(`Resource support is not yet available`)}async makeMigration(){let e=this.argument(`name`),t=h().format(`YYYY_MM_DD_HHmmss`),n=database_path(`migrations/${t}_${e}.ts`),r=i.findModulePkg(`@h3ravel/database`,this.kernel.cwd)??``,o=this.option(`create`,!1),s=this.option(`table`);if(!s&&typeof o==`string`&&(s=o,o=!0),!s){let t=k.guess(e);s=t[0],o=!!t[1]}let c=g.join(r,this.getMigrationStubName(s,o)),l=await d(c,`utf-8`);s!==null&&(l=l.replace(/DummyTable|{{\s*table\s*}}/g,s)),a.info(`INFO: Creating Migration`),await this.kernel.ensureDirectoryExists(g.dirname(n)),await p(n,l),a.split(`INFO: Migration Created`,a.log(g.basename(n),`gray`,!1))}makeFactory(){a.success(`Factory support is not yet available`)}makeCommand(){a.success(`Musket command creation is not yet available`)}makeSeeder(){a.success(`Seeder support is not yet available`)}async makeModel(){let e=this.option(`type`,`ts`),t=this.argument(`name`),n=this.option(`force`),r=app_path(`Models/${t.toLowerCase()}.${e}`);t.includes(`/`)&&await u(m(r,`/`),{recursive:!0}),!n&&await i.fileExists(r)&&a.error(`ERORR: ${t} model already exists`);let o=i.findModulePkg(`@h3ravel/database`,this.kernel.cwd)??``,s=g.join(o,`dist/stubs/model-${e}.stub`),c=await d(s,`utf-8`);c=c.replace(/{{ name }}/g,t),await p(r,c),a.split(`INFO: ${t} Model Created`,a.log(g.basename(r),`gray`,!1))}async makeView(){let e=this.argument(`name`),t=this.option(`force`),n=base_path(`src/resources/views/${e}.edge`);e.includes(`/`)&&await u(m(n,`/`),{recursive:!0}),!t&&await i.fileExists(n)&&a.error(`ERORR: ${e} view already exists`),await p(n,`{{-- src/resources/views/${e}.edge --}}`),a.split(`INFO: View Created`,a.log(`src/resources/views/${e}.edge`,`gray`,!1))}getMigrationStubName(e,t=!1,n=`ts`){let r;return r=e?t?`migration.create-${n}.stub`:`migration.update-${n}.stub`:`migration-${n}.stub`,`dist/stubs/`+r}},j=class extends E{signature=`postinstall`;description=`Default post installation command`;async handle(){this.createSqliteDB()}async createSqliteDB(){config(`database.default`)===`sqlite`&&(await i.fileExists(database_path())||await u(database_path(),{recursive:!0}),await i.fileExists(database_path(`db.sqlite`))||await p(database_path(`db.sqlite`),``))}},M=class e{static parseOptions(t){let n=[],r=/\{([^{}]+(?:\{[^{}]*\}[^{}]*)*)\}/g,i;for(;(i=r.exec(t))!==null;){let t=i[1][0]===`^`||/:[#^]/.test(i[1]),r=([`#`,`^`].includes(i[1][0])||/:[#^]/.test(i[1]))&&!t,a=i[1].trim().replace(/[#^]/,``),o=a.indexOf(`:`);if(o===-1){n.push({name:a});continue}let s=a.substring(0,o).trim(),c=a.substring(o+1).trim(),l=c,u,d=c.indexOf(`|`);if(d!==-1){l=c.substring(0,d).trim();let t=c.substring(d+1).trim().replace(/^\{/,``).trim();u=e.parseOptions(`{`+t+`}`)}else l=l.trim();let f=s,p=/[^a-zA-Z0-9_|-]/.test(f),m=!1;f.endsWith(`?*`)?(p=!1,m=!0,f=f.slice(0,-2)):f.endsWith(`*`)?(m=!0,f=f.slice(0,-1)):f.endsWith(`?`)&&(p=!1,f=f.slice(0,-1));let h=f.startsWith(`--`),g,_;if(h){let e=f.split(`|`).map(e=>e.trim());g=[];for(let t of e){t.startsWith(`--`)&&t.slice(2).length===1?t=`-`+t.slice(2):t.startsWith(`-`)&&!t.startsWith(`--`)&&t.slice(1).length>1?t=`--`+t.slice(1):!t.startsWith(`-`)&&t.slice(1).length>1&&(t=`--`+t);let e=t.indexOf(`=`);if(e!==-1){g.push(t.substring(0,e));let n=t.substring(e+1);_=n===`*`?[]:n===`true`||n===`false`||!n&&!p?n===`true`:isNaN(Number(n))?n:Number(n)}else g.push(t)}}n.push({name:h?g[g.length-1]:f,required:p,multiple:m,description:l,flags:g,shared:t,isFlag:h,isHidden:r,defaultValue:_,nestedOptions:u})}return n}static parseSignature(t,n){let r=t.split(`
`).map(e=>e.trim()).filter(e=>e.length>0),i=[`#`,`^`].includes(r[0][0])||/:[#^]/.test(r[0]),a=r[0].replace(/[^\w=:-]/g,``),o=n.getDescription(),s=a.endsWith(`:`),c=r.slice(1).join(` `),l=e.parseOptions(c);return s?{baseCommand:a.slice(0,-1),isNamespaceCommand:s,subCommands:l.filter(e=>!e.flags&&!e.isHidden),description:o,commandClass:n,options:l.filter(e=>!!e.flags),isHidden:i}:{baseCommand:a,isNamespaceCommand:s,options:l,description:o,commandClass:n,isHidden:i}}};function N(e={}){let t,n,r=e.args||[],i=e.allowRestarts||!1,a=e.input,o=e.options||e;return delete o.args,delete o.allowRestarts,{name:`run`,buildStart(e){let n=a??e.input;if(typeof n==`string`&&(n=[n]),typeof n==`object`&&(n=Object.values(n)),n.length>1)throw Error("@rollup/plugin-run must have a single entry point; consider setting the `input` option");t=C(n[0])},generateBundle(e,t,n){n||this.error(`@rollup/plugin-run currently only works with bundles that are written to disk`)},writeBundle(e,a){let s=(e,t)=>{n&&n.kill(),n=b(S(e,t),r,o)},c=e.dir||x(e.file),l=Object.keys(a).find(e=>{let n=a[e];return n.isEntry&&n.facadeModuleId===t});l?(s(c,l),i&&(process.stdin.resume(),process.stdin.setEncoding(`utf8`),process.stdin.on(`data`,e=>{let t=e.toString().trim().toLowerCase();t===`rs`||t===`restart`||e.toString().charCodeAt(0)===11?s(c,l):(t===`cls`||t===`clear`||e.toString().charCodeAt(0)===12)&&console.clear()}))):this.error(`@rollup/plugin-run could not find output chunk`)}}}const P=process.env.NODE_ENV||`development`;let F=P===`development`?`.h3ravel/serve`:`dist`;process.env.DIST_DIR&&(F=process.env.DIST_DIR);var I={outDir:F,entry:[`src/**/*.ts`],format:[`esm`],target:`node22`,sourcemap:P===`development`,minify:!!process.env.DIST_MINIFY,external:[/^@h3ravel\/.*/gi],clean:!0,shims:!0,copy:[{from:`public`,to:F},`src/resources`,`src/database`],env:P===`development`?{NODE_ENV:P,DIST_DIR:F}:{},watch:P===`development`&&process.env.CLI_BUILD!==`true`?[`.env`,`.env.*`,`src`,`../../packages`]:!1,dts:!1,logLevel:`silent`,nodeProtocol:!0,skipNodeModulesBundle:!0,hooks(e){e.hook(`build:done`,async()=>{let e=[`database/migrations`,`database/factories`,`database/seeders`];for(let t=0;t<e.length;t++){let n=e[t];y(g.join(F,n))&&await f(g.join(F,n),{recursive:!0})}})},plugins:P===`development`&&process.env.CLI_BUILD!==`true`?[N({env:Object.assign({},process.env,{NODE_ENV:P,DIST_DIR:F}),execArgv:[`-r`,`source-map-support/register`],allowRestarts:!1,input:process.cwd()+`/src/server.ts`})]:[]},L=class e{commands=[];constructor(e,t){this.app=e,this.kernel=t}async build(){return this.loadBaseCommands(),await this.loadDiscoveredCommands(),this.initialize()}loadBaseCommands(){[new A(this.app,this.kernel),new O(this.app,this.kernel),new j(this.app,this.kernel),new T(this.app,this.kernel)].forEach(e=>this.addCommand(e))}async loadDiscoveredCommands(){let e=`/${env(`DIST_DIR`,`.h3ravel/serve`)}/`.replaceAll(`//`,``),t=[...this.app.registeredCommands.map(e=>new e(this.app,this.kernel))],n=app_path(`Console/Commands/*.js`).replace(`/src/`,e);for await(let e of l(n)){let n=g.basename(e).replace(`.js`,``);try{let r=(await import(e))[n];t.push(new r(this.app,this.kernel))}catch{}}t.forEach(e=>this.addCommand(e))}addCommand(e){this.commands.push(M.parseSignature(e.getSignature(),e))}initialize(){let e=a.parse([[`Musket CLI:`,`white`],[this.kernel.consolePackage.version,`green`]],` `,!1),t=a.parse([[`H3ravel Framework:`,`white`],[this.kernel.modulePackage.version,`green`]],` `,!1),n={quiet:[`-q, --quiet`,`Do not output any message`],silent:[`--silent`,`Do not output any message`],verbose:[`-v, --verbose <number>`,`Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug`],lock:[`--lock`,`Locked and loaded, do not ask any interactive question`]};v.name(`musket`).version(`${e}\n${t}`).description(D).addOption(new _(n.quiet[0],n.quiet[1]).implies({silent:!0})).addOption(new _(n.silent[0],n.silent[1]).implies({quiet:!0})).addOption(new _(n.verbose[0],n.verbose[1]).choices([`1`,`2`,`3`])).addOption(new _(n.lock[0],n.lock[1])).action(async()=>{let e=new O(this.app,this.kernel);e.setInput(v.opts(),v.args,v.registeredArguments,{},v),e.handle()}),v.command(`init`).description(`Initialize H3ravel.`).action(async()=>{a.success(`Initialized: H3ravel has been initialized!`)});for(let e=0;e<this.commands.length;e++){let t=this.commands[e],r=t.commandClass;if(t.isNamespaceCommand&&t.subCommands){let e=t.isHidden?v:v.command(t.baseCommand).description(t.description??``).addOption(new _(n.quiet[0],n.quiet[1]).implies({silent:!0})).addOption(new _(n.silent[0],n.silent[1]).implies({quiet:!0})).addOption(new _(n.verbose[0],n.verbose[1]).choices([`1`,`2`,`3`])).addOption(new _(n.lock[0],n.lock[1])).action(async()=>{r.setInput(e.opts(),e.args,e.registeredArguments,t,v),await r.handle()});(t.options?.length??0)>0&&t.options?.filter((e,t,n)=>n.findIndex(t=>t.name===e.name)===t).forEach(t=>{this.makeOption(t,e)}),t.subCommands.filter((e,t,n)=>!e.shared&&n.findIndex(t=>t.name===e.name)===t).forEach(e=>{let i=v.command(`${t.baseCommand}:${e.name}`).description(e.description||``).addOption(new _(n.quiet[0],n.quiet[1]).implies({silent:!0})).addOption(new _(n.silent[0],n.silent[1]).implies({quiet:!0})).addOption(new _(n.verbose[0],n.verbose[1]).choices([`1`,`2`,`3`])).addOption(new _(n.lock[0],n.lock[1])).action(async()=>{r.setInput(i.opts(),i.args,i.registeredArguments,e,v),await r.handle()});t.subCommands?.filter(e=>e.shared).forEach(t=>{this.makeOption(t,i,!1,e)}),t.options?.filter(e=>e.shared).forEach(t=>{this.makeOption(t,i,!1,e)}),e.nestedOptions&&e.nestedOptions.filter((e,t,n)=>n.findIndex(t=>t.name===e.name)===t).forEach(e=>{this.makeOption(e,i)})})}else{let e=v.command(t.baseCommand).description(t.description??``);t?.options?.filter((e,t,n)=>n.findIndex(t=>t.name===e.name)===t).forEach(t=>{this.makeOption(t,e,!0)}),e.action(async()=>{r.setInput(e.opts(),e.args,e.registeredArguments,t,v),await r.handle()})}}return v.hook(`preAction`,async(e,t)=>{t.name()!==`fire`&&await w({...I,watch:!1,plugins:[]})}),v}makeOption(e,t,n,r){let i=e.description?.replace(/\[(\w+)\]/g,(e,t)=>r?.[t]??`[${t}]`)??``,a=e.name.replaceAll(`-`,``);if(e.isFlag)if(n){let n=e.flags?.map(e=>e.length===1?`-${e}`:`--${e}`).join(`, `).replaceAll(`----`,`--`).replaceAll(`---`,`-`);t.option(n||``,i,String(e.defaultValue)||void 0)}else t.option(e.flags?.join(`, `)+(e.required?` <${a}>`:``),i,e.defaultValue);else t.argument(e.required?`<${e.name}>`:`[${e.name}]`,i,e.defaultValue)}static async parse(t){return(await new e(t.app,t).build()).parseAsync()}},R=class e extends n{constructor(e){super(e),this.app=e}static init(t){let n=new e(t);Promise.all([n.loadRequirements()]).then(([e])=>e.run())}async run(){await L.parse(this),process.exit(0)}async loadRequirements(){this.cwd=g.join(process.cwd(),this.basePath),this.modulePath=i.findModulePkg(`@h3ravel/core`,this.cwd)??``,this.consolePath=i.findModulePkg(`@h3ravel/console`,this.cwd)??``;try{this.modulePackage=await import(g.join(this.modulePath,`package.json`))}catch{this.modulePackage={version:`N/A`}}try{this.consolePackage=await import(g.join(this.consolePath,`package.json`))}catch{this.consolePackage={version:`N/A`}}return this}},z=class extends r{static priority=992;static console=!0;console=!0;register(){}boot(){R.init(this.app),process.on(`SIGINT`,()=>{process.exit(0)}),process.on(`SIGTERM`,()=>{process.exit(0)})}};new class{async fire(){let t=process.env.DIST_DIR??`/.h3ravel/serve/`,n=[],r=new e(process.cwd()),i=(base_path(g.join(t,`bootstrap/providers.js`)));n.push(...(await import(i)).default),n.push(z),await r.quickStartup(n,[`CoreServiceProvider`])}}().fire();export{};