generated from AthennaIO/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): add install mail component command
- Loading branch information
Showing
11 changed files
with
1,806 additions
and
670 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.