Skip to content

Commit

Permalink
Merge pull request #36 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(commands): add install mail component command
  • Loading branch information
jlenon7 authored Dec 26, 2022
2 parents 4fe78db + 8369702 commit 5f07206
Show file tree
Hide file tree
Showing 11 changed files with 1,806 additions and 670 deletions.
15 changes: 4 additions & 11 deletions app/Console/Commands/Install/DatabaseCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import { File, Path } from '@athenna/common'
import { Command, FilePropertiesHelper } from '@athenna/artisan'
import { NotFoundDatabaseException } from '#app/Exceptions/NotFoundDatabaseException'

export class InstallTestCommand extends Command {
/**
* Resolve any dependency of the service container
* inside the constructor.
*/
constructor() {
super()
}

export class InstallDatabaseCommand extends Command {
/**
* The name and signature of the console command.
*/
Expand All @@ -37,7 +29,7 @@ export class InstallTestCommand extends Command {
.option('--no-lint', 'Do not run eslint in the command.', true)
.option(
'--db <db>',
'Set the database to be configured in project. Current databases available: mysql and postgres.',
'Set the database to be configured in project. Current databases available: mysql, mongo and postgres.',
'postgres',
)
}
Expand All @@ -53,7 +45,7 @@ export class InstallTestCommand extends Command {

this.title('INSTALLING DATABASE COMPONENT\n', 'bold', 'green')

const availableDatabases = ['mysql', 'postgres']
const availableDatabases = ['mysql', 'mongo', 'postgres']

if (!availableDatabases.includes(options.db)) {
throw new NotFoundDatabaseException(options.db)
Expand Down Expand Up @@ -82,6 +74,7 @@ export class InstallTestCommand extends Command {
const dictionary = {
mysql: 'mysql2',
postgres: 'pg',
mongo: 'mongoose',
}

const cdCommand = `cd ${projectPath}`
Expand Down
180 changes: 180 additions & 0 deletions app/Console/Commands/Install/MailCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { File, Path } from '@athenna/common'
import { Command, FilePropertiesHelper } from '@athenna/artisan'

export class InstallMailCommand extends Command {
/**
* The name and signature of the console command.
*/
get signature() {
return 'install:mail'
}

/**
* The console command description.
*/
get description() {
return 'Install the mail component in your Athenna application.'
}

/**
* Set additional flags in the commander instance.
* This method is executed when registering your command.
*
* @param {import('@athenna/artisan').Commander} commander
* @return {import('@athenna/artisan').Commander}
*/
addFlags(commander) {
return commander.option(
'--no-lint',
'Do not run eslint in the command.',
true,
)
}

/**
* Execute the console command.
*
* @param {any} options
* @return {Promise<void>}
*/
async handle(options) {
this.log(this.createRainbow('Athenna'))

this.title('INSTALLING MAIL COMPONENT\n', 'bold', 'green')

const projectPath = `${Env('CALL_PATH')}`

await this.installAthennaMailPackage(projectPath)
await this.createMailConfigFile(projectPath)
await this.addMailProviderToAppConfig(projectPath)
await this.addEnvVarsToEnvFile(projectPath)

if (options.lint) {
await this.lintProject(projectPath)
}

console.log()

this.success('Athenna mail component successfully installed.')
}

async installAthennaMailPackage(projectPath) {
const cdCommand = `cd ${projectPath}`
const npmInstallCommand = `${cdCommand} && npm install @athenna/mail --production=false`

await this.execCommand(
npmInstallCommand,
`Installing @athenna/mail package in your project`,
)
}

async createMailConfigFile(projectPath) {
const mailConfigFile = `${projectPath}/config/mail.js`
const message = 'Creating config/mail.js file in project'

const spinner = this.createSpinner(message)

if (message) {
spinner.color = 'yellow'

spinner.start()
}

try {
await new File(Path.resources(`scaffolds/mailComponent/config/mail.js`))
.loadSync()
.copy(mailConfigFile)

if (message) spinner.succeed(message)
} catch (err) {
if (message) spinner.fail(message)

throw err
}
}

async addMailProviderToAppConfig(projectPath) {
const appConfigPath = `${projectPath}/config/app.js`
const message = 'Registering MailProvider in config/app.js'

const spinner = this.createSpinner(message)

if (message) {
spinner.color = 'yellow'

spinner.start()
}

try {
await FilePropertiesHelper.addContentToArrayProperty(
appConfigPath,
'providers: ',
"import('@athenna/mail/providers/MailProvider')",
)

if (message) spinner.succeed(message)
} catch (err) {
if (message) spinner.fail(message)

throw err
}
}

async addEnvVarsToEnvFile(projectPath) {
const envFilePath = `${projectPath}/.env`
const envTestFilePath = `${projectPath}/.env.test`
const envExampleFilePath = `${projectPath}/.env.example`
const message =
'Registering env variables in .env, .env.test and .env.example files.'

const spinner = this.createSpinner(message)

if (message) {
spinner.color = 'yellow'

spinner.start()
}

const envVars =
'MAIL_HOST=127.0.0.1\n' +
'MAIL_PORT=587\n' +
'MAIL_USERNAME=\n' +
'MAIL_PASSWORD=\n'

try {
await (await new File(envFilePath).load()).append(envVars)
await (await new File(envTestFilePath).load()).append(envVars)
await (await new File(envExampleFilePath).load()).append(envVars)

if (message) spinner.succeed(message)
} catch (err) {
if (message) spinner.fail(message)

throw err
}
}

async lintProject(projectPath) {
const message = 'Linting project'

const spinner = this.createSpinner(message)

if (message) {
spinner.color = 'yellow'

spinner.start()
}

try {
await this.execCommand(
`cd ${projectPath} && npm run lint:fix --silent -- --quiet`,
)

if (message) spinner.succeed(message)
} catch (err) {
if (message) spinner.fail(message)

throw err
}
}
}
Loading

0 comments on commit 5f07206

Please sign in to comment.