Skip to content

Commit

Permalink
feat: add default to email
Browse files Browse the repository at this point in the history
  • Loading branch information
vafanassieff committed May 28, 2022
1 parent dffc4e9 commit a0954e1
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 37 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ To update the package run `npm update -g @vafanassieff/email-alias`

## Usage

Start by using the command `email-alias configuration` to choose your default to email and your default provider

```Shell
Usage: email-alias [options] [command]

Expand Down
23 changes: 14 additions & 9 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { URL } from 'node:url'
import { Command } from 'commander'

import add from '#src/base/add.js'
import configuration from '#src/base/configuration.js'
import list from '#src/base/list.js'
import printConfiguration from '#src/base/print-configuration.js'
import remove from '#src/base/remove.js'
import configure from '#src/provider/configure.js'
import set from '#src/provider/set.js'

const program = new Command()

Expand All @@ -33,23 +34,27 @@ provider
.argument('<provider>', 'Provider to configure')
.action(configure)

provider
.command('set')
.description('Set the used provider')
.argument('<provider>', 'Provider to use')
.action(set)

program.name('email-alias').description('Manage email alias').version(version)

program.command('list').description('List current alias').action(list)

program
.command('add')
.description('Add a new alias')
.requiredOption('--from <string>', 'alias email')
.requiredOption('--to <string>', 'Destination email')
.argument('<from>', 'alias email')
.option('--to <string>', 'Destination email')
.action(add)

program
.command('configuration')
.description('Configure the application')
.action(configuration)

program
.command('print-config')
.description('Print the app configuration')
.action(printConfiguration)

program
.command('remove')
.description('Add a new alias')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vafanassieff/email-alias",
"version": "0.1.3",
"version": "0.1.4",
"description": "Create a email alias to keep you real email hidden !",
"main": "src/index.js",
"repository": {
Expand Down
6 changes: 3 additions & 3 deletions src/base/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { loadConfig, loadProvider } from '#src/utils.js'
/**
* Add a new redirecion to the current provider
*
* @param {string} from From address
* @param {object} params Object
* @param {string} params.from From address
* @param {string} params.to To address
*/
export default async ({ from, to }) => {
export default async (from, { to }) => {
const appConfig = await loadConfig()
const provider = await loadProvider(appConfig.provider)

await provider.add({ from, to }, appConfig[appConfig.provider])
await provider.add({ from, to }, appConfig[appConfig.provider], appConfig)
console.log(`New alias from ${from} to ${to} !`)
}
41 changes: 41 additions & 0 deletions src/base/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import console from 'node:console'
import fs from 'node:fs/promises'

import config from '#src/config.js'
import configure from '#src/provider/configure.js'
import { loadConfig, getProviders } from '#src/utils.js'

/**
* Configure the application
*/
export default async () => {
const appConfig = await loadConfig()

appConfig.defaultTo = await question(
'On which email do you want a redirection by default ? ',
{
choices: [appConfig.defaultTo],
}
)

const providers = await getProviders()

appConfig.provider = await question('Which provider will you use ? ', {
choices: providers,
})

await fs.writeFile(config.path, JSON.stringify(appConfig, undefined, 2))

console.log('Successfully configure email-alias ! Yai !')

const shouldConfigProvider = await question(
`Do you want to configure the ${appConfig.provider} provider ? `,
{
choices: ['Yes', 'No'],
}
)

if (shouldConfigProvider === 'Yes') {
await configure(appConfig.provider)
}
}
2 changes: 1 addition & 1 deletion src/base/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { loadConfig, loadProvider } from '#src/utils.js'
export default async () => {
const appConfig = await loadConfig()
const provider = await loadProvider(appConfig.provider)
const list = await provider.list(appConfig[appConfig.provider])
const list = await provider.list(appConfig[appConfig.provider], appConfig)

console.log(`Found ${list.length} alias :`)

Expand Down
11 changes: 11 additions & 0 deletions src/base/print-configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import console from 'node:console'

import { loadConfig } from '#src/utils.js'

/**
* Print the app configuration
*/
export default async () => {
const config = await loadConfig()
console.log(config)
}
2 changes: 1 addition & 1 deletion src/base/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async (alias) => {
const appConfig = await loadConfig()
const provider = await loadProvider(appConfig.provider)

await provider.remove(alias, appConfig[appConfig.provider])
await provider.remove(alias, appConfig[appConfig.provider], appConfig)

console.log(`Successfully removed ${alias} !`)
}
18 changes: 0 additions & 18 deletions src/provider/set.js

This file was deleted.

10 changes: 6 additions & 4 deletions src/providers/ovh.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ const request = async (path, method, parameters, config) => {
* @returns {OvhConfiguration} User OVH configuration
*/
export const configure = async () => {
const endpoint = await question('Which api endpoint should we use ? ')
const endpoint = await question('Which api endpoint should we use ? ', {
choices: Object.keys(endpoints),
})

if (!endpoint) {
throw new Error('You need a endpoint', Object.keys(endpoints))
throw new Error('You need a endpoint')
}

const applicationKey = await question('What is your OVH application key ? ')
Expand Down Expand Up @@ -152,15 +154,15 @@ export const list = async (config) => {
* @param {OvhConfiguration} config OVH configuration
* @returns {Array<string>} List of alias
*/
export const add = async ({ from, to }, config) => {
export const add = async ({ from, to }, config, { defaultTo }) => {
if (!from.includes(config.domain)) {
throw new Error(`From address should use the ${config.domain} domain`)
}

await request(
`/email/domain/${config.domain}/redirection`,
'POST',
{ from, to, localCopy: false },
{ from, to: to || defaultTo, localCopy: false },
config
)
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ export const loadProvider = async (name) => {

return import(providerPath)
}

/**
* Get all the supported providers
*
* @returns {Array<string>} Array of providers name
*/
export const getProviders = async () => {
const providerFolder = path.join(__dirname, '../src/providers')
const files = await fs.readdir(providerFolder)
return files.map((p) => p.split('.js')[0])
}

0 comments on commit a0954e1

Please sign in to comment.