Skip to content

Commit

Permalink
fix: display clean error when user didnt selected an adonis project w…
Browse files Browse the repository at this point in the history
…hen prompted, close #3

* refactor commands execution to handle errors
  • Loading branch information
Julien-R44 committed Apr 30, 2022
1 parent 9b8fd86 commit f18947d
Show file tree
Hide file tree
Showing 20 changed files with 158 additions and 146 deletions.
50 changes: 48 additions & 2 deletions src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import { exec as baseExec } from 'child_process'
import { join } from 'path'
import Config from '../utilities/config'
import Extension, { AdonisProject } from '../Extension'
import { capitalize } from '../utilities/functions'
const exec = promisify(baseExec)

let outputChannel = window.createOutputChannel('AdonisJS')

export enum ExtensionErrors {
ERR_ADONIS_PROJECT_SELECTION_NEEDED,
}

export default class BaseCommand {
/**
* Show a message to the user
Expand Down Expand Up @@ -114,14 +119,55 @@ export default class BaseCommand {
{ placeHolder: 'Select the project in which you want to run this command' }
)

return adonisProjects.find((project) => project.path === target!.description)!
return adonisProjects.find((project) => project.path === target?.description)
}

/**
* Execute execCmd and handle errors/success notifications
*/
protected static async handleExecCmd({
command,
successMessage,
errorMessage,
fileType = 'file',
openCreatedFile = false,
}: {
command: string
successMessage?: string
errorMessage?: string
fileType?: string
openCreatedFile?: boolean
}) {
successMessage = successMessage || `${capitalize(fileType)} created successfully`
errorMessage = errorMessage || `Failed to create ${fileType.toLowerCase()}`

try {
const res = await this.execCmd(command)

if (openCreatedFile) {
this.openCreatedFile(res.adonisProject, res.result!.stdout)
}

this.showMessage(successMessage)
} catch (err) {
// @ts-ignore
if (err.errorCode === ExtensionErrors.ERR_ADONIS_PROJECT_SELECTION_NEEDED) {
return this.showError('You must select an AdonisJS project on which to run your command.')
}

this.showError(errorMessage, err)
}
}

/**
* Execute the final `node ace x` command
*/
protected static async execCmd(command: string, background: boolean = true) {
let adonisProject = (await this.pickAdonisProject())!
let adonisProject = await this.pickAdonisProject()

if (!adonisProject) {
return Promise.reject({ errorCode: ExtensionErrors.ERR_ADONIS_PROJECT_SELECTION_NEEDED })
}

/**
* If we are on windows, we need the remove the first slash
Expand Down
15 changes: 5 additions & 10 deletions src/commands/generate/Manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ import BaseCommand from '../BaseCommand'
*/
export class Manifest extends BaseCommand {
public static async run() {
/**
* Execute the command
*/
try {
let command = `generate:manifest`
await this.execCmd(command)
await this.showMessage('Manifest has been generated.')
} catch (err) {
this.showError('Could not generate the manifest file.', err)
}
return this.handleExecCmd({
command: 'generate:manifest',
successMessage: 'Manifest generated successfully.',
errorMessage: 'Manifest generation failed.',
})
}
}
7 changes: 6 additions & 1 deletion src/commands/list/Routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BaseCommand from '../BaseCommand'
import BaseCommand, { ExtensionErrors } from '../BaseCommand'
import { HtmlTable } from '../../helpers/HtmlTable'

export class RouteList extends BaseCommand {
Expand All @@ -10,6 +10,11 @@ export class RouteList extends BaseCommand {
const { headers, rows } = this.parseRoutes(res.result!.stdout)
HtmlTable.openPanelWithTable('routes-list', 'Route List', headers, rows)
} catch (err) {
// @ts-ignore
if (err.errorCode === ExtensionErrors.ERR_ADONIS_PROJECT_SELECTION_NEEDED) {
return this.showError('You must select an AdonisJS project on which to run your command.')
}

return this.showError('The route list could not be generated', err)
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/commands/make/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export class Command extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `make:command ${commandName}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('Command created successfully.')
} catch (err) {
this.showError('Could not create the command.', err)
}
return this.handleExecCmd({
command: `make:command ${commandName}`,
fileType: 'command',
openCreatedFile: true,
})
}
}
13 changes: 5 additions & 8 deletions src/commands/make/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ export class Controller extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `make:controller ${controllerName} ${resource ? '-r' : ''}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('Controller created successfully.')
} catch (err) {
this.showError('Could not create the controller.', err)
}
return this.handleExecCmd({
command: `make:controller ${controllerName} ${resource ? '-r' : ''}`,
fileType: 'controller',
openCreatedFile: true,
})
}
}
17 changes: 7 additions & 10 deletions src/commands/make/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ export class Exception extends BaseCommand {
/**
* Get the exception name
*/
let commandName = await this.getInput('Exception name')
if (!commandName) {
let exceptionName = await this.getInput('Exception name')
if (!exceptionName) {
this.showError('Command name is required.')
return
}

/**
* Execute the command
*/
try {
let command = `make:exception ${commandName}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('Exception created successfully.')
} catch (err) {
this.showError('Could not create the exception.', err)
}
return this.handleExecCmd({
command: `make:exception ${exceptionName}`,
fileType: 'exception',
openCreatedFile: true,
})
}
}
17 changes: 7 additions & 10 deletions src/commands/make/Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ export class Middleware extends BaseCommand {
/**
* Get the middleware name
*/
let commandName = await this.getInput('Middleware name')
if (!commandName) {
let name = await this.getInput('Middleware name')
if (!name) {
this.showError('Middleware name is required.')
return
}

/**
* Execute the command
*/
try {
let command = `make:middleware ${commandName}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('Middleware created successfully.')
} catch (err) {
this.showError('Could not create the middleware.', err)
}
return this.handleExecCmd({
command: `make:middleware ${name}`,
fileType: 'middleware',
openCreatedFile: true,
})
}
}
13 changes: 6 additions & 7 deletions src/commands/make/Migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ export class Migration extends BaseCommand {
const modifyPart = `${modifyTable ? `--table=${tableName}` : ''}`
const connectionPart = `${dbName ? `--database=${dbName}` : ''}`

try {
let command = `${basePart} ${createPart} ${modifyPart} ${connectionPart}`
await this.execCmd(command)
this.showMessage('Migration created successfully.')
} catch (err) {
this.showError('Could not create the migration.', err)
}
let command = `${basePart} ${createPart} ${modifyPart} ${connectionPart}`
return this.handleExecCmd({
command,
fileType: 'migration',
openCreatedFile: true,
})
}
}
19 changes: 9 additions & 10 deletions src/commands/make/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ export class Model extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `make:model ${modelName} --migration=${
generateMigration ? 'true' : 'false'
} --controller=${generateController ? 'true' : 'false'}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('Model created successfully.')
} catch (err) {
this.showError('Could not create the model.', err)
}
let command = `make:model ${modelName} --migration=${
generateMigration ? 'true' : 'false'
} --controller=${generateController ? 'true' : 'false'}`

return this.handleExecCmd({
command,
fileType: 'model',
openCreatedFile: true,
})
}
}
13 changes: 5 additions & 8 deletions src/commands/make/PreloadedFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ export class PreloadedFile extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `make:prldfile ${prldName} --environment=${environment.join(',')}`
const res = await this.execCmd(command, false)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage(`Preloaded file ${prldName} created.`)
} catch (err) {
this.showError('Could not create the seeder.', err)
}
return this.handleExecCmd({
command: `make:prldfile ${prldName} --environment=${environment.join(',')}`,
fileType: 'preloaded file',
openCreatedFile: true,
})
}
}
13 changes: 5 additions & 8 deletions src/commands/make/Seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export class Seeder extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `make:seeder ${seederName}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('Seeder created successfully.')
} catch (err) {
this.showError('Could not create the seeder.', err)
}
return this.handleExecCmd({
command: `make:seeder ${seederName}`,
fileType: 'seeder',
openCreatedFile: true,
})
}
}
13 changes: 5 additions & 8 deletions src/commands/make/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ export class Test extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `make:test ${suiteName} ${testName}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('Test created successfully.')
} catch (err) {
this.showError('Could not create the test.', err)
}
return this.handleExecCmd({
command: `make:test ${suiteName} ${testName}`,
fileType: 'test',
openCreatedFile: true,
})
}
}
13 changes: 5 additions & 8 deletions src/commands/make/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export class Validator extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `make:validator ${seederName}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('Validator created successfully.')
} catch (err) {
this.showError('Could not create the validator.', err)
}
return this.handleExecCmd({
command: `make:validator ${seederName}`,
fileType: 'validator',
openCreatedFile: true,
})
}
}
13 changes: 5 additions & 8 deletions src/commands/make/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export class View extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `make:view ${viewName}`
const res = await this.execCmd(command)
this.openCreatedFile(res.adonisProject, res.result!.stdout)
this.showMessage('View created successfully.')
} catch (err) {
this.showError('Could not create the view.', err)
}
return this.handleExecCmd({
command: `make:view ${viewName}`,
fileType: 'view',
openCreatedFile: true,
})
}
}
15 changes: 6 additions & 9 deletions src/commands/migration/Fresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ export class Fresh extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `migration:fresh ${dbName ? `--database=${dbName}` : ''} ${
seed ? '--seed' : ''
}`
await this.execCmd(command)
this.showMessage('Database has been successfully freshed.')
} catch (err) {
this.showError('Could not execute migration:fresh', err)
}
let command = `migration:fresh ${dbName ? `--database=${dbName}` : ''} ${seed ? '--seed' : ''}`
return this.handleExecCmd({
command,
successMessage: 'Migration:fresh command executed successfully.',
errorMessage: 'Migration:fresh command failed.',
})
}
}
18 changes: 9 additions & 9 deletions src/commands/migration/Refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export class Refresh extends BaseCommand {
/**
* Execute the command
*/
try {
let command = `migration:refresh ${dbName ? `--database=${dbName}` : ''} ${
seed ? '--seed' : ''
}`
await this.execCmd(command)
this.showMessage('Database has been successfully refreshed.')
} catch (err) {
this.showError('Could not execute migration:fresh', err)
}
let command = `migration:refresh ${dbName ? `--database=${dbName}` : ''} ${
seed ? '--seed' : ''
}`

return this.handleExecCmd({
command,
successMessage: 'Migration:refresh command executed successfully.',
errorMessage: 'Migration:refresh command failed.',
})
}
}
Loading

0 comments on commit f18947d

Please sign in to comment.