Skip to content

Commit

Permalink
refactor: commands description, flags and aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 20, 2023
1 parent 4a98cae commit 45d8134
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 125 deletions.
12 changes: 4 additions & 8 deletions commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import { BaseCommand, flags } from '../modules/ace/main.js'
import { detectAssetsBundler, importAssembler, importTypeScript } from '../src/internal_helpers.js'

/**
* Serve command is used to run the AdonisJS HTTP server during development. The
* command under the hood runs the "bin/server.ts" file and watches for file
* system changes
* Create the production build by compiling TypeScript source and the
* frontend assets
*/
export default class Build extends BaseCommand {
static commandName = 'build'
Expand All @@ -33,16 +32,13 @@ export default class Build extends BaseCommand {
'```',
]

@flags.boolean({ description: 'Watch filesystem and restart the HTTP server on file change' })
declare watch?: boolean

@flags.boolean({ description: 'Ignore TypeScript errors and continue with the build process' })
declare ignoreTsErrors?: boolean

@flags.string({
description: 'Select the package manager you want to use to install production dependencies',
description: 'Define the package manager to copy the appropriate lock file',
})
declare packageManager?: 'npm' | 'pnpm' | 'yarn'
declare packageManager?: 'npm' | 'pnpm' | 'yarn' | 'bun'

@flags.boolean({
description: 'Build frontend assets',
Expand Down
6 changes: 3 additions & 3 deletions commands/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { args, BaseCommand, flags } from '../modules/ace/main.js'
*/
export default class Configure extends BaseCommand {
static commandName = 'configure'
static description = 'Configure a package post installation'
static description = 'Configure a package after it has been installed'
static options: CommandOptions = {
allowUnknownFlags: true,
}
Expand Down Expand Up @@ -44,13 +44,13 @@ export default class Configure extends BaseCommand {
/**
* Turn on verbose mode for packages installation
*/
@flags.boolean({ description: 'Display logs in verbose mode' })
@flags.boolean({ description: 'Display logs in verbose mode', alias: 'v' })
declare verbose?: boolean

/**
* Forcefully overwrite existing files.
*/
@flags.boolean({ description: 'Forcefully overwrite existing files' })
@flags.boolean({ description: 'Forcefully overwrite existing files', alias: 'f' })
declare force?: boolean

/**
Expand Down
2 changes: 1 addition & 1 deletion commands/generate_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { BaseCommand, flags } from '../modules/ace/main.js'
*/
export default class GenerateKey extends BaseCommand {
static commandName = 'generate:key'
static description = 'Generate a secure random application key'
static description = 'Generate a cryptographically secure random application key'

@flags.boolean({
description: 'Display the key on the terminal, instead of writing it to .env file',
Expand Down
10 changes: 5 additions & 5 deletions commands/make/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ export default class MakeController extends BaseCommand {
@args.string({ description: 'The name of the controller' })
declare name: string

@args.spread({ description: 'Method names to pre-define on the controller', required: false })
@args.spread({ description: 'Create controller with custom method names', required: false })
declare actions?: string[]

@flags.boolean({
description: 'Convert controller class and file name to its singular form',
alias: 'r',
description: 'Generate controller in singular form',
alias: 's',
})
declare singular: boolean

@flags.boolean({
description: 'Generate controller with resource actions',
description: 'Generate controller with methods to perform CRUD actions on a resource',
alias: 'r',
})
declare resource: boolean

@flags.boolean({
description: 'Generate controller with api resource actions',
description: 'Generate resourceful controller with the "edit" and the "create" methods',
alias: 'a',
})
declare api: boolean
Expand Down
2 changes: 1 addition & 1 deletion commands/make/exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { args, BaseCommand } from '../../modules/ace/main.js'
*/
export default class MakeException extends BaseCommand {
static commandName = 'make:exception'
static description = 'Create a new ace exception class'
static description = 'Create a new custom exception class'

@args.string({ description: 'Name of the exception' })
declare name: string
Expand Down
2 changes: 1 addition & 1 deletion commands/make/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class MakeListener extends BaseCommand {
static commandName = 'make:listener'
static description = 'Create a new event listener class'

@args.string({ description: 'Name of the listener' })
@args.string({ description: 'Name of the event listener' })
declare name: string

@flags.string({
Expand Down
4 changes: 2 additions & 2 deletions commands/make/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import { args, BaseCommand, flags } from '../../modules/ace/main.js'
*/
export default class MakeMiddleware extends BaseCommand {
static commandName = 'make:middleware'
static description = 'Create a new middleware class'
static description = 'Create a new middleware class for HTTP requests'

@args.string({ description: 'Name of the middleware' })
declare name: string

@flags.string({ description: 'The stack in which to register the middleware' })
@flags.string({ description: 'The stack in which to register the middleware', alias: 's' })
declare stack?: 'server' | 'named' | 'router'

/**
Expand Down
59 changes: 14 additions & 45 deletions commands/make/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,15 @@ export default class MakePreload extends BaseCommand {

@flags.array({
description: `Define the preload file's environment. Accepted values are "${ALLOWED_ENVIRONMENTS}"`,
alias: 'e',
})
declare environments: AllowedAppEnvironments
declare environments?: AllowedAppEnvironments

/**
* The stub to use for generating the preload file
*/
protected stubPath: string = 'make/preload/main.stub'

/**
* Check if the mentioned environments are valid
*/
#isValidEnvironment(environment: string[]): environment is AllowedAppEnvironments {
return !environment.find((one) => !ALLOWED_ENVIRONMENTS.includes(one as any))
}

/**
* Validate the environments flag passed by the user
*/
Expand All @@ -50,37 +44,13 @@ export default class MakePreload extends BaseCommand {
return true
}

return this.#isValidEnvironment(this.environments)
}

/**
* Prompt for the environments
*/
async #promptForEnvironments(): Promise<AllowedAppEnvironments> {
const selectedEnvironments = await this.prompt.multiple(
'Select the environment(s) in which you want to load this file',
[
{ name: 'all', message: 'Load file in all environments' },
{ name: 'console', message: 'Environment for ace commands' },
{ name: 'repl', message: 'Environment for the REPL session' },
{ name: 'web', message: 'Environment for HTTP requests' },
{ name: 'test', message: 'Environment for the test process' },
] as const
)

if (selectedEnvironments.includes('all')) {
return ['web', 'console', 'test', 'repl']
}

return selectedEnvironments as AllowedAppEnvironments
return this.environments.every((one) => ALLOWED_ENVIRONMENTS.includes(one))
}

/**
* Run command
*/
async run() {
let environments: AllowedAppEnvironments = this.environments

/**
* Ensure the environments are valid when provided via flag
*/
Expand All @@ -91,26 +61,25 @@ export default class MakePreload extends BaseCommand {
return
}

/**
* Prompt for the environments when not defined
*/
if (!environments) {
environments = await this.#promptForEnvironments()
}

const codemods = await this.createCodemods()
const output = await codemods.makeUsingStub(stubsRoot, this.stubPath, {
flags: this.parsed.flags,
entity: this.app.generators.createEntity(this.name),
})

/**
* Registering the preload file with the `adonisrc.js` file. We register
* Registering the preload file with the `adonisrc.ts` file. We register
* the relative path, since we cannot be sure about aliases to exist.
*/
const preloadImportPath = `./${output.relativeFileName.replace(/(\.js|\.ts)$/, '')}.js`
await codemods.updateRcFile((rcFile) => {
rcFile.addPreloadFile(preloadImportPath, environments)
})
try {
const preloadImportPath = `./${output.relativeFileName.replace(/(\.js|\.ts)$/, '')}.js`
await codemods.updateRcFile((rcFile) => {
rcFile.addPreloadFile(preloadImportPath, this.environments)
})
} catch (_) {
this.logger.warning(
'Unable to register preload file inside the adonisrc.ts file. Make sure to manually register it'
)
}
}
}
46 changes: 41 additions & 5 deletions commands/make/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
*/

import { stubsRoot } from '../../stubs/main.js'
import { args, BaseCommand } from '../../modules/ace/main.js'
import type { AppEnvironments } from '../../types/app.js'
import { args, BaseCommand, flags } from '../../modules/ace/main.js'

const ALLOWED_ENVIRONMENTS = ['web', 'console', 'test', 'repl'] satisfies AppEnvironments[]
type AllowedAppEnvironments = typeof ALLOWED_ENVIRONMENTS

/**
* Make a new provider class
Expand All @@ -20,12 +24,38 @@ export default class MakeProvider extends BaseCommand {
@args.string({ description: 'Name of the provider' })
declare name: string

@flags.array({
description: `Define the provider environment. Accepted values are "${ALLOWED_ENVIRONMENTS}"`,
alias: 'e',
})
declare environments?: AllowedAppEnvironments

/**
* The stub to use for generating the provider class
*/
protected stubPath: string = 'make/provider/main.stub'

/**
* Validate the environments flag passed by the user
*/
#isEnvironmentsFlagValid() {
if (!this.environments || !this.environments.length) {
return true
}
return this.environments.every((one) => ALLOWED_ENVIRONMENTS.includes(one))
}

async run() {
/**
* Ensure the environments are valid when provided via flag
*/
if (!this.#isEnvironmentsFlagValid()) {
this.logger.error(
`Invalid environment(s) "${this.environments}". Only "${ALLOWED_ENVIRONMENTS}" are allowed`
)
return
}

const codemods = await this.createCodemods()
const output = await codemods.makeUsingStub(stubsRoot, this.stubPath, {
flags: this.parsed.flags,
Expand All @@ -36,9 +66,15 @@ export default class MakeProvider extends BaseCommand {
* Registering the provider with the `adonisrc.js` file. We register
* the relative path, since we cannot be sure about aliases to exist.
*/
const providerImportPath = `./${output.relativeFileName.replace(/(\.js|\.ts)$/, '')}.js`
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider(providerImportPath)
})
try {
const providerImportPath = `./${output.relativeFileName.replace(/(\.js|\.ts)$/, '')}.js`
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider(providerImportPath, this.environments)
})
} catch (_) {
this.logger.warning(
'Unable to register provider inside the adonisrc.ts file. Make sure to manually register it'
)
}
}
}
2 changes: 1 addition & 1 deletion commands/make/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class MakeTest extends BaseCommand {
@args.string({ description: 'Name of the test file' })
declare name: string

@flags.string({ description: 'The suite for which to create the test file' })
@flags.string({ description: 'The suite for which to create the test file', alias: 's' })
declare suite?: string

/**
Expand Down
8 changes: 5 additions & 3 deletions commands/make/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import { args, flags, BaseCommand } from '../../modules/ace/main.js'
*/
export default class MakeValidator extends BaseCommand {
static commandName = 'make:validator'
static description = 'Create a new VineJS validator'
static description = 'Create a new file to define VineJS validators'

@args.string({ description: 'Name of the validator' })
@args.string({ description: 'Name of the validator file' })
declare name: string

@flags.boolean({ description: 'Define actions to validate CRUD operations' })
@flags.boolean({
description: 'Create a file with pre-defined validators for create and update actions',
})
declare resource: boolean

/**
Expand Down
7 changes: 5 additions & 2 deletions commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ export default class Serve extends BaseCommand {

declare devServer: DevServer

@flags.boolean({ description: 'Watch filesystem and restart the HTTP server on file change' })
@flags.boolean({
description: 'Watch filesystem and restart the HTTP server on file change',
alias: 'w',
})
declare watch?: boolean

@flags.boolean({ description: 'Use polling to detect filesystem changes' })
@flags.boolean({ description: 'Use polling to detect filesystem changes', alias: 'p' })
declare poll?: boolean

@flags.boolean({
Expand Down

0 comments on commit 45d8134

Please sign in to comment.